Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does "import module" and then "from package import module" load the module again?

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?

like image 981
Ben Hoyt Avatar asked Jun 08 '11 16:06

Ben Hoyt


2 Answers

Effectively, by running code from the package directory, you've misconfigured Python. You shouldn't have put that directory on sys.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 ~/bar were on PYTHONPATH - 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 PYTHONPATH by 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 from sys.path by starting off with sys.path.remove('').

like image 160
Glyph Avatar answered Oct 24 '22 00:10

Glyph


It is a minor defect of the current module system.

When importing module, you do it from the current namespace, which has no name. the values inside this namespace are the same as those in package, but the interpreter cannot know it.

When importing package.module, you import module from the package namespace.

This the reason, that the main.py should be outside the package forlder. Many modules have this organisation :

package /
    main.py
    package /
        sub_package1/
        sub_package2/
        sub_package3/
        module1.py
        module2.py

Calling only main.py make sure the namespaces are correctly set, aka the current namespace is main.py's. Its makes impossible to call import module1.py in module2.py. You'ld need to call import package.module1. Makes things simpler and homogeneous.

And yes, import the current folder as the current nameless folder was a bad idea. It is a PITA if you go beyond a few scripts. But as Python started there, it was not completely senseless.

like image 34
LBarret Avatar answered Oct 24 '22 02:10

LBarret