[Tutor] folder and module

Liam Clarke ml.cyresse at gmail.com
Sun Sep 17 10:09:51 CEST 2006


Hi Linda,

As Alan said, you can modify your sys.path at runtime - to clarify, a
simple example is:

>>> import sys
>>> sys.path.append("c:/trytry")

"import b" should now work.

Regards,

Liam Clarke

On 9/17/06, Alan Gauld <alan.gauld at freenet.co.uk> wrote:
> Linda,
>
> > 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
> > reported.
>
> 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
> sys.path.
>
> 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! :-)
>
> Alan Gauld
> Author of the Learn to Program web site
> http://www.freenetpages.co.uk/hp/alan.gauld
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list