[Tutor] sys.path, managing modules and packages

Kent Johnson kent37 at tds.net
Thu Apr 26 13:44:19 CEST 2007


Switanek, Nick wrote:
> Can someone help me better understand how I ought to manage the modules 
> and packages that I download? I often find that I can’t use the code 
> I’ve just downloaded, despite putting it into Lib/site-packages.

site-packages should just contain the module or package itself. For 
example suppose you download the latest version of the wunderbar 
package. It unzips to a folder called wunderbar-0.99 which contains a 
doc folder, an example folder and a wunderbar folder which is the actual 
package. It is the wunderbar folder that should go in site-packages.

Many Python packages come with a setup script called setup.py that does 
the right thing. In this case, just go to the dir containing setup.py 
and type
   python setup.py install

If the downloaded code is a single module (file) rather than a package 
(directory), then put the file into site-packages. Don't put it in a 
subdirectory within site-packages.

> Often 
> I’ve added the subdirectory path via sys.path.append, but this seems to 
> go only one level down, whereas I thought that when trying to import 
> something python would search all subdirectories of the directories in 
> the sys.path.

No, Python doesn't search subdirectories except when looking for 
subpackages. The top-level module or package that you import must be in 
a file or directory that is directly contained in one of the dirs in 
sys.path.

Kent


More information about the Tutor mailing list