Importing from Another Path
ykingma at accessforall.nl
Tue Jul 24 17:07:46 EDT 2001
Paul Sidorsky wrote:
> I'm working on a client/server project (in Python 2.1) and I've got it
> organized in the familiar structure of client, server, and common
> subdirectories. What is the correct (i.e. simplest and fastest) way to
> import modules from the common directory into the client or server?
> Right now I'm doing it by putting this at the top of each module:
> import sys
> del sys
That is almost as portable as it gets: when also you take
the common directory from the command line sys.argv, eg:
I wouldn't know of a more portable way.
The non portable part is then moved into the script(s) that
start(s) the client and the server.
I prefer to use a command line argument when provided,
and to have some default built in for easy testing.
Also you should be aware that the sys module (any imported module
in fact) is unique in the process that runs the python interpreter,
so you should not add the common directory when it is already there:
if sys.argv not in sys.path: sys.path.append(sys.argv)
More information about the Python-list