python app import guidelines
tuos at codegrove.org
Wed Sep 22 18:29:42 UTC 2010
On Wed, Sep 22, 2010 at 12:43:32PM -0500, Stephen Burke wrote:
> I am packaging up a python app to upload it to my PPA eventually.
> Before this everything I have written is in one directory so all my
> imports were simple. Now that I am breaking up the app and the top
> level script is in a "bin" directory and the helper scripts are in a
> "helpers" directory on the same level. How should my imports be with
> this directory structure. Would I modify the PYTHONPATH to add any
> directories I need or is there a better way to do this? I have read
> python docs about imports but I'm wondering if there are any more
> guidelines in terms of imports for python apps that are packaged for
If I understood the question correctly..
I don't think this problem is Ubuntu-specific at all. It's more about
Python's import mechanisms and distutils, but let me tell how I would do
in your situation.
I assume you are using Python version 2.6.
It's a quite good practice to place all your modules inside a package
module. Let's assume that your project is called myproject. Then I would
call the project directory myproject. I would also create a top-level
package called myproject (but the directory name here is lib). It's a
directory with __init__.py file in it.
Directory structure of the project would be something like this:
And setup() in setup.py would resemble the one below:
So the scripts reside in bin-directory and lib-directory would represent
your top-level package, myproject.
python setup.py install will install files by default under /usr/local
/usr/local/bin is by default in PATH and
/usr/local/lib/python2.6./dist-packages in Python's search path.
Once installed, helper.py can be imported with:
There should be no need to play with PYTHONPATH or sys.path.append()
The package structure works as a namespace. It gives a logical structure
for your project and reduces the risk of mixing identically named
For further information, please refer to:
When you are building a deb-package for your project, see
I hope this helped. Perhaps more experienced ones can correct my
mistakes and give alternative solutions.
-------------- next part --------------
A non-text attachment was scrubbed...
Size: 197 bytes
Desc: Digital signature
More information about the Ubuntu-devel-discuss