且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

多个 -m 命令行参数 (Python)

更新时间:2023-08-25 23:13:10

使用两个 -m 参数来启动两个模块是不可能的.这是因为 -m 之后的命令行参数都作为 sys.argv 提供给命名模块.这在文档中没有明确描述,但您可以通过实验进行尝试.

It is not possible to start two modules using two -m arguments. This is because the command line arguments after -m are all given to the named module as sys.argv. This is not described explicitly in the documentation but you can try it out experimentally.

创建两个python文件a.pyb.py.

Create two python files a.py and b.py.

a.py 的内容:

print 'a'
import sys
print sys.argv

b.py 的内容:

print 'b'

现在尝试使用两个 -m 参数运行两者:

Now try to run both using two -m arguments:

$ python -m a -m b

输出:

a
['/home/lesmana/tmp/a.py', '-m', 'b']

如您所见,模块 b 从未启动,因为第二个 -m 不是由 python 处理的.交给模块a处理.

As you can see module b is never started because the second -m is not handled by python. It is given to module a to handle.