module search path
Alex Martelli
aleax at aleax.it
Mon Feb 24 05:14:48 EST 2003
Wouter van Marle wrote:
> Hi all!
>
> I have the following problem. I have written a nice (well, that's what I
> think) piece of software - a main program and a bunch of *.py files with
> basically all the information. And of course a glade file and some pics.
>
> Now in future, I want the main program to be installed in /usr/local/bin,
> while the rest of the software goes to either /usr/local/lib/sqfax or
> /usr/local/share/sqfax. Anyway - how to import modules in those dirs?
> Working dir is going to be ~/.sqfax. So I need to set something like a
> search path in my main program?
You can do it in several ways, of which, putting in your main program:
import sys
sys.path.insert(0, '/usr/local/lib/sqfax')
may indeed be the simplest to understand.
Personally, however, I would suggest making your "bunch of *.py files"
into a package. Your main program must then code, e.g.:
from sqfax import utilities
rather than just
import utilities
for each module it imports from the sqfax package.
Making sqfax into a package means:
-- directory sqfax must include a file __init__.py -- that file
may be empty, if there's nothing special you want to do the
first time any module of the package gets imported, but it
must be there to indicate that the directory IS a package
-- the PARENT directory to sqfax must be on the Python search
path (and you can put it there in many ways, such as the
above one, a .pth file in site-packages, etc).
The second point is usually best finessed by having sqfax be
a subdirectory of site-packages, but if you have specific reasons
to avoid that, you can. Still, the distutils support that most
easily (though they also let you arrange things differently).
Alex
More information about the Python-list
mailing list