What is the order in which Python packages are introduced?

the file structure is as follows:

$ tree .
.
 main.py
 os.py
 platform.py

main.py:

import platform
import os
import sys
print("Now in main, Start`enter code here` to Exec following")
print(os.getcwd())
print(sys.modules["platform"].__file__)
print(platform.processor())

os.py:

print("Now in os.py module")

platform.py:

print("Now in platform.py module")

when I run python main.py , the result is as follows:

$ python main.py
Now in platform.py module
Now in main, Start to Exec following
/Users/michael/Code/00-Temp/pkg_import
/Users/michael/Code/00-Temp/pkg_import/platform.py
Traceback (most recent call last):
  File "main.py", line 9, in <module>
    print(platform.processor())
AttributeError: module "platform" has no attribute "processor"

question 1: os and platform are both built-in modules. When import os , they go to the built-in module by default, but import platform gets the plarform in the same directory. Why does this happen?

question 2: for the same script, I ran main.py directly in Pycharm, but reported this error:

/Users/michael/anaconda3/bin/python /Users/michael/Code/00-Temp/pkg_import/main.py
Now in os.py module
Fatal Python error: initsite: Failed to import the site module
Traceback (most recent call last):
  File "/Users/michael/anaconda3/lib/python3.7/site.py", line 570, in <module>
    main()
  File "/Users/michael/anaconda3/lib/python3.7/site.py", line 547, in main
    known_paths = removeduppaths()
  File "/Users/michael/anaconda3/lib/python3.7/site.py", line 126, in removeduppaths
    dir, dircase = makepath(dir)
  File "/Users/michael/anaconda3/lib/python3.7/site.py", line 91, in makepath
    dir = os.path.join(*paths)
AttributeError: module "os" has no attribute "path"

Process finished with exit code 1

1. Executing python will introduce os, but it is done by the interpreter. Before we use it in the program, we still need to display the import os , but the interpreter has already cached it! When the interpreter starts, it imports some standard libraries (note that they are standard libraries, not built-in libraries). Moreover, when it is cached, the default is to use built-in module , because it may default, and you are not allowed to use a module with the same name

.

2. You can modify the main.py to

import sys
print(sys.modules)

import platform
import os

print('Now in main, Start`enter code here` to Exec following')
print(os.getcwd())
print(sys.modules['platform'].__file__)
print(platform.processor())

at this time, you can see that there is already a os cache in it, so even if you go to import os below, it will not be reloaded. Unless you del sys.modules ['os]' before executing import os , built-in module will be blocked by os.py in the same directory.

3. The built-in module is not a standard library, but refers to the string meta-ancestor of sys.builtin_module_names output. The interpreter does load buit-in module when it starts, but executing print (sys.builtin_module_names) finds that os is not built-in module . So, when the interpreter executes the python file, it doesn't just load the built-in module! Use sys.modules to see all the modules loaded at actual startup.

The modules dictionary contains all loaded modules. The import statement checks this dictionary before it actually loads something from disk
sys.modules contains all loaded modules. The import statement checks the dictionary before actually loading the module from disk!

reference:


if it is a python environment, the guide package will first find your imported module from the sibling directory. If you can't find it, go back to the third-party package built into python to find the module. The complete search method is as follows: peer method <-- sibling module <-- sibling package <-- third-party package


python runtime will pre-reprint some module, you will know by using sys.modules print. If it is already loaded, there will be no more search.

Menu