[Tutor] Import from project's lib directory?

Cameron Simpson cs at zip.com.au
Fri Aug 29 00:39:48 CEST 2014


On 28Aug2014 22:36, Alan Gauld <alan.gauld at btinternet.com> wrote:
>On 28/08/14 19:03, leam hall wrote:
>>python 2.4.3 on Red Hat Linux.
>>
>>I'm starting a project and want to break the scripts into "input",
>>"output", and "ddl". I'd like to have a "lib" library directory for
>>local modules. The library directory would be at the same level as the
>>others.
>>
>>How do I get the library modules?
>
>Add lib to the sys.path list?
>
>That may not be viable if you need it to be portable across systems, 
>although using os.getcwd to locate the current folder or an
>environment variable to store the app root folder might be
>options there.

I tend to modify $PYTHONPATH outside the script. It means the script is not 
responsible for figuring this stuff out, and it also makes it easier to run the 
"development" version when working.

For example, if I'm off modifying stuff in a copy of the code I test it by 
issuing the command:

   dev python -m the.python.module arguments...

"dev" is a short shell script of my own whose purpose it to set things up to 
run "here", and then run the command supplied ("python").

Among other things it prepends "$PWD/lib/python" to $PYTHONPATH, as that is 
where my modules live. In this way it runs the hacked module instead of running 
the "official" version.  The OP would prepend "$PWD/lib" in the same scenario.

Cheers,
Cameron Simpson <cs at zip.com.au>

Invoking the supernatural can explain anything, and hence explains nothing.
- University of Utah bioengineering professor Gregory Clark


More information about the Tutor mailing list