[Tutor] functions in other files, executing

Jeff Shannon jeff@ccvcorp.com
Fri, 18 Oct 2002 10:38:07 -0700


Thomi Richards wrote:

> yes, i am aware of the import function. the problem i am trying to get
> around however, is that i have about 20-50 to import, an each one is in
> a different directory. i know where the directories are, but i do not
> want to fill up sys.path with a whole heap of directories...
>
> then again, maybe this is OK?

I'd be kind of wondering why you have such a scattered directory structure,
but it *is* okay.  Rather than dynamically adding them to sys.path directly,
I'd suggest either using a .PTH file or the PYTHONPATH environment variable.

I'd also suggest that, if you have that many modules in so many different
directories, that you try to organize them into one or more packages.  You
can then have just a single import statement (per package), and it'll be
easier to track just where a specific function is defined.

>>> import mypackage
>>> x = mypackage.subpackage1.tools.getdata()

I'd even go so far as to submit that, if your application requires that many
directories but doesn't lend itself to being organized into packages, you
may wish to rethink your architecture.  There might be valid reasons for
doing it as you are, but not knowing just what you're doing, no likely
reasons spring to mind.

Jeff Shannon
Technician/Programmer
Credit International