-m 开关的目的是什么? [英] What is the purpose of the -m switch?

查看:24
本文介绍了-m 开关的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能向我解释一下调用之间的区别吗

Could you explain to me what the difference is between calling

python -m mymod1 mymod2.py args

python mymod1.py mymod2.py args

似乎在这两种情况下 mymod1.py 都被调用,而 sys.argv

It seems in both cases mymod1.py is called and sys.argv is

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

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

So what is the -m switch for?

推荐答案

PEP 338 说:

The first line of the Rationale section of PEP 338 says:

Python 2.4 添加了命令行开关 -m 以允许使用 Python 模块命名空间定位模块以作为脚本执行.激励示例是标准库模块,例如 pdb 和 profile,Python 2.4 实现非常适合这个有限的目的.

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 具有完全相同的效果,这是正确的.Scope of this proposal 部分的第一行指出:

So you can specify any module in Python's search path this way, not just files in the current directory. You're correct that python mymod1.py mymod2.py args has exactly the same effect. The first line of the Scope of this proposal section states:

在 Python 2.4 中,使用 -m 定位的模块的执行就像在命令行中提供了它的文件名一样.

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 其余部分的内容.阅读它以了解更多信息.

With -m more is possible, like working with modules which are part of a package, etc. That's what the rest of PEP 338 is about. Read it for more info.

这篇关于-m 开关的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆