Search code examples
pythonsetuptools

project/bin after install, howto use the scripts


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) 

Solution

  • 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 develop
    This 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 FOO into your PYTHONPATH, and copy everything in your scripts to the python bin. For instance on my machine:

    Creating /Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/foo.egg-link (link to .)
    Installing use-foo.py script to /Library/Frameworks/Python.framework/Versions/2.6/bin

    Now in your use-foo.py, you can refer to FOO as the import package, as it will exist in the python path. If you choose not to use the develop command, you would need to manually add foo/ to your PYTHONPATH, so that it will find the FOO package underneath.

    Most important here: add a __init__.py file (empty) to your FOO package. 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):

    export PYTHONPATH=/path/to/foo:$PYTHONPATH
    export PATH=/path/to/foo/bin:$PATH