23

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?

Katriel
  • 120,462
  • 19
  • 136
  • 170
Jeeyoung Kim
  • 5,827
  • 8
  • 44
  • 54

5 Answers5

11

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 named spam.py in the current directory, and then in the list of directories specified by the environment variable PYTHONPATH. 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.

Paulo Scardine
  • 73,447
  • 11
  • 124
  • 153
  • 1
    Have things changed? because I don't seem to get the current directory added as you suggest. The docs now state, "When a module named spam is imported, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file named spam.py in a list of directories given by the variable sys.path." And I don't see that the "current directory" is listed. But since I'm lost maybe I'm misunderstanding? – PatrickT Oct 05 '14 at 06:35
  • 1
    And (I could be misunderstanding all of this) a previous stackoverflow answer states, "Python does not add the current directory to sys.path, but rather the directory that the script is in." http://stackoverflow.com/questions/2325923/how-to-fix-importerror-no-module-named-error-in-python – PatrickT Oct 05 '14 at 06:39
  • 1
    This doesn't seem to answer **Case 1** – qff Jan 10 '17 at 21:54
5

from a python shell:

from foo import bar

print bar.__file__

should tell you which file has been imported

Rob

rtmie
  • 531
  • 1
  • 4
  • 16
3

I would like to complement the accepted answer. For Python 3.3+, namespace packages have been introduced and the import order according to PEP 420 follows:

During import processing, the import machinery will continue to iterate over each directory in the parent path as it does in Python 3.2. While looking for a module or package named "foo", for each directory in the parent path:

  • If <directory>/foo/__init__.py is found, a regular package is imported and returned.
  • If not, but <directory>/foo.{py,pyc,so,pyd} is found, a module is imported and returned. The exact list of extension varies by platform and whether the -O flag is specified. The list here is representative.
  • If not, but <directory>/foo is found and is a directory, it is recorded and the scan continues with the next directory in the parent path.
  • Otherwise the scan continues with the next directory in the parent path.

If the scan completes without returning a module or package, and at least one directory was recorded, then a namespace package is created.

alex_noname
  • 26,459
  • 5
  • 69
  • 86
1

Packages (directories with __init__.py) take precedence over modules. The documentation of this fact is difficult to find but you can see this in the source: python 2.7, python 3.6 (thanks @qff for the find).

You will also need a __init__.py within the foo directory for your example to work.

If other.py is inside of foo/ then it will load foo.py (not the directory foo/) because it will look in the current directory first (unless you've played with PYTHONPATH or sys.path).

kanaka
  • 70,845
  • 23
  • 144
  • 140
  • 1
    How do you know it takes precedence? – I couldn't find it in [the Python documentation](https://docs.python.org/2/tutorial/modules.html) – qff Jan 10 '17 at 21:56
  • @qff I tested it. I also tested python3 just now and it has the same behavior. A link to official documentation of the fact would be good. If you find one, please feel free to edit my answer or post a comment and I'll edit it. – kanaka Jan 10 '17 at 22:14
  • 1
    Found it! (kind of) – the entries of a directory are sorted before trying to load each one as either a package or module. This ensures packages are loaded first. [Link to CPython source code](https://github.com/python/cpython/blob/c30098c8c6014f3340a369a31df9c74bdbacc269/Lib/pkgutil.py#L235) – qff Jan 10 '17 at 22:58
-1

in the first case you're trying to import the function bar from file 'foo.py'

In the second you're trying to import the file 'foo.py'

oadams
  • 3,019
  • 6
  • 30
  • 53