In the past, when using import, there were often some problems due to the import of modules, as well as some half confused and half confused problems. I simply spent some time studying some python reference methods and experimented with them. I have deep feelings. I leave this article for a summary. If there are any mistakes, please comment and correct them
This article will try my best to describe in detail. There will be a large number of words. I hope you will read it patiently.
First of all, I think we should first understand how python references are referenced
Let's first create a python file demo py
print(dir())
The dir() command obtains all the attributes of an object. When the value is empty, the current py file is passed in by default. We can use this function to view all the attributes of the PY file
['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__']
Variables beginning and ending with double underscores are a special kind of variables in python, which is called python's magic function. Here is a brief explanation of the meaning of some common attributes
- __ annotations__: Predefined variable types
- __ doc__: Document comments, including only the first internal comment document
- __ file__: File name, which returns absolute path or relative path according to whether it is referenced or executed
- __ name_: File name, executable file. The attribute is__ main__, The package name is returned for the referenced file
When we write some code in the python file
a = 1 class b: def __init__(self) -> None: self.value = "123" c = [1,2,3] def f(a,b): return a+b print(dir())
After executing again, you can find
['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'a', 'b', 'c', 'f']
Compared with before, all variables, classes and functions are added to the attribute value of py file, which is why these declared variables or functions can be used later.
For the following common library file import methods
import math import torch.nn as nn from numpy import arange print(dir())
Similarly, we can clearly see that when a python library file is introduced, the name of the library is actually added to the python file. If it is renamed with the keyword "as", the renamed name will be retained in the file
['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'math','nn','arange']
At this time, I suddenly had a question. Where are the print and dir functions defined? I seem to use them directly without referring to any library.
This is actually a very interesting question, but let's wait a minute and I'll answer it later.
Let's take another practical scenario. Usually, a more complex project works with multiple files. Many of them use multi-level directories to reasonably divide file relationships and sort out the overall code architecture for the purposes of unified namespace consistency and clear and orderly overall file structure.
We introduce the following file system environment (this diagram will be repeated in the appropriate place in the text to enhance memory)
|- python | |- demo.py | |- folder1 | |- a.py | |- b.py | |- folder2 | |- c.py | |- d.py
In addition, f1-f4 functions are defined in each a/b/c/d.py file for calling, as shown below:
#a.py def f1(): print("this is function f1 in a.py")
Then we are in demo The following code is executed in py. Obviously, there is no problem with the correct introduction
#demo.py from folder1.a import f1 from folder1.b import f2 from folder2.c import f3 from folder2.d import f4 f1() f2() f3() f4() # Output: # this is function f1 in a.py # this is function f2 in b.py # this is function f3 in c.py # this is function f4 in d.py
If I want to use f2 in b.py in a.py, I can also change and execute it without any problem
from b import f2 def f1(): print("this is function f1 in a.py") f2() # Output: # this is function f2 in b.py
But if I want to use f3 in c.py in a.py, I obviously need some other means, because it involves cross folder references
|- python | |- demo.py | |- folder1 | |- a.py | |- b.py | |- folder2 | |- c.py | |- d.py
Considering the hierarchy of the file structure, a.py is located in the directory folder1. We hope that a.py can return to the upper directory python, so that it can be referenced smoothly in folder2/c.py.
Many files do the same, adding an import sys, sys path,sys. path. append(".") Then the problem seemed to be solved smoothly,
import sys sys.path.append(".") from folder2.c import f3 def f1(): print("this is function f1 in a.py") f3() # Output: # this is function f3 in c.py
However, why is this approach feasible? We might as well explore the logic behind the correct implementation of this approach
First, let's take a look at sys What's the role of path? We're in demo PY
import sys print(sys.path)
The python environment I use here is a python 3 created by Anaconda 3 7 virtual environment with the name fastreid
['g:\\learner_lu\\code-grammar\\python', 'C:\\ProgramData\\Anaconda3\\envs\\fastreid\\python37.zip', 'C:\\ProgramData\\Anaconda3\\envs\\fastreid\\DLLs', 'C:\\ProgramData\\Anaconda3\\envs\\fastreid\\lib', 'C:\\ProgramData\\Anaconda3\\envs\\fastreid', 'C:\\Users\\Administrator\\AppData\\Roaming\\Python\\Python37\\site-packages', 'C:\\ProgramData\\Anaconda3\\envs\\fastreid\\lib\\site-packages']
We can observe sys Path contains many absolute paths. The first path seems to be demo The folder where py is located, other paths, and small partners who have configured environment variables must feel familiar.
If we choose to execute a.py, we will get the following results:
['g:\\learner_lu\\code-grammar\\python\\folder1', 'C:\\ProgramData\\Anaconda3\\envs\\fastreid\\python37.zip', 'C:\\ProgramData\\Anaconda3\\envs\\fastreid\\DLLs', 'C:\\ProgramData\\Anaconda3\\envs\\fastreid\\lib', 'C:\\ProgramData\\Anaconda3\\envs\\fastreid', 'C:\\Users\\Administrator\\AppData\\Roaming\\Python\\Python37\\site-packages', 'C:\\ProgramData\\Anaconda3\\envs\\fastreid\\lib\\site-packages']
Our first conjecture, the only difference, is confirmed The first value in path is the absolute path in the operating system of the folder where the py file is executed.
So now the question is, what are the other paths?
- C:\ProgramData\Anaconda3\envs\fastreid\python37.zip is a compressed package of Python. It will be deleted after decompression. The path is invalid
- C:\ProgramData\Anaconda3\envs\fastreid\DLLs are all pyd format file is an encrypted format of D language. This format can be referenced but cannot be viewed as source code.
- C:\ProgramData\Anaconda3\envs\fastreid\lib are some libraries that come with python. You can see some common copies when installing with python py,glob. py,io. py,os. py
- C:\ProgramData\Anaconda3\envs\fastreid is the python interpreter The directory where exe is located is also the file that needs to be started to interpret statements line by line when the whole py file is executed
- The remaining two site packages are the installation location of the package when pip install / conda install is used. I believe that the operation of downloading third-party libraries is not unfamiliar to the partners using python
OK, back to the question just now, sys Path is the absolute path of the folder where the py file is executed in the operating system and the directory of all libraries in the python environment
|- python | |- demo.py | |- folder1 | |- a.py | |- b.py | |- folder2 | |- c.py | |- d.py
This has solved some of my doubts
Why can I import b in a.py? It turned out to be g: \ \ learner_ This file can be found in the Lu \ \ code grammar \ \ Python \ \ folder1 directory
Why can I import os? It turns out that there is OS in the C:\ProgramData\Anaconda3\envs\fastreid\lib directory py
Why can I import numpy? It was put under XXX \ \ site packages
So we can summarize, as long as it is in sys The path in the import file can be found directly.
Then the previous question is well explained. Sys. Of a.py C.py cannot be found under path, so we need to find it in sys There are two ways to add a path that can find c.py in path
#method-1 import sys sys.path.append('g:\\learner_lu\\code-grammar\\python\\folder2') import c c.f3() # Output: # this is function f3 in c.py #method-2 import sys sys.path.append('g:\\learner_lu\\code-grammar\\python') import folder2.c folder2.c.f3() # Output: # this is function f3 in c.py
Although it can be executed, this method is obviously troublesome, because you also need to enter the absolute path of the directory, which is obviously not an easy way.
So, the sys.exe used at the beginning of the introduction path. append(".") What does that mean?
|- python | |- demo.py | |- folder1 | |- a.py | |- b.py | |- folder2 | |- c.py | |- d.py
The current directory is where you are. For example, I am now in G: \ learner_ Lu \ code grammar \ python, I want to execute a.py, which can be accessed through relative path, python folder1/a.py, and sys path. append(".") Is to join the current G: \ learner_ Lu \ code grammar \ Python path, and sys path. Append ("G: \ learner_lu \ code grammar \ Python") is exactly the same.
But if I go further now, I'm in G: \ learner_ Lu \ code grammar \ Python \ folder1, if I want to run a.py directly, I can directly run python a.py. At this time, sys path. append(".") Equivalent to sys path. append("G:\learner_lu\code-grammar\python\folder1")
That is, sys path. append(".") It's actually in sys Add the absolute path of the current directory to path, which will change as you cd enter or exit to other directories.
This is indeed a solution, which eliminates the cumbersome input of absolute path and adds the current directory (usually the outermost root directory of the whole project), so that any file location can be directly introduced into the file. This approach has also been applied in many projects.
#Current directory: G:\learner_lu\code-grammar\python import sys sys.path.append('.') from folder2.c import f3 f3() # Output: # this is function f3 in c.py
When using this method, you must pay attention to your directory, preferably the root directory, so that all files can be referenced. If you are not there, you can also use sys path. Append ('..') and other methods to get a larger reference range
Another usage of reference is also very common, that is, relative reference between files Other files and functions refer to each other, such as the previous file structure
|- python | |- demo.py | |- folder1 | |- a.py | |- b.py | |- folder2 | |- c.py | |- d.py
I rewritten the f1 function of a.py, which calls f2 from b.py and f3 in c.py, and then I want to Py uses f1 function, which is common. What should we do?
First, I rewrite a.py and import F2 and F3 by relative reference
from .b import f2 from ..folder2.c import f3 def f1(): print("start f1") f2() f3() print("end f1")
Second, I rewrite the demo py
from folder1.a import f1 f1()
It seems reasonable, but run demo Py, but it reported an error.
Traceback (most recent call last): File "g:\learner_lu\code-grammar\python\demo.py", line 3, in <module> from folder1.a import f1 File "g:\learner_lu\code-grammar\python\folder1\a.py", line 4, in <module> from ..folder2.c import f3 ValueError: attempted relative import beyond top-level package
The reason for error reporting is "relative quotation outside the top package", which is vague and doesn't quite understand what it means.
Let's run a.py first to see if there is a problem with a.py?
from .b import f2 from ..folder2.c import f3 def f1(): print("start f1") f2() f3() print("end f1") f2() f3()
At this time, something more strange happened. The wrong position was even reported in advance. For the first time, at least from B import F2 did not report an error, but now it also reports an error?
Traceback (most recent call last): File "g:\learner_lu\code-grammar\python\folder1\a.py", line 3, in <module> from .b import f2 ImportError: attempted relative import with no known parent package
The reason for the error is "try to import relatively without knowing the parent package". It is clear that there is no problem when I use from b import f2 to import. a.py and b.py are the same directory. I import and add one with the same directory What's the problem? What do these errors mean?
At this time, we might as well stop and analyze how python imports modules with relative references.
Through the dir() function at the beginning, we can see that each py file has one__ name__ attribute
- When the file is executed, the value of this attribute is "_main_"
- When it is referenced as a module, the value of its attribute is the relative path from the currently executed file to it
|- python | |- demo.py | |- folder1 | |- a.py | |- b.py | |- folder2 | |- c.py | |- d.py
Still the original file directory, we output c.py in different cases__ name__ attribute
#demo.py from folder2.c import f3 ------------------------------------------- #d.py from c import f3 ------------------------------------------- #c.py print(__name__) def f3(): print("this is function f3 in c.py") # Execute demo py # Output: # folder2.c # Execute d.py # Output: # c # Execute c.py # Output: # __main__
The principle of relative reference is here, according to the module__ name__ Values and used Wait for relative paths to search for files.
So back to the question just now, why do a.py and b.py use from under the same directory The relative reference of B import F2 failed?
There is no problem with directory reference B also has no problem. The problem is that when you execute a.py, its package name, that is__ name__ The value of is "_main_", Other files cannot be found through "_main_". That is to say, if a.py does not run directly, but only makes relative reference with b.py in the form of module, there is no problem, for example:
#a.py from .b import f2 def f1(): print("this is function f1 in a.py") ------------------------------------------- #b.py def f2(): print("this is function f2 in b.py") ------------------------------------------- #demo.py from folder1.a import f1,f2 f1() f2() # Execute demo py # Output: # this is function f1 in a.py # this is function f2 in b.py
Because a.py has not been executed now, its__ name__ Property is the demo that is executed The relative path from py to a.py, that is, folder1 a. And we execute demo Py, first add sys In the path is demo The folder where py is located, that is, G: \ learner_ Lu \ code grammar \ python, which uses B makes a relative reference, that is, first find a folder of folder1, and then try to find a b.py file in it. You can find the file, so execute it correctly
What if I just want to reference b.py and execute a.py through relative reference? Then, according to the principle, we first change the "_name_" Property. Secondly, we need to be able to find the folder folder1, because a.py is executed in sys G: \ learner is added to path_ Lu \ code grammar \ Python \ folder1 it can't find its own, The method is as follows
#a.py __name__ = "folder1.a" import sys # The current directory is / python sys.path.append(".") from .b import f2 def f1(): print("this is function f1 in a.py") f2()
There is no problem with correct implementation. One more word, among which__ name__ The value of does not have to be "folder1.a", because it takes the peer directory of a anyway, so it doesn't matter what the package name of a is. It can be changed to "folder1.asd","folder1.pql", etc., but there can only be one, Multiple directories will be considered as multi-layer directory structures, and the search location is wrong.
However, this way of directly rewriting name is just like a picture of music. There is no need to add details if you can directly quote it
So go back to the second error message just now and use from B import F2 reports an error, "try to import relatively without known parent package":
Because you are running a.py directly, its package name__ name__ Value is__ main__, How can you find the location of the relative referenced file through it? And we run demo The reason why this reference does not report an error in py is that a.py__ name__ The value is folder1 a. You can find folder by relative reference b.
Then there is another problem just now. I run demo Py, a.py/b.py/c.py adopt mutual reference through package name. What's the problem? Looks like their__ name__ There seems to be no problem?
from .b import f2 from ..folder2.c import f3 def f1(): print("start f1") f2() f3() print("end f1")
demo.py directly refers to a. the problem is that from folder2. c import f3
|- python | |- demo.py | |- folder1 | |- a.py | |- b.py | |- folder2 | |- c.py | |- d.py
Run the demo The package name of a.py is folder1 a. You tried to pass To enter its parent directory. The parent directory of folder1 is python, and execute demo Py, first add sys Path is g: \ learner_ Lu \ code grammar \ Python directory cannot find the same level directory Python through it. A higher level directory reference is required.
Therefore, the error message: "make relative reference outside the top-level package", that is, the package name of a.py is not enough. I searched its parent directory, folder1 A just enough for me to perform, If you want to execute Then your package name should at least be python folder1.a or longer
There are two solutions:
1. Improve demo The directory level of Py is at the same level as that of python, that is, the file system is changed to
|- demo.py |- python | |- folder1 | |- a.py | |- b.py | |- folder2 | |- c.py | |- d.py
In demo From python. Is used in Python folder1.a import F1, improve demo The PY position is equivalent to expanding the name of a package from the original folder1 A is now python folder1.a. Can use To jump to the python directory and search for folder2
2. The original directory remains unchanged, and the indirect reference of a.py to c.py is changed to direct reference
#a.py from .b import f2 from folder2.c import f3 def f1(): print("this is function f1 in a.py") f2() f3() print("end of f1")
demo.py unchanged
from folder1.a import f1 f1() # Output: # this is function f1 in a.py # this is function f2 in b.py # this is function f3 in c.py # end of f1
When I first learned python, I was told that the reference of Python can be used directly between multi-level directories It's OK. This is the syntax of Python. I don't think about the principle behind it.
In fact, we use To connect directories and directories, directories and files, and finally write import python folder1. A is actually the package name of this file. Python introduces the module by looking up the package name.
Now you can answer the question raised at the beginning. Where is the print function referenced? It seems that I used it directly without introducing any modules.
Some functions in python are called built-in functions. Observe the attributes of the py file mentioned at the beginning. One attribute is called '__ builtins__', This attribute contains all built-in functions. We can use print (dir (_builtins_)) Further view
['ArithmeticError', 'AssertionError', 'AttributeError'......., abs,any,chr,help,set,round,sum,tuple,list,zip,min,max,str.... print,next,object,pow,quit,dir,.....]
There are many built-in functions that we are familiar with or unfamiliar with. These functions do not need to be imported like other py files. python is implemented in c language, and so do these built-in functions. You can be here( https://hg.python.org/cpython/file/937fa81500e2/Python/bltinmodule.c )Find the code implementation of all built-in functions, in [here]( https://hg.python.org/cpython/file/937fa81500e2/Python/bltinmodule.c#l1567 )Find the source code implementation of the print function, which is completed in c language. The header files they reference can also be found in C:\ProgramData\Anaconda3\envs\fastreid\include, such as code h,eval. h. However, there is no corresponding c source file, which has been compiled and linked to python Exe file. See the end of the article for the relationship between cpython and python.
When we use a function, python will first find out whether it is introduced into the py file. If it is not found, it will find it in the built-in function. If it is not found again, an error will be reported.
How to find the module reference and what is the priority?
For example, we can define a print function, which will override the built-in function print to be called
def print(x): return print("123") #No output
Now let's create a copy.py in the same directory of c.py Py file. When we refer to import copy, we refer to the copy defined by me Py is also a copy under 'C:\ProgramData\Anaconda3\envs\fastreid\lib' in the python environment Where's py?
The current directory structure is as follows:
|- python | |- demo.py | |- folder1 | |- a.py | |- b.py | |- folder2 | |- c.py | |- copy.py | |- d.py
I'm in copy Py defines a function deepcopy, which only outputs one sentence
#copy.py def deepcopy(x): print(f"this is just a deepcopy function of {x} in copy.py")
The copy in the original 'C:\ProgramData\Anaconda3\envs\fastreid\lib' The function of deepcopy with the same name in py is to make a deep copy of variables. A new copy of variables has been made. They have different addresses and the same value.
Now I import copy in c.py, which will it refer to?
#c.py import copy copy.deepcopy("123") # Output: # this is just a deepcopy function of 123 in copy.py
You can see that copy. In the same directory is referenced Py, not in the python environment.
It seems that the documents cited under this directory take precedence.
At this time, I have a question again. If I copy as before Add the directory of Py to sys In path, is it the same if it is not referenced in the same directory?
|- python | |- demo.py | |- folder1 | |- a.py | |- b.py | |- folder2 | |- c.py | |- copy.py | |- d.py
I'm in demo Py, add the directory path of folder2 # to sys Path, directly using import copy reference
#demo.py #Current directory / python import sys sys.path.append("./folder2") import copy copy.deepcopy("123") # No output
It's strange that there is no output, which indicates that it calls copy in python environment Py made a deep copy of "123".
Oh, I found out. I use sys path. Append, add at the end of the list, that is, the current sys Path is like this
['g:\\learner_lu\\code-grammar\\python', 'C:\\ProgramData\\Anaconda3\\envs\\fastreid\\python37.zip', 'C:\\ProgramData\\Anaconda3\\envs\\fastreid\\DLLs', 'C:\\ProgramData\\Anaconda3\\envs\\fastreid\\lib', 'C:\\ProgramData\\Anaconda3\\envs\\fastreid', 'C:\\Users\\Administrator\\AppData\\Roaming\\Python\\Python37\\site-packages', 'C:\\ProgramData\\Anaconda3\\envs\\fastreid\\lib\\site-packages', './folder2']
What if I insert it at the head of the list? That is to put sys Path becomes
['./folder2', 'g:\\learner_lu\\code-grammar\\python', 'C:\\ProgramData\\Anaconda3\\envs\\fastreid\\python37.zip', 'C:\\ProgramData\\Anaconda3\\envs\\fastreid\\DLLs', 'C:\\ProgramData\\Anaconda3\\envs\\fastreid\\lib', 'C:\\ProgramData\\Anaconda3\\envs\\fastreid', 'C:\\Users\\Administrator\\AppData\\Roaming\\Python\\Python37\\site-packages', 'C:\\ProgramData\\Anaconda3\\envs\\fastreid\\lib\\site-packages']
#demo.py #Current directory / python import sys sys.path.insert(0,"./folder2") import copy copy.deepcopy("123") # output # this is just a deepcopy function of 123 in copy.py
It also refers to the copy Py!
It seems that we are closer to the truth. It is according to sys Path. If it is found, it will not continue to search, that is, it will return the first found result
Now let's test the conjecture. Let's try to reference a math one by one
|- python | |- demo.py | |- folder1 | |- a.py | |- b.py | |- folder2 | |- c.py | |- copy.py | |- d.py | |- math.py
New file math Py, define the variable pi="123", and then reference it in c.py!
#math.py pi = "123" ------------------------------- #c.py from math import pi print(pi) # Output: # 3.141592653589793
Eh? Why is it still the original value? Shouldn't I be referenced preferentially in the same directory? Why copy Py OK, are you sick again?
After careful inspection, we found that math is a built-in library, which is similar to the built-in function and is also written in c language.
import sys print(sys.builtin_module_names) # Output: ('_abc', '_ast', '_bisect', '_blake2', '_codecs',.... 'math', 'mmap', 'msvcrt', 'nt', 'parser', 'sys', 'time'...)
The sys,time,math and other modules we use are built-in libraries. They need to be explicitly referenced through import xxx, and their reference priority is higher than all other modules, that is, sys The file in path can only be referenced if it has a different name from the built-in library file, otherwise it must refer to the built-in library module
Now the order of reference is ready to come out
- First check whether it is a built-in library, that is, in sys builtin_ module_ Search in names and return the first found result
- The second order is sys Search in path, the top priority is found, and the first found result is returned
This requires attention to the file naming specification and function scope. Except for function overloading, try to avoid using the same name, otherwise the later ones will overwrite the first ones, for example:
#a.py def f(): print("this is function f in a.py") ---------------------------------------- #b.py def f(): print("this is function f in b.py") ---------------------------------------- #demo.py from folder1.a import f from folder1.b import f f() # Output: # this is function f in b.py
It also warns us that we must pay attention to the naming norms of files, functions and variables when writing our own code. It is not easy to detect if we misquote, misquote, overload and overwrite.
We will find that in demo It seems too troublesome to introduce one by one in py. If demo2, demo3 and demo4 need to be introduced in large sections, even copy and paste is obviously not what we want. Can there be a more convenient way to introduce modules?
We can create a new one under folder1__ init__.py file, the directory structure is as follows:
|- python | |- demo.py | |- folder1 | |- __init__.py | |- a.py | |- b.py | |- folder2 | |- c.py | |- d.py
Explicit definitions are no longer required in Python 3 today__ init__.py file to use a folder as a package, but we can add some things to facilitate our use. Add the following code
#__init__.py from .a import f1 from .b import f2
So we can directly in the demo PY
from folder1 import * f1() f2() # output # this is function f1 in a.py # this is function f2 in b.py
Here__ init__. The function of Py is to preferentially execute this initialization file when referencing folder1 # and take folder1 as a package to collect all references under it, so that all definitions can be referenced only by from folder1 import *.
In addition, it can be encountered in some files__ all__ This variable is actually called when we use from xxx import *__ all__ This variable contains all sub files by default, and the attribute is a list composed of strings. Of course, we can explicitly define this variable to achieve some effects, such as:
|- python | |- demo.py | |- folder1 | |- __init__.py | |- a.py | |- b.py
-
We add several functions under a.py
#a.py def f1(): print("this is function f1 in a.py") def fx(): print("this is function fx in a.py") def fy(): print("this is function fy in a.py")
Use dir() to view the demo Variables introduced in PY
#__init__.py from .a import * from .b import * #demo.py from folder1 import * print(dir()) # output # ['__annotations__', '__builtins__', '__cached__', '__doc__', # '__file__', '__loader__', '__name__', '__package__', '__spec__', # 'a', 'b', 'f1', 'f2', 'fx', 'fy']
Can see__ init__.py calls a and B, and then continues to import * call all functions in a.py and b.py, and all functions can be used
But if we add in a.py__ all__ Limitation of
#a.py __all__ = ["f1","fy"] def f1(): print("this is function f1 in a.py") def fx(): print("this is function fx in a.py") def fy(): print("this is function fy in a.py") ------------------------------------------- #demo.py from folder1 import * print(dir()) # output # ['__annotations__', '__builtins__', '__cached__', '__doc__', # '__file__', '__loader__', '__name__', '__package__', '__spec__', # 'a', 'b', 'f1', 'f2', 'fy']
fx will be discarded and will not be referenced. This method is usually used to eliminate some private base classes and functions in the current file and do not want to be referenced.
Similarly, we can also__ init__.py
from .a import * from .b import * __all__ = ["fx","b"] #demo.py from folder1 import * print(dir()) # output # ['__annotations__', '__builtins__', '__cached__', '__doc__', # '__file__', '__loader__', '__name__', '__package__', '__spec__', # 'b', 'fx']
So in demo Only constraints can be used in py__ all__ Functions or variables in can manually distinguish namespaces to prevent pollution
It should be noted that it is only involved when using the reference method of from xxx import *__ all__, Setting this variable for other reference methods has no effect on references.
This is the end of this detailed introduction to Python import. Finally, I wish you progress every day!! The most important thing to learn Python is mentality. We are bound to encounter many problems in the process of learning. We may not be able to solve them if we want to break our head. This is normal. Don't rush to deny yourself and doubt yourself. If you have difficulties in learning at the beginning and want to find a python learning and communication environment, you can join us to receive learning materials and discuss together.