Organizing a Python project
Casey McGinty
casey.mcginty at gmail.com
Wed May 21 06:44:50 EDT 2008
> 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.
I'd like to add a follow up question. Are there any idioms for writing
/usr/bin scripts to run installed package modules? For this I am assuming a
simple case where there is a single package with one or more modules all
making up a single application. A have a few thoughts, please correct me if
I'm wrong.
1. Script code should be as basic as possible, ideally a module import line
and function or method call. This is so you don't have to worry about script
errors and/or increase startup time because a *.pyc file can not be store in
/usr/bin.
2. In the top of your package directory it is typical to have a module name
'_package.py'. This is ideally where the main command line entry point for
the package code should be placed.
3. In the _package.py file you should add a "class Package" that holds most
or all of the application startup/execution code not designated to other
modules. Then run the application with the call to "Package()", as in
if __name__ == '__main__':
Package()
Some other questions I have are:
A. What should go in the package __init__.py file? For example, a doc
describing the program usage seems helpful, but maybe it should have info
about your modules only? Assuming the __init__.py code gets executed when
you import the module, you could place part or all of the application code
here as well. I'm guessing this is not a good idea, but not really
convinced.
B. How should you import your _package.py module into your /usr/bin script.
Is there a way to use an '__all__' to simplify this? Again this goes back to
question A, should there be any code added to __init__.py?
C. If you have a _package.py file as the application entry, is it worth it
to place most of the application code in a class, described in part 3?
D. When I import a package._package module, I get a lot of junk in my
namespace. I thought an '__all__' define in the module would prevent this,
but it does not seem to work.
Thanks for reading,
- Casey
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080521/2ff1ce7f/attachment-0001.html>
More information about the Python-list
mailing list