I wasn't clear how to correctly name this question.
Case 1
Assume that I have the following directory structure.
foo
|
+- bar/__init__.py
|
+- bar.py
If I have
from foo import bar
How do I know which bar (bar.py or bar/__init__.py) is being imported? Is there any easy way to automatically detect this from occurring?
Case 2
foo
|
+- foo.py
|
+- other.py
If other.py has the line
import foo
How do I know which foo (foo or foo.foo) is being imported? Again, is tehre any easy way to automatically detect this from occurring?
Import order does not matter. If a module relies on other modules, it needs to import them itself. Python treats each . py file as a self-contained unit as far as what's visible in that file.
A Python package is nothing but a collection of modules along with a __init__.py file. The modules can also be arranged in hierarchy of folders inside a package. Just by adding an empty __init__.py file to the in the folder, Python knows it is a Package.
Imports are always put at the top of the file, just after any module comments and docstrings, and before module globals and constants.
A module is a file containing Python code in run time for a user-specific code. A package also modifies the user interpreted code in such a way that it gets easily functioned in the run time.
TLDR; a package takes precedence over a module of the same name if they are in the same directory.
From the docs:
"When a module named
spamis imported, the interpreter searches for a file namedspam.pyin the current directory, and then in the list of directories specified by the environment variablePYTHONPATH. This has the same syntax as the shell variable PATH, that is, a list of directory names."
This is a bit misleading because the interpreter will also look for a package called spam (a directory called spam 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 because spam comes before spam.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.
from a python shell:
from foo import bar
print bar.__file__
should tell you which file has been imported
Rob
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With