Import, how to change sys.path on Windows, and module naming?

Steve Holden steve at holdenweb.com
Sat Mar 1 13:23:30 EST 2008


Jeremy Nicoll - news posts wrote:
> If I understand correctly, when I import something under Windows, Python
> searches the directory that the executing script was loaded from, then other
> directories as specified in "sys.path".
> 
Technically, I believe it just puts the script's directory at the start 
of sys.path, then uses sys.path as its module search path.

> I assume there are standard locations inside my installed Python - in my
> case inside:  C:\Program Files\~P-folder\Python25  - where I could put my
> modules and they'd automatically be found?  But even if that's the norm, I
> don't want to put my own modules in such directories, partly because a
> uninstall or reinstall or upgrade of Python might lose my stuff, and partly
> because I don't believe in mixing distributed code with home-grown code. 
> 
There's Lib/site-packages, which is where third-party code normally goes.

> However I'm quite happy to have a line or three of code to alter  sys.path 
> to suit my set-up, if that's a normal way to handle this problem.  Where
> does one do that?
> 
Take a look at site.py, and see that it runs sitecustomize.py when present.

> 
> Also, I presume that there's a risk that the module name that I give to any
> of my utilities will clash with a present or future standard module's name.
> Does that mean that I should give my own modules names like "JNfoo" rather
> than "foo", etc?  Or something like "JNutils.foo"?
> 
You can always be sure your own modules will be loaded in preference to 
shadowing system modules if you put your directories on the path before 
the standard directories, but most people don't spend a lot of time 
worrying about this.

regards
  Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC              http://www.holdenweb.com/




More information about the Python-list mailing list