python app import guidelines

Tuomas Räsänen 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
> Ubuntu.
> 

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:

  myproject/setup.py
  myproject/src/bin/script
  myproject/src/lib/__init__.py
  myproject/src/lib/helper.py

And setup() in setup.py would resemble the one below:

  distutils.core.setup(
  .
  .
  .
  package_dir={'myproject': 'src/lib'},
  packages=['myproject'],
  scripts=['src/bin/script'],
  .
  .
  )

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
prefix:

  /usr/local/bin/script
  /usr/local/lib/python2.6/dist-packages/myproject
  /usr/local/lib/python2.6/dist-packages/myproject/__init__.py
  /usr/local/lib/python2.6/dist-packages/myproject/helper.py

/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:

  import myproject.helper

There should be no need to play with PYTHONPATH or sys.path.append()
whatsoever.

The package structure works as a namespace. It gives a logical structure
for your project and reduces the risk of mixing identically named
modules.

For further information, please refer to:

  http://docs.python.org/distutils/

When you are building a deb-package for your project, see
https://wiki.ubuntu.com/PackagingGuide/Complete#Packaging%20Python%20modules%20with%20CDBS

I hope this helped. Perhaps more experienced ones can correct my
mistakes and give alternative solutions.

-- 
Tuomas
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 197 bytes
Desc: Digital signature
URL: <https://lists.ubuntu.com/archives/ubuntu-devel-discuss/attachments/20100922/9230f5b6/attachment.sig>


More information about the Ubuntu-devel-discuss mailing list