I have a simple python application where my directory structure is as follows:
- project/
- main.py
- config.py
- plugins/
- plugin1
- plugin2
- …
Config.py only loads configuration files, it does not contain any configuration info in itself.
I now want to distribute this program, and I thought I’d use setuptools to do it. The file users are expected to use is main.py, so that one clearly goes into /usr/bin and the rest of the files go into /usr/share/project.
But there’s one problem: I would somehow need to tell main.py to look for config.py in the share directory. But I can’t really be sure where exactly the share directory is since that’s up to setuptools, right?
What’s the best practice when distributing Python-based applications?
setuptools install your package in a location which is reachable from python i.e. you can import it:
the problem raise when you do relative imports instead of absolute imports. if your
main.pyimportsconfig.pyit works because they live in the same directory. when you move yourmain.pyto another location like/usr/binor another location present in PATH environment variable, python try to importconfig.pyfromsys.pathand not from your package dir. the solution is to use absolute import:now
main.pyis “movable”.another solution, which i prefer, is using automatic script creation offered by setuptools.
instead of having your code in a
statement, put your code in a function (main could be a good name):
now modify your
setup.py:that’s all. after an install setuptools will create a wrapper script which is callable from shell and that calls your main function. now
main.pycan live in your project directory and you don’t need anymore to move it in abin/directory. note that setuptools automatically puts this script in thebin/directory relative to the installation prefix.es.
install your project package in
and your script in
so be sure that
~/.local/binis present in your PATH env.more info at: http://peak.telecommunity.com/DevCenter/setuptools#automatic-script-creation