I am trying to learn Python distribute package, and can’t seems figure out the /bin part myself. The Foo package installed and I can reference to it by using from FOO import Foo.
For example, if I had a project named foo, and I have a script, called use-foo, after I
put ‘use-foo’ inside my foo/bin, how I am suppose to use it?
The project source file/directory structure looks like this:
foo
-->FOO (source code for foo)
-->Foo.py
-->bin (script uses Foo goes here)
-->use-foo.py
I’ve tried import use-foo, from foo import use-foo not working, relative path?
Add setup.py:
try:
from setuptools import setup
except ImportError:
from distutils.core import setup
config = {
'description': 'My Foo Project',
'author': 'Paul Liu',
'url': 'URL to get it at.',
'download_url': 'Where to download it.',
'author_email': 'My email.',
'version': '0.1',
'install_requires':['nose'],
'packages':['FOO'],
'scripts':['bin/use-foo.py'],
'name': 'foo'
}
setup(**config)
The source directory alone is only where you will develop your code. It doesn’t really reflect the final product (installed) form until you have it build by one of the available commands (or manually add the various paths to your env).
To develop, what you can do is run this:
python setup.py developThis will install an egg in your python site-packages that is really just a link back to your source location. Meaning that you can actively edit code, and it will reflect in your PYTHONPATH. The build process will place the package
FOOinto your PYTHONPATH, and copy everything in your scripts to the pythonbin. For instance on my machine:Now in your
use-foo.py, you can refer toFOOas the import package, as it will exist in the python path. If you choose not to use thedevelopcommand, you would need to manually addfoo/to your PYTHONPATH, so that it will find theFOOpackage underneath.Most important here: add a
__init__.pyfile (empty) to yourFOOpackage. This is required for python to recognize it as a package.Manual environment paths
Adding paths manually to your shell environment might look like this (bash shell):