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

bockman at virgilio.it bockman at virgilio.it
Mon Mar 3 11:12:20 EST 2008


On 1 Mar, 20:17, Steve Holden <st... at holdenweb.com> wrote:
> Jeremy Nicoll - news posts wrote:
>
>
>
> > Jeremy Nicoll - news posts <jn.nntp.scrap... at wingsandbeaks.org.uk> 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".
>
> > Sorry to followup my own question, but I ran
>
> >  for p,q in enumerate(sys.path): print p, q
>
> > and got:
>
> > 0 C:\Documents and Settings\Laptop\My Documents\JN_PythonPgms
> > 1 C:\Program Files\~P-folder\Python25\Lib\idlelib
> > 2 C:\WINDOWS\system32\python25.zip
> > 3 C:\Program Files\~P-folder\Python25\DLLs
> > 4 C:\Program Files\~P-folder\Python25\lib
> > 5 C:\Program Files\~P-folder\Python25\lib\plat-win
> > 6 C:\Program Files\~P-folder\Python25\lib\lib-tk
> > 7 C:\Program Files\~P-folder\Python25
> > 8 C:\Program Files\~P-folder\Python25\lib\site-packages
> > 9 C:\Program Files\~P-folder\Python25\lib\site-packages\win32
> > 10 C:\Program Files\~P-folder\Python25\lib\site-packages\win32\lib
> > 11 C:\Program Files\~P-folder\Python25\lib\site-packages\Pythonwin
>
> > Does every Windows user have: 2 C:\WINDOWS\system32\python25.zip
> > in their sys.path?  What's the point of having a zip in the path?
>
> So that the files inside the zip can be imported as modules and
> packsges, of course.
>
> > Also, looking in  C:\WINDOWS\system32\   I don't actually have a file called
> > python25.zip, but I do have one called  python25.dll - so has something gone
> > wrong in creation of sys.path?
>
> No. I'm not sure why the zip file is on there by default.
>
> regards
>   Steve
> --
> Steve Holden        +1 571 484 6266   +1 800 494 3119
> Holden Web LLC              http://www.holdenweb.com/- Nascondi testo tra virgolette -
>
> - Mostra testo tra virgolette -

I believe the answer is in how the new import protocol (PEP 302)
works: when you install a new path handler in sys.import_hooks, this
is called for each element of sys.path; the path handler has two
options: either to raise
ImportError, which means that cannot handle the specific path, or to
return an object with the methods defined
in the PEP, which means that the returned object - and only that one -
will be used to import modules in the specific path.

This means that only one path handler can be used for each element of
sys.path. Therefore, if you want to add a
path handler that does not interfere with the other ones, one way to
do it is to add something in sys.path that
is rejected by all path handlers except yours. I believe that
python25.zip is that 'something' that is used by the
zipimporter path handler, which allows to import directly from zip
files.

I did something similar in my toy experiment with the import hooks,
half-believing that there was something I missed. Nice to see that the
'big guys' did the same trick  :-)
(unless of course I _did_ miss something and my guess is completely
wrong; I should have done some experiment
before posting, but I'm too lazy for that).

Ciao
-------
FB



More information about the Python-list mailing list