Organizing a Python project

ivarnelispam at gmail.com ivarnelispam at gmail.com
Mon May 19 01:28:20 EDT 2008


Hello all,

I'm starting work on what is going to become a fairly substantial
Python project, and I'm trying to find the best way to organize
everything. The project will consist of:

- A few applications
- Several small scripts and utilities
- Unit tests and small interactive test programs
- A number of custom libraries and modules that may be shared and
referenced among all of the above

I have the following general structure in mind:

myproject/
  app1/
    main.py
    file1.py
    file2.py
    tests/
      test_abc.py
      test_xyz.py
  app2/
    ...
  scripts/
    script1.py
    script2.py
  shared/
    mylib1/
      file1.py
      file2.py
      tests/
        test_foo.py
        test_bar.py
    mylib2/
      ...


The files that you might want to execute directly are:
  - Any of the "main.py" files under app*/
  - Any of the files under shared/
  - Any of the files under app*/tests or shared/mylib*/tests

So, my questions:
First of all, does this look like a reasonable overall structure, or
are there better alternatives?

Second (and the thing I'm primarily interested in), what is the best
way to deal with importing the shared modules in the applications,
scripts, test programs, and possibly other shared modules? I think the
most obvious solution is to add /path/to/myproject to PYTHONPATH.
However, this seems like an annoying little dependency that you are
likely to forget whenever you move your workspace to a new path, open
up a branch of the project in a different directory, or download and
work on the project using a different computer.

Is there a way to set this up that is a bit more self contained? For
example, at first I was somewhat hopeful that Python could ascend
parent directories until it reached a directory that did not include
an __init__.py file, and it could use this as a root for referring to
packages and modules from any file contained within. (e.g. in the
example project above, any file could refer to myproject.shared.mylib1
so long as 'myproject' and all subdirectories contained an
__init__.py, and the parent of 'myproject' didn't contain such a
file). Evidently this is not the case, but it seems like it could be a
useful feature in these situations.

Anyway, I'm sure this is not an unusual situation, so I'm curious to
hear how other people have handled it.

(Note - don't remove 'spam' from my e-mail address when replying. The
address is correct as listed)

Thanks!
Kevin



More information about the Python-list mailing list