import详解
D:\Python39\Lib\site-packages>md zz
D:\>python
>>>import zz
# 目录zz可以当module import,__init__.py不是必需的。
D:\Python39\Lib\site-packages\zz>type m.py
def p(): print('module')
D:\>python
>>>import zz
>>>zz.p
AttributeError: module 'zz' has no attribute 'p'
>>> zz.m.p
AttributeError: module 'zz' has no attribute 'm'
>>> import zz.m
>>> zz.m.p()
module
D:\>python
>>> from zz import *
>>> p()
NameError: name 'p' is not defined
# 没有__init__.py是不方便的。
D:\Python39\Lib\site-packages\zz>type __init__.py
print('init')
__all__ = ['m']
D:\>python
>>> from zz import *
init
>>> m.p()
module
Packages are a way of structuring Python’s module namespace by using “dotted module names”. For example, the module name A.B designates a submodule named B in a package named A.
__init__.py can just be an empty file, but it can also execute initialization code for the package or set the __all__ variable.
If a package’s __init__.py code defines a list named __all__, it is taken to be the list of module names that should be imported when from package import * is encountered. It is up to the package author to keep this list up-to-date when a new version of the package is released. Package authors may also decide not to support it, if they don’t see a use for importing * from their package. 此时你可以到site-packages目录下去自己添加一个,或者把他的.py copy当前目录下——如果文件不多的话。
When a module named spam is imported, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file named spam.py in a list of directories given by the variable sys.path. sys.path is initialized from these locations:
- The directory containing the input script (or the current directory when no file is specified).
- PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).
- The installation-dependent default.
On file systems which support symlinks, the directory containing the input script is calculated after the symlink is followed. In other words the directory containing the symlink is not added to the module search path.
After initialization, Python programs can modify sys.path. The directory containing the script being run is placed at the beginning of the search path, ahead of the standard library path. This means that scripts in that directory will be loaded instead of modules of the same name in the library directory.
装了python后,D:\Python39\Doc\python394.chm 8,653KB. 它读起来比较晦涩,但详尽程度超过绝大多数其它python书。
比如search(import):
5. The import system
Python code in one module gains access to the code in another module by the process of importing it. The import statement is the most common way of invoking the import machinery, but it is not the only way. Functions such as importlib.import_module() and built-in __import__() can also be used to invoke the import machinery......
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?