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
spam
is imported, the interpreter searches for a file namedspam.py
in 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
.