python - -m 开关的目的是什么?

标签 python command-line module package

你能解释一下调用和调用的区别吗

python -m mymod1 mymod2.py args

python mymod1.py mymod2.py args

似乎在这两种情况下都调用了 mymod1.pysys.argv

['mymod1.py', 'mymod2.py', 'args']

那么 -m 开关是干什么用的?

最佳答案

PEP 338Rationale 部分的第一行说:

Python 2.4 adds the command line switch -m to allow modules to be located using the Python module namespace for execution as scripts. The motivating examples were standard library modules such as pdb and profile, and the Python 2.4 implementation is fine for this limited purpose.

因此,您可以通过这种方式在 Python 的搜索路径中指定任何模块,而不仅仅是当前目录中的文件。你是正确的 python mymod1.py mymod2.py args 具有完全相同的效果。 此提案的范围部分的第一行说明:

In Python 2.4, a module located using -m is executed just as if its filename had been provided on the command line.

使用 -m 更多是可能的,例如使用作为包的一部分的模块等。这就是 PEP 338 的其余部分的内容。阅读它以获取更多信息。

关于python - -m 开关的目的是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7610001/

相关文章:

r - 如何在 Shiny 的模块中使用 downloadButton 和 downloadHandler

function - 为什么 Erlang 元组模块有争议?

python - 我想在我的网站上有一个按钮来执行 python 脚本

python - 用Python制作圆柱体?

unix - 从 Unix 命令行查找标点符号并计算每个标点符号的数量

regex - 将grep正则表达式多行搜索的匹配结果限制为1

Angular 2模块通信

python - 在python中使用OpenCV分隔图像中的线条和圆圈

python - 取 nlargest 5 并对 pandas 中的其余部分求和/计数

java - 当我运行 .jar 时,出现 "No lwjgl in java.library.path"错误