[Newbie:]Importing modules from different files

Robert Kern kernr at mail.ncifcrf.gov
Thu Sep 9 14:16:53 EDT 1999


On Thu, 09 Sep 1999 13:45:08 GMT, gtnorton at my-deja.com wrote:

>                           Hello folks,
>                                      Up until now, I've been putting
>all of my modules in my main Python folder.But now it grows too large to
>find quickly.When I create a folder(still in the main Python file) for
>the scripts and try to import them with: >>>import spam    I get back
>"no module named spam".Please clue me in.
>                                Thanks in advance
>                                              gtnorton
>(P.S.)Running Windows98

I the long standing tradition of this newgroup to post as many
possible, different solutions to a problem (who says there's "only one
way"?), here is what I have done:

Say I have a subdirectory 'Stuff' that has most of my non-package
scripts (like 'compile.py', 'ipp.py', 'codeplot.py', etc.).  I place a
text file in the main Python folder ('C:\Python' for me) called
'Stuff.pth'.  The name is irrelevant, but the '.pth' extension is
necessary.  The content of this file is the single word 'Stuff'.

When the interpreter searches the PYTHONPATH and it comes across such
a file, it will add the directory specified in the file to the search
path.  In general, the content of a '.pth' file can be any directory
specification either relative or absolute.  Therefore, if you have
scripts in a place like 'C:\Even\More\Python\Scripts', the '.pth' file
would contain '/Even/More/Python/Scripts' (following the UNIX path
separator convention because it's easier.  It works on Windows fine).

I know this is documented somewhere, but I don't know where.

Robert Kern           |
----------------------|"In the fields of Hell where the grass grows high
This space            | Are the graves of dreams allowed to die."
intentionally         |           - Richard Harter
left blank.           |




More information about the Python-list mailing list