Article directory
1. Custom Python package
1. Create a Python package in PyCharm
Right-click on the root directory of the Python project in PyCharm, select the "New / Python Package" option,
Enter the Python package name, and click Enter to create a Python package;
After the creation is complete, a my_package directory is automatically generated, and a file is automatically generated in this directory __init__.py
;
In the command line, use tree /f
the command to view the directory structure of the Python project:
Y:\002_WorkSpace\PycharmProjects\HelloPython>tree /f
卷 新加卷 的文件夹 PATH 列表
卷序列号为 C893-6821
Y:.
│ hello.py
│ main.py
│
├─.idea
│ │ .gitignore
│ │ HelloPython.iml
│ │ misc.xml
│ │ modules.xml
│ │ workspace.xml
│ │
│ └─inspectionProfiles
│ profiles_settings.xml
│
└─my_package
__init__.py
Y:\002_WorkSpace\PycharmProjects\HelloPython>
2. Custom Module module code
Right-click on the module package in the root directory of PyCharm and select the "New / Python File" option,
Enter a filename,
Click Enter to create the module code in the Python package;
implement a function in my_module1.py:
"""
Python 包模块代码示例
"""
def my_module1_print():
print("my_package 的 my_module1 模块运行")
Create a module my_module2.py again and implement a function in my_module2.py:
"""
Python 包模块代码示例
"""
def my_module2_print():
print("my_package 的 my_module2 模块运行")
3. __init__.py
Code
__init__.py
The source code file can be empty, but it must be present, which is the sign of the Python package;
After __init__.py
the source code file is available, the directory will be treated as a package.
As shown in the figure below, there are __init__.py
source code files in the my_package directory, and there is a dot in the middle of the directory icon , which is what a normal directory looks like , such as the root directory of the PyCharm project HelloPython ;
__init__.py
Variables can be defined in the source code __all__
, which is a list container, and the element type is a string;
2. Import and call custom Python package
1. Use import to import custom package modules
After importing, you can access the specified function through package name. module name. function name;
Code example:
"""
自定义 Python 包 代码示例
"""
import my_package.my_module1
import my_package.my_module2
my_package.my_module1.my_module1_print()
my_package.my_module2.my_module2_print()
Results of the :
Y:\002_WorkSpace\PycharmProjects\pythonProject\venv\Scripts\python.exe Y:/002_WorkSpace/PycharmProjects/HelloPython/hello.py
my_package 的 my_module1 模块运行
my_package 的 my_module2 模块运行
Process finished with exit code 0
2. Use from to import custom package modules
Code example:
"""
自定义 Python 包 代码示例
"""
from my_package import my_module1
from my_package import my_module2
my_module1.my_module1_print()
my_module2.my_module2_print()
Results of the :
Y:\002_WorkSpace\PycharmProjects\pythonProject\venv\Scripts\python.exe Y:/002_WorkSpace/PycharmProjects/HelloPython/hello.py
my_package 的 my_module1 模块运行
my_package 的 my_module2 模块运行
Process finished with exit code 0
3. Use from to import the functions in the custom package module
Code example:
"""
自定义 Python 包 代码示例
"""
from my_package.my_module1 import my_module1_print
from my_package.my_module2 import my_module2_print
my_module1_print()
my_module2_print()
Results of the :
Y:\002_WorkSpace\PycharmProjects\pythonProject\venv\Scripts\python.exe Y:/002_WorkSpace/PycharmProjects/HelloPython/hello.py
my_package 的 my_module1 模块运行
my_package 的 my_module2 模块运行
Process finished with exit code 0
3. __init__.py
Define __all__
variables
__init__.py
Define the variable in , __all__
which means that in this package, the my_module1 module is valid, and other modules are not valid;
__all__ = ["my_module1"]
use
from my_package import *
code, import the entire package,
In the following code, only the content in the my_module1 module is available, and the my_module2 module cannot be accessed;
"""
自定义 Python 包 代码示例
"""
from my_package import *
my_module1.my_module1_print()
my_module2.my_module2_print()
Results of the :
Y:\002_WorkSpace\PycharmProjects\pythonProject\venv\Scripts\python.exe Y:/002_WorkSpace/PycharmProjects/HelloPython/hello.py
my_package 的 my_module1 模块运行
Traceback (most recent call last):
File "Y:\002_WorkSpace\PycharmProjects\HelloPython\hello.py", line 8, in <module>
my_module2.my_module2_print()
NameError: name 'my_module2' is not defined. Did you mean: 'my_module1'?
Process finished with exit code 1