Multiple -m command line arguments (Python)

2019-06-15 20:50发布

I want to run both cProfiler (For time measurement, mainly) and also a memory profiler that I found here. However, both require the -m command line argument to be given, which doesn't exactly play nicely.

Is there a way to have both running? All I've managed to do so far is get the interpreter yelling at me.

If you need any more information, let me know and I'll do my best to provide it. Thanks in advance!

2条回答
Anthone
2楼-- · 2019-06-15 21:24

While it's now evident to me that you can't use two -m arguments on the same file, I managed to pull together something of a solution. It's a bit round-about, and not exactly perfect, though. I used 2 .bat files, which can be seen here. On the left hand side is the .bat that handles cProfiler, and on the right is the .bat that handles the memory profiler.

The code for the python programs seen in the .bat handling the memory profiler can be seen here and here.

The first program adds # to the line directly above the function in my main code here, which means that the program can actually run, and cProfiler can do its thing.

The second program removes that #, meaning that the memory profiler can work.

For this system to work properly with my layout, the "@profile" needs to be commented out in the first place.

It's a bit kludgey, and could use some refinement to automate it further (Such as having to specify the name of the file in the .bat file handling the memory profiler), but it'll do for now. I also realize that it's quite a specific case, but who knows, maybe someone is in the exact same position as I was...

查看更多
疯言疯语
3楼-- · 2019-06-15 21:33

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.

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

Contents of a.py:

print 'a'
import sys
print sys.argv

Contents of b.py:

print 'b'

Now try to run both using two -m arguments:

$ python -m a -m b

Output:

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

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.

查看更多
登录 后发表回答