[Tutor] Importing Files

alan.gauld@bt.com alan.gauld@bt.com
Mon, 21 Oct 2002 16:57:19 +0100


> other words will "import" only work if the file is in a specific
> place/specific places?

No.
import looks at the sys.path setting and searches 
all of the folders in that path.

It also uses the PYTHONPATH environment variable but 
I suspect that it does so by adding the folders 
defined there to the sys.path settings at startup.

What this means is that you can define a default set 
of folders in PYTHONPATH and then add others as 
needed within a program by modifying sys.path.

In addition there is the package magic that uses the 
init files within folders to allow searching of 
folders nested within the top level ones defined 
in sys.path.... Read the reference pages in the 
documentation on packages for the full story.

As a for instance, I have python installed in D:\python
and save all my reusable modules in D:\PROJECTS\MODULES
I also have project source code in:

D:\PROJECTS\<PROJNAME>

Thus I can import modules from the standard python places
(the D:\PYTHON..... part), my libraries(defined in 
PYTHONPATH) and from my current folder (C:\PROJECTS\PROJNAME)

HTH,

Alan g.
Author of the 'Learning to Program' web site
http://www.freenetpages.co.uk/hp/alan.gauld