Packages will be imported before modules. Illustrated:
% tree .
.
|-- foo
| |-- __init__.py
| `-- __init__.pyc
`-- foo.py
foo.py
:
% cat foo.py
print 'you have imported foo.py'
foo/__init__.py
:
% cat foo/__init__.py
print 'you have imported foo/__init__.py'
And from interactive interpreter:
>>> import foo
you have imported foo/__init__.py
I have no idea where this is officially documented.
Edit per comment: This was performed with Python 2.7 on Mac OS X 10.6.7. I also performed this using Python 2.6.5 on Ubuntu 10.10 and experienced the same result.
You seem to be misunderstanding how import
searches for modules. When you use an import statement it always searches the actual module path (and/or sys.modules
); it doesn't make use of module objects in the local namespace that exist because of previous imports. When you do:
import package.subpackage.module
from package.subpackage import module
from module import attribute1
The second line looks for a package called package.subpackage
and imports module
from that package. This line has no effect on the third line. The third line just looks for a module called module
and doesn't find one. It doesn't "re-use" the object called module
that you got from the line above.
In other words from someModule import ...
doesn't mean "from the module called someModule that I imported earlier..." it means "from the module named someModule that you find on sys.path...". There is no way to "incrementally" build up a module's path by importing the packages that lead to it. You always have to refer to the entire module name when importing.
It's not clear what you're trying to achieve. If you only want to import the particular object attribute1, just do from package.subpackage.module import attribute1
and be done with it. You need never worry about the long package.subpackage.module
once you've imported the name you want from it.
If you do want to have access to the module to access other names later, then you can do from package.subpackage import module
and, as you've seen you can then do module.attribute1
and so on as much as you like.
If you want both --- that is, if you want attribute1
directly accessible and you want module
accessible, just do both of the above:
from package.subpackage import module
from package.subpackage.module import attribute1
attribute1 # works
module.someOtherAttribute # also works
If you don't like typing package.subpackage
even twice, you can just manually create a local reference to attribute1:
from package.subpackage import module
attribute1 = module.attribute1
attribute1 # works
module.someOtherAttribute #also works
Best Answer
TLDR; a package takes precedence over a module of the same name if they are in the same directory.
From the docs:
This is a bit misleading because the interpreter will also look for a package called
spam
(a directory calledspam
containing an__init__.py
file). Since the directory entries are sorted before searching, packages take precedence over modules with the same name if they are in the same directory becausespam
comes beforespam.py
.Note that "current directory" is relative to the main script path (the one where
__name__ == '__main__' is True
). So if you are at/home/billg
calling/foo/bar.py
, "current directory" refers to/foo
.