你能解释一下调用和调用的区别吗
python -m mymod1 mymod2.py args
和
python mymod1.py mymod2.py args
似乎在这两种情况下都调用了 mymod1.py
而 sys.argv
是
['mymod1.py', 'mymod2.py', 'args']
那么 -m
开关是干什么用的?
最佳答案
PEP 338 的 Rationale
部分的第一行说:
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/