你在很多的地方都能看到关于package的定义:在Python中在当前目录下有__init__.py文件的目录即为一个package。
嗯,包括python目前的官网文档也是类似这么介绍的
https://docs.python.org/zh-cn/3.9/tutorial/modules.html#packages
Python 只把含 __init__.py 文件的目录当成包
https://docs.python.org/3.9/tutorial/modules.html#packages
The __init__.py files are required to make Python treat directories containing the file as packages
3.9如此,3.11也是的,可能是文档维护人员的工资没发?
我认为是比较符合当前的设定的
Much of the Python documentation states that an __init__.py file must be present in the package directory when creating a package. This was once true. It used to be that the very presence of __init__.py signified to Python that a package was being defined. The file could contain initialization code or even be empty, but it had to be present.
Starting with Python 3.3, Implicit Namespace Packages were introduced. These allow for the creation of a package without any __init__.py file. Of course, it can still be present if package initialization is needed. But it is no longer required.
很多的python文档说__init__.py必须要存在于package目录中,当你创建一个package的时候(注:事实上IDE如pycharm也是这么做的,默认就给你创建了..)。这曾经是对的。曾经它出现在一个python的目录中就表示了你在创建一个包。这个__init__.py文件可以包含初始化文件或者为空,但它必须存在。
从Python3.3开始,开始引入了隐式命名空间包的概念。这就允许了创建包的时候不需要__init__.py。当然如果一个包要初始化,它仍然是需要的。但是,不再是必要的!
在Specification章节
Regular packages will continue to have an __init__.py and will reside in a single directory.
Namespace packages cannot contain an __init__.py
它也提到了namespace packages和regular packages的区别
__file__
attribute.__path__
attribute is a read-only iterable of strings, which is automatically updated when the parent path is modified.__init__.py
module.__loader__
attribute这个区别实在不好测试,比想象的要繁琐很多
目录结构
D:\pythonProject\AutoTest\PackageTest (7.08KB)
├── NamespacePack (2b)
│ ├── demoNP.py (24b)
├── RegularPack (35b)
│ ├── demoRP.py (24b)
│ ├── __init__.py (2b)
└── testPack.py (114b)
区别就是在pycharm上创建一个目录NamespacePack,创建一个package(RegularPack)
demoNP.py
nameNP = 'NamespacePack'
demoRP.py
nameRP = 'RegularPack'
__init__.py为空
testPack.py
from NamespacePack.demoNP import nameNP
from RegularPack.demoRP import nameRP
print(nameNP) # 输出NamespacePack
print(nameRP) # 输出RegularPack
跟普通的包似乎没有任何区别(至少我感知不到)
__init__.py
,over在性能测试过程中总会收到一些需求如:单接口每秒并发20,这种并发持续60秒,通过负载测试查看系统稳定性,今天就让我们来浅谈一下这种场景如何去实现性能测试~