为什么 source/byte 代码文件在 运行 作为脚本时不作为模块导入?
Why isn’t a source/byte code file imported as a module when run as a script?
来自 import system 的 Python 文档(粗体强调我的):
5.8. Special considerations for __main__
The __main__
module is a special case relative to Python’s import system. As noted elsewhere,
the __main__
module is directly initialized at interpreter startup,
much like sys
and builtins
. However, unlike those two, it doesn’t
strictly qualify as a built-in module. This is because the manner in
which __main__
is initialized depends on the flags and other options
with which the interpreter is invoked.
5.8.1. __main__.__spec__
Depending on how __main__
is initialized, __main__.__spec__
gets set appropriately or to None
.
When Python is started with the -m
option, __spec__
is set to the
module spec of the corresponding module or package. __spec__
is also
populated when the __main__
module is loaded as part of executing a
directory, zipfile or other sys.path
entry.
In the remaining cases __main__.__spec__
is set to None
, as the code
used to populate the __main__
does not correspond directly with an
importable module:
- interactive prompt
-c
option
- running from stdin
- running directly from a source or bytecode file
Note that __main__.__spec__
is always None
in the last
case, even if the file could technically be imported directly as a
module instead. Use the -m
switch if valid module metadata is desired
in __main__
.
Note also that even when __main__
corresponds with an importable
module and __main__.__spec__
is set accordingly, they’re still
considered distinct modules. This is due to the fact that blocks
guarded by if __name__ == "__main__":
checks only execute when the
module is used to populate the __main__
namespace, and not during
normal import.
当 运行 作为脚本时,为什么 source/byte 代码文件不作为模块导入?
如文档中所述,当 运行 作为脚本 时,source/byte 代码文件不会作为模块导入(python <file path>
) 因为 运行 将代码 作为一个模块 已经是 -m
参数 (python -m <module name>
).
的目的
来自 import system 的 Python 文档(粗体强调我的):
5.8. Special considerations for
__main__
The
__main__
module is a special case relative to Python’s import system. As noted elsewhere, the__main__
module is directly initialized at interpreter startup, much likesys
andbuiltins
. However, unlike those two, it doesn’t strictly qualify as a built-in module. This is because the manner in which__main__
is initialized depends on the flags and other options with which the interpreter is invoked.5.8.1.
__main__.__spec__
Depending on how
__main__
is initialized,__main__.__spec__
gets set appropriately or toNone
.When Python is started with the
-m
option,__spec__
is set to the module spec of the corresponding module or package.__spec__
is also populated when the__main__
module is loaded as part of executing a directory, zipfile or othersys.path
entry.In the remaining cases
__main__.__spec__
is set toNone
, as the code used to populate the__main__
does not correspond directly with an importable module:
- interactive prompt
-c
option- running from stdin
- running directly from a source or bytecode file
Note that
__main__.__spec__
is alwaysNone
in the last case, even if the file could technically be imported directly as a module instead. Use the-m
switch if valid module metadata is desired in__main__
.Note also that even when
__main__
corresponds with an importable module and__main__.__spec__
is set accordingly, they’re still considered distinct modules. This is due to the fact that blocks guarded byif __name__ == "__main__":
checks only execute when the module is used to populate the__main__
namespace, and not during normal import.
当 运行 作为脚本时,为什么 source/byte 代码文件不作为模块导入?
如文档中所述,当 运行 作为脚本 时,source/byte 代码文件不会作为模块导入(python <file path>
) 因为 运行 将代码 作为一个模块 已经是 -m
参数 (python -m <module name>
).