Basic Packaging strategy question

Steven D'Aprano steve at pearwood.info
Tue Mar 7 00:16:44 EST 2017


On Sat, 04 Mar 2017 15:08:15 -0800, bilmar19 wrote:

> I have a simple project that I want to package to put it on another
> machine but everything I have read so far about packaging ends up
> putting the whole install alongside  with 'packages' - typically in
> .../site-packages.
> This seems a strange place to launch an app from!
> 
> So, if I have an app with a main.py entry point is it ok for everything
> to be in .../site-packages/myproject?

Packaging is one of the more hairy areas of Python, but the general 
advice is that you give your package a __main__.py which is run as the 
"execute this package" entry point. A basic skeleton would be a directory 
like this:

myproject/
+-- __init__.py
+-- __main__.py
+-- utils.py
+-- config.py


Notice that only "init" and "main" are special double-underscore names. 
The __main__.py file should look something like this:



def main():
    print("Hello world!")

if __name__ == '__main__':
    main()



Place the entire myproject directory under site-packages, and you can run 
it as a script as:

    python -m myproject

from the system shell. Under Windows you may need to specify the path to 
python.exe, I don't really know, I'm not a Windows expert.

If you want to provide a single name command for your users, I expect you 
can probably create a simple batch file containing the command, place it 
in the usual Windows executable path (My Applications or whatever it is 
called these days) and in theory it should Just Work.

I expect that setuptools, pip, etc. should be able to install the batch 
file as well as the package, provided you have appropriate Admin access 
to write to My Applications (?), but I've never tried it myself.


[...]
> simple example which ends up with needing to run:
>  python /usr/local/lib/site_packages/myproject/main.py:

Almost correct, it will be spelled __main__ not main, and site-packages 
with a hyphen, not underscore.




-- 
Steve



More information about the Python-list mailing list