【Python】Python package② (custom Python package | import and call custom Python package | `__init__.py` defines `__all__` variable)





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,

insert image description here

Enter the Python package name, and click Enter to create a Python package;
insert image description here

After the creation is complete, a my_package directory is automatically generated, and a file is automatically generated in this directory __init__.py;
insert image description here

In the command line, use tree /fthe 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>

insert image description here


2. Custom Module module code


Right-click on the module package in the root directory of PyCharm and select the "New / Python File" option,
insert image description here

Enter a filename,
insert image description here

Click Enter to create the module code in the Python package;
insert image description here
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__.pyCode


__init__.pyThe source code file can be empty, but it must be present, which is the sign of the Python package;

After __init__.pythe source code file is available, the directory will be treated as a package.

As shown in the figure below, there are __init__.pysource code files in the my_package directory, and there is a dot in the middle of the directory icon insert image description here, which is what a normal directory looks like insert image description here, such as the root directory of the PyCharm project HelloPython ;

insert image description here


__init__.pyVariables 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__.pyDefine __all__variables



__init__.pyDefine 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"]

insert image description here


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

insert image description here

Guess you like

Origin blog.csdn.net/han1202012/article/details/131485047