python runs or calls another py file or parameter
1. Run another py file
(1) Run the file_B.py file in file_A.py
import os ("python file_B.py para_a1 para_a2") # Other forms ("python file_B.py %s" % para_A) ("python file_B.py " + para_A)
You need to pay attention to the way you write the path to the file, because I'm running the file in docker, and you need to add / in front of the filename, e.g. ("python /file_B.py")
(2) file_B.py uses the parameters passed by file_A.py
import sys print() # From the printout, [1:] is the parameter passed on the command line, and [0] is the name of the file the command line is running on. para_B = [1]
2. referencing a variable in another py file
from file_A import df_A
Python calls a py file you wrote yourself.
How does python call a py file that you wrote yourself?
The same directory directly write import xx on it, xx for the name of the module they want to call, although there will be an underline error, in fact, there is no error, you can still call, before have been scared by the error report!
How do you call it if it's a different directory?
Then add the directory that needs to be called, for example, call C:\xxx\
Then add the c:\xxx path (windows path and linux path symbols are different \\,/, note the difference)
Then import a on the line, write r in front of it to tell python that this is the path, don't translate \n and other special combinations.
If there are multiple files in the same c:\xxx directory, e.g., etc., you can also import them directly, as this tells python to look for package files in that folder.
import sys (r"C:\xxx") import a import b
What if the multiple files to be called are in multiple directories?
It is of course possible to write (r "C:\xxx") multiple times to add multiple directories, the
But this is cumbersome, and if the files are close together, as in this case there is no need to write the
The solution is to add an empty __init__.py file (for defining package attributes and methods, which can be empty) to indicate that this is a package, such as this, which declares aaa as a package, with a being the internal method
You can also do this by saying that bbb is a package, bb is a sub-package of bbb, and b is a method inside the bb package
Then if you need to call with, you can
import import import
If you are in the file, you want to call, only need to use the knowledge of step 2 can be, such as
import sys # Add parent directory ("..//") import import
Explanation of __init__.py
The effect is to turn the folder into a Python module, and when you import the package, you are in fact importing its __init__.py file
The __init__.py file can be empty, but it is also possible to add the following two functions
1. Initialize each module in the package and import in bulk
pack package, configure __init__.py
import a import b
Importing packages
To access the referenced files in the __init__.py file, you need to add the package name
import pack print(,)
2.__init__.py in __all__, all import the declared module
At this point importing the pack package is equivalent to importing,, the
pack package, configure __init__.py
__all__ = ['a', 'b', 'c']
call (programming)
from pack import *
Interpretation of
When importing a module, the interpreter looks for files in the order of the directories in the list.
print()
will print out a list indicating the directory of the file to look for when introducing the file, with a null character in the first element indicating the current directory
About pyc and pyo files
pyc is the bytecode file generated when py is compiled, pyc will be executed every time it is imported in the future, and pyc will be updated when the py file is updated.
If you add the -o command to the interpreter, py compiles with a pyo file, which is faster than pyc because it removes assertions, line breaks, and other debugging information.
If the -OO option is used, the generated pyo file ignores the documentation information
Internal principle during module import
The objects that import can import can be of the following types:
- Module files (.py files)
- C or C++ extensions (compiled as shared libraries or DLL files)
- Package (contains multiple modules)
- Built-in modules (written in C and linked into the Python interpreter)
- Understand that the suffixes are .py, .pyo, .pyc, .pyd, .so, .dll
Interpreter work:
1. Create a namespace based on the name of the imported file (used to access the file's internal properties and methods)
2. Execute source code in namespace
3. Create an object of the source code file, which references the corresponding namespace and manages the internal functions and variables of the module.
4. A module can be imported multiple times, but the module that is imported later only performs step 3
You can print out the mapping of imported module names to module objects
So you can understand the three ways of importing files:
-
import
: Import the global namespace, to call the c method you need to -
from a import b
: import b into the global namespace. -
from import c
: Import the attribute c of b directly into the namespace
The above is a personal experience, I hope it can give you a reference, and I hope you can support me more.