disutils, project structure & developing - n00b question

Lie Ryan lie.1296 at gmail.com
Thu Oct 29 18:02:10 EDT 2009


Simon Forman wrote:

> In order for "from pymlb import fetcher" no work you must make the
> './pymlb' directory into a "package" by adding a file called
> __init__.py (it can be empty.)
> 
> Then make sure the "top" directory (i.e. '.' in your example) is in
> the python PATH.  There are a couple of ways to do that:
> 
> 1.) Hack it in demo.py before importing fetcher
>   (i.e. "import sys; sys.path.append(<string absolute path of '.'>)")
> 
> 2.) Use the PYTHONPATH environment variable.
> 
> 3.) Use a .pth file (See http://docs.python.org/library/site.html)
> You'll have to figure out what directory to put it in (on my system
> '/usr/lib/python2.5/site-packages' works) Note, although it's not
> mentioned in the site module docs you can include an absolute path and
> it will be added to sys.path.
> 
> There is additional good information about .pth files on Bob
> Ippolito's blog:
> http://bob.pythonmac.org/archives/2005/02/06/using-pth-files-for-python-development/
> Be sure to read the comments too.
> 
> 4.) Probably some other method(s) that someone else will tell you...  ;]

4.) By importing the module from a main.py script in the main directory, 
and making every imported folder a package (by putting __init__.py 
file). This is the simplest method I found, but has the drawback that 
you can't use a subpackage for execution (only for imports).

i.e.

$ ls
./__init__.py
./setup.py
./pymlb/
./pymln/__init__.py
./pymlb/fetcher.py
./demos
./demos/demo.py
./run_script.py
$ cat run_script.py
#!/usr/bin/env python
from demos import demo
$ cat demos/demo.py
from pymlb import fetcher
$ ./run_script.py
...

or something like that...



More information about the Python-list mailing list