Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Question

Post Timeline

Commonmark migration
Source Link

I encounted something strange about python import statement.

Let's say that I have a file structure like below:

foo\
 __init__.py
 bar.py
 os.py

Codes in bar.py (Other files are empty)

import os
print os.__file__

The strange thing is when I run python -m foo.bar, it prints

foo/os.pyc

However, when I changed direcotry to foo, and run python -m bar, it prints

/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.pyc

What's the difference between the two ways I run script?

In a word, what's the order Python used to import module?

From official documents, I found several text about this problem (They made me even more confused)

  1. 6.1.2. The Module Search Path

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.

  1. sys.path

the first item of this list, path[0], is the directory containing the script that was used to invoke the Python interpreter. If the script directory is not available (e.g. if the interpreter is invoked interactively or if the script is read from standard input), path[0] is the empty string, which directs Python to search modules in the current directory first.

  1. 6.4.2. Intra-package References

In fact, such references are so common that the import statement first looks in the containing package before looking in the standard module search path.

...

If the imported module is not found in the current package (the package of which the current module is a submodule), the import statement looks for a top-level module with the given name.

I encounted something strange about python import statement.

Let's say that I have a file structure like below:

foo\
 __init__.py
 bar.py
 os.py

Codes in bar.py (Other files are empty)

import os
print os.__file__

The strange thing is when I run python -m foo.bar, it prints

foo/os.pyc

However, when I changed direcotry to foo, and run python -m bar, it prints

/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.pyc

What's the difference between the two ways I run script?

In a word, what's the order Python used to import module?

From official documents, I found several text about this problem (They made me even more confused)

  1. 6.1.2. The Module Search Path

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.

  1. sys.path

the first item of this list, path[0], is the directory containing the script that was used to invoke the Python interpreter. If the script directory is not available (e.g. if the interpreter is invoked interactively or if the script is read from standard input), path[0] is the empty string, which directs Python to search modules in the current directory first.

  1. 6.4.2. Intra-package References

In fact, such references are so common that the import statement first looks in the containing package before looking in the standard module search path.

...

If the imported module is not found in the current package (the package of which the current module is a submodule), the import statement looks for a top-level module with the given name.

I encounted something strange about python import statement.

Let's say that I have a file structure like below:

foo\
 __init__.py
 bar.py
 os.py

Codes in bar.py (Other files are empty)

import os
print os.__file__

The strange thing is when I run python -m foo.bar, it prints

foo/os.pyc

However, when I changed direcotry to foo, and run python -m bar, it prints

/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.pyc

What's the difference between the two ways I run script?

In a word, what's the order Python used to import module?

From official documents, I found several text about this problem (They made me even more confused)

  1. 6.1.2. The Module Search Path

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.

  1. sys.path

the first item of this list, path[0], is the directory containing the script that was used to invoke the Python interpreter. If the script directory is not available (e.g. if the interpreter is invoked interactively or if the script is read from standard input), path[0] is the empty string, which directs Python to search modules in the current directory first.

  1. 6.4.2. Intra-package References

In fact, such references are so common that the import statement first looks in the containing package before looking in the standard module search path.

...

If the imported module is not found in the current package (the package of which the current module is a submodule), the import statement looks for a top-level module with the given name.

Source Link
YON
  • 1.7k
  • 3
  • 19
  • 35

What's the order Python used to import module?

I encounted something strange about python import statement.

Let's say that I have a file structure like below:

foo\
 __init__.py
 bar.py
 os.py

Codes in bar.py (Other files are empty)

import os
print os.__file__

The strange thing is when I run python -m foo.bar, it prints

foo/os.pyc

However, when I changed direcotry to foo, and run python -m bar, it prints

/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.pyc

What's the difference between the two ways I run script?

In a word, what's the order Python used to import module?

From official documents, I found several text about this problem (They made me even more confused)

  1. 6.1.2. The Module Search Path

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.

  1. sys.path

the first item of this list, path[0], is the directory containing the script that was used to invoke the Python interpreter. If the script directory is not available (e.g. if the interpreter is invoked interactively or if the script is read from standard input), path[0] is the empty string, which directs Python to search modules in the current directory first.

  1. 6.4.2. Intra-package References

In fact, such references are so common that the import statement first looks in the containing package before looking in the standard module search path.

...

If the imported module is not found in the current package (the package of which the current module is a submodule), the import statement looks for a top-level module with the given name.

lang-py

AltStyle によって変換されたページ (->オリジナル) /