I have a package in my PYTHONPATH that looks something like this:
package/
__init__.py
module.py
print 'Loading module'
If I’m running Python from the package/ directory (or writing another module in this directory) and type
import module
it loads module.py and prints out “Loading module” as expected. However, if I then type
from package import module
it loads module.py and prints “Loading module” again, which I don’t expect. What’s the rationale for this?
Note: I think I understand technically why Python is doing this, because the sys.modules key for import module is just "module", but for from package import module it’s "package.module". So I guess what I want to know is why the key is different here — why isn’t the file’s path name used as the key so that Python does what one expects here?
Effectively, by running code from the
packagedirectory, you’ve misconfigured Python. You shouldn’t have put that directory onsys.path, since it’s inside a package.Python doesn’t use the filename as the key because it’s not importing a file, it’s importing a module. Allowing people to do ‘
import c:\jim\my files\projects\code\stuff‘ would encourage all kinds of nastiness.Consider this case instead: what if you were in
~/foo/package/and~/barwere onPYTHONPATH– but ~/bar is just a symlink to~/foo? Do you expect Python to resolve, then deduplicate the symbolic link for you? What if you put a relative directory on PYTHONPATH, then change directories? What if ‘foo.py’ is a symlink to ‘bar.py’? Do you expect both of those to be de-duplicated too? What if they’re not symlinks, but just exact copies? Adding complex rules to try to do something convenient in ambiguous circumstances means it does something highly inconvenient for other people. (Python zen 12: in the face of ambiguity, refuse the temptation to guess.)Python does something simple here, and it’s your responsibility to make sure that the environment is set up correctly. Now, you could argue that it’s not a very good idea to put the current directory on
PYTHONPATHby default – I might even agree with you – but given that it is there, it should follow the same consistent set of rules that other path entries do. If it’s intended to be run from an arbitrary directory, your application can always remove the current directory fromsys.pathby starting off withsys.path.remove('').