[Tutor] folder and module
alan.gauld at freenet.co.uk
Sun Sep 17 09:51:15 CEST 2006
> i have test.py under c:\try1\new;
> I want to import b and b.py is under c:\trytry. How to do it?
> Also, I import c and c.py is in the desktop folder but no error
Python imports modules by looking for them in the folders
listed in the variable sys.path. sys.path gets populated when
Python starts up and includes some standard locations plus
any you define in your PYTHONPATH environment variable.
You can also modify sys.path yourself at run-time.
But basically that means that any files you want to import
must be located in a folder in sys.path
So either you save them there, or you add the location to
Personally I save all the modules I will reuse across projects
in a PROJECTS\Lib folder which I added to my PYTHONPATH
environment variable. Modules that I want to import in the
current project only I leave in the current directory.
If your project is big you may want to subdivide the modules
into a folder heirarchy and Python's package system allows
you to do that. But since most beginners won't generate that
many modules in a project I'll leave reading about packages
as an excercise for the keen student! :-)
Author of the Learn to Program web site
More information about the Tutor