Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Where to put user-written module? 1

Status
Not open for further replies.

mikevh

Programmer
Apr 23, 2001
1,033
US
Hi. I've just written my first Python module, and I'm wondering where to put it so Python will find it when I import it. Here's what I've got in sys.path:
Code:
['', 'C:\\WINDOWS\\system32\\python23.zip', 'C:\\Python23\\Lib\\site-packages\\Pythonwin', 'C:\\Python23\\Lib\\site-packages\\win32', 'C:\\Python23\\Lib\\site-packages\\win32\\lib', 'C:\\Python23\\Lib\\site-packages', 'C:\\Python23\\DLLs', 'C:\\Python23\\lib', 'C:\\Python23\\lib\\plat-win', 'C:\\Python23\\lib\\lib-tk', 'C:\\Python23']
I don't know which, if any, of these directories is appropriate for a user-written module, or what the usual practice is. (I'm a novice at Python, though not at coding.)

I'm using ActivePython v2.3.2, on Win XP, if that matters.
Thanks for any help.

P.S. What's the empty string at the start of sys.path for?
 
I would put it in site-packages. Possibly under a separate directory.
 
If I put the module in site-packages it imports OK. If I put it in a subdirectory under site-packages it doesn't. (Not surprising.) I can live with this if necessary, but I like your suggestion of a separate directory. Is there a way I can get sys.path to include a subdirectory under site-packages? (I know I can do this by modifying sys.path in my code, but I don't mean this, I mean "permanently," so that Python sees it when it starts up.) Thanks.
 
Assuming you module is called "mymodule.py" and you put it under a directory called "mine" in site packages, then you would import it like this:

import mine.mymodule.py
 
Oops, cut and paste error:

import mine.mymodule

Look in the manual under the environment variables to get some ideas about adding a directory to your path.
 
Thanks, eric. I created env var PYTHONPATH with the old contents of sys.path and a new directory under site-packages and put the module there. It imports fine now. I appreciate the help.

Still wondering about the empty string at the front of the path, though. (I left it there.) Do you know what that's for?

 
Thanks again. I just realized that Python inserts the '' at the beginning of sys.path when it starts up and it doesn't need to be in PYTHONPATH.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top