In Python, each py file is called a module, and each directory with a __init__.py file is called a package. As long as the module or the directory in which the package resides is in Sys.path, you can use the Import module or the import package.
If you want to use code that is not in the current module, you need to use import, as we all know.
If you want to use the module (PY file) and the current module in the same directory, just import the corresponding file name, such as the use of b.py in a.py:
Import b
But what if you want to import a file of a different directory (for example, b.py)?
You first need to use the Sys.path.append method to add the directory where b.py resides in the search directory. Then import them, for example
Import Sys
Sys.path.append (' c:\xxxx\b.py ')
In most cases, the above code works very well. But if you don't see any problem with the code above, it's important to note that the code above sometimes finds no module or package (Importerror:no module named XXXXXX), because:
The SYS module is written in C, so the string supports ' \ n ', ' \ R ', ' \ t ', and so on to represent special characters. So the above code is best written as:
Sys.path.append (' c:\\xxx\\b.py ')
or Sys.path.append (' c:/xxxx/b.py ')
This avoids invalid search directory (Sys.path) settings because of the incorrect composition of the escape character.
Reason for not being able to import (Importerror:no module named * * * * * * *)