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

查看:174
本文介绍了-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.pysys.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?

推荐答案

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天全站免登陆