[Tutor] question about sys.path and importing

Kent Johnson kent37 at tds.net
Tue Oct 3 20:38:04 CEST 2006


Dick Moores wrote:
> This morning I was sternly warned by Wingware support not to leave my 
> module of useful functions in Python25\Lib. So I put it in a 
> subfolder in site-packages I named "mine". Importing of or from that 
> module, mycalc.py goes well, to my surprise, because of
> 
>  >>> import sys
>  >>> [x for x in sys.path if "site-packages" in x]
> ['e:\\Python25\\lib\\site-packages', 
> 'e:\\Python25\\lib\\site-packages\\win32', 
> 'e:\\Python25\\lib\\site-packages\\win32\\lib', 
> 'e:\\Python25\\lib\\site-packages\\Pythonwin', 
> 'e:\\Python25\\lib\\site-packages\\wx-2.6-msw-unicode']
>  >>>
> 
> in which "mine" isn't included, even though other folders in site-packages are.
> 
> Can someone explain this, please?

If the program that does the import is also in site-packages\mine, that 
would explain it. When you run a script its directory is added to sys.path.

Normally you will need to either
- make 'mine' be a package, by creating an empty file named 
site-packages\mine\__init__.py, and changing your imports to include the 
package name (from mine import mycalc), or
- add site-packages\mine to sys.path, maybe by creating a .pth file.
http://www.python.org/doc/2.4.3/lib/module-site.html

Gee, maybe I should just invite you over and we can talk ;)

Kent



More information about the Tutor mailing list