conflict between my package and os standard module

Jan Decaluwe jand at easics.be
Sun Jan 16 14:42:24 EST 2000


Ben Wittner wrote:
> 
> I'm looking for help.
> 
> I have a bunch of extensions to standard modules that are pretty
> specific to the needs of my project.
> 
> I chose to create a package for my project, let's call it my_proj.

Good! Packages seem to be an underused feature in python. In the
recent "Learning Python" book they are even classified as "advanced".
 
> In there, I chose to create modules corresponding to standard
> modules (e.g., my_proj.os, my_proj.re) to hold the project-specific
> extensions of the standard modules.
> 
> I put the directory containing my_proj on my PYTHONPATH,
> but I did NOT put my_proj on my PYTHONPATH.
> 
> In my_proj/os.py I put the following line:
> 
> import os
> 
> Since my_proj is NOT on my PYTHONPATH, I thought that the line above
> would cause the standard module os to be imported.
> Instead, it seems that my_proj/os.py is imported.

You are victim of the "intra-package reference shortcut". The current
directory is always searched first for modules, hence the problem.

This was actually my first frustration when I started using 
python - fortunately also one of the last. I tried exactly the
same thing following the same - in my opinion solid - reasoning.

The reason for the shortcut is that packages are a relatively
new feature in python. Thanks to the shortcut, existing source
directories could easily be put into packages - without 
having to modify module importing code. OK - makes sense.

> Of course, I could rename my extension modules to things like
> os_x.py and re_x.py, but it seems like that should be unecessary
> since I'm already indicating that it's part of my project related package
> by prepending with "my_proj.".
> 
> Any ideas before I rename all my extension packages and change all
> references to them?

You could play tricks with __import__, but I don't recall exactly
how and I wouldn't recommend it.

For now, I would go for the renaming option (that's what I did.)

As a good solution, I have proposed to define a (possibly virtual)
package for the standard modules, so that you could say:

  from standard import os

to avoid the ambiguity. This proposal received some favorable
response, but I have no idea what the current status is.

Regards, Jan

-- 
Jan Decaluwe	       Easics              
Design Manager         System-on-Chip design services  
+32-16-395 600	       Interleuvenlaan 86, B-3001 Leuven, Belgium
mailto:jand at easics.be  http://www.easics.com



More information about the Python-list mailing list