Python calls py files or parameters in other folders across folders

Table of contents

Summary:

Chapter 1 Running another py file

(1) Run the file_B.py file in file_A.py, note that this is running, not a reference

(2) file_B.py uses the parameters passed by file_A.py

(3) Refer to variables or methods in another py file

Chapter 2 python calls the py file written by itself

(1) Files in the same directory

(2) Files in different directories

(3) Calling of multiple files in multiple directories

Chapter 3 Explanation of __init__.py 

(1) Initialize each module in the package and import in batches

(2) __all__ in __init__.py, all import the declared modules

Chapter 4 About pyc and pyo files

Chapter 5 Internal Principles When Modules Are Imported

(1) Importable objects can be of the following types:

(2) The interpreter works:

(3) Therefore, three ways to import files can be understood:


Summary:

        This article mainly introduces how python runs or calls another py file or parameter, which has a good reference value and hopes to be helpful to everyone. If there are mistakes or incomplete considerations, please point them out.

Chapter 1 Running another py file

(1) Run the file_B.py file in file_A.py, note that this is running, not a reference

import os
os.system("python file_B.py para_a1 para_a2")
#其他形式
os.system("python file_B.py %s" % para_A)
os.system("python file_B.py " + para_A)

Pay attention to the writing of the file path. To run the file in docker, you need to add / in front of the file name, such as os.system("python /file_B.py")

(2) file_B.py uses the parameters passed by file_A.py

import sys
print(sys.argv)
#由打印的结果可知,sys.argv[1:]是命令行传递的参数,sys.argv[0]是命令行运行的文件名
para_B = sys.argv[1]

(3) Refer to variables or methods in another py file

from file_A import df_A

Chapter 2 python calls the py file written by itself

(1) Files in the same directory

        Just write import xx directly in the same directory. xx is the name of the module you want to call. Although there will be an underscore to report an error, in fact, there is nothing wrong, and you can still call it. The underscore can be ignored.

(2) Files in different directories

        Files under different paths must first call the add path sys.path.append(r"C:\xxx"), and then make a reference

import sys
#首先,添加路径,windows的路径和linux的路径符号不同\\,/,注意区别
#r是为了告诉python这是路径,别#把\n等特殊组合给转译)
sys.path.append(r"C:\xxx")
#下面import就可以了
import a
import b

(3) Calling of multiple files in multiple directories

        First of all, we can also write sys.path.append(r"C:\xxx") multiple times to add multiple directories, but this is a bit troublesome. Every time we create a new project, we may need to add a lot of code to call the common unit.

        The solution is to add an empty __init__.py file (used to define the properties and methods of the package, which can be empty), indicating that this is a package. For example, aaa is declared as a package, and a is an internal method.

It can also be like this, indicating that bbb is a package, bb is a sub-package of bbb, and b is the internal method of bb package

  If you need to call a.py and b.py:

import aaa.a
import bbb.bb.b
import ccc.c

If you are in the c.py file and want to call a.py, b.py only needs to use the knowledge of step 2, such as 

import sys
#添加上级目录
sys.path.append("..//")
import aaa.a
import bbb.bb.b

Chapter 3 Explanation of __init__.py 

The role of __init__.py is to turn the folder into a Python module. When importing a package, it actually imports its __init__.py file

The __init__.py file can be empty, but you can also add the following two functions

(1) Initialize each module in the package and import in batches

pack package, configure __init__.py

1

2

import a

import b

import pack package

To access the referenced files in the __init__.py file, you need to add the package name

(2) __all__ in __init__.py, all import the declared modules

At this time, importing the pack package is equivalent to importing a.py, b.py, c.py

pack package, configure __init__.py

1

__all__ = ['a', 'b', 'c']

transfer

1

from pack import *

Chapter 4 About pyc and pyo files

pyc is the bytecode file generated when py is compiled, and pyc will be executed every time it is imported, and pyc will also be updated when the py file is updated

If the interpreter adds the -o command, the pyo file will be generated when py is compiled, which removes the assertion (assert), line break number and other debugging information compared to pyc, and runs faster

If the -OO option is used, the generated pyo file will ignore the document information

Chapter 5 Internal Principles When Modules Are Imported

(1) Importable objects 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)
  • Builtin modules (written in C and linked into the Python interpreter)
  • Understand that the suffix is ​​.py, .pyo, .pyc, .pyd, .so, .dll

(2) The interpreter works:

1. Create a namespace based on the imported file name (used to access the internal properties and methods of the file)

2. Execute source code in the namespace

3. Create an object of a source code file, which refers to the corresponding namespace, and manages the internal functions and variables of the module

4. A module can be imported multiple times, but the modules imported later only perform the third step

sys.modules can print out the mapping between imported module names and module objects

(3) Therefore, three ways to import files can be understood:

  • import a.b: Import ab into the global namespace, and you need abc to call the c method
  • from a import b: Import b into the global namespace, bc
  • from a.b import c: Import the attribute c of b directly into the namespace

 

Guess you like

Origin blog.csdn.net/fyq158797/article/details/130226881