[Pythonmac-SIG] Where am I ?

Chris Barker Chris.Barker at noaa.gov
Mon Sep 13 18:38:22 CEST 2004


Jerry LeVan wrote:
> Hi,
> 
> I am fairly new to python, there is a task I need to
> figure out.
> 
> Suppose I have my "main" program in a folder, and various
> support modules in *folders* in the same folder as the
> main program.
> 
> How can I import stuff from the folders? Can I do this
> so that if everything gets moved to  a new location,
> the imports will still work?

What you want to do is make those folders Python packages. All you need
to do to do that is have a __init__.py file in each of them. This module
will get called when you import the package. It can have initialization
stuff it it, or nothing (I usually have a doc string and pass statement)

Once you've done that, you can import the modules in the folders like so:

import FolderName.Modulename

or

from FolderName import Modulename

Python should look in the directory that the current module is in for
importing (in addition to sys.path), so that should be all there is to
it. If you do need to have Python look in odd places for modules, you
can add any path to sys.path:

import sys

sys.path.append("A_new_path")

-Chris





-- 
Christopher Barker, Ph.D.
Oceanographer
                                     		
NOAA/OR&R/HAZMAT         (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov



More information about the Pythonmac-SIG mailing list