Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple -m command line arguments (Python)

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!

like image 645
dantdj Avatar asked Nov 16 '12 20:11

dantdj


People also ask

How do you pass two command line arguments?

To pass command line arguments, we typically define main() with two arguments : first argument is the number of command line arguments and second is list of command-line arguments. The value of argc should be non negative. argv(ARGument Vector) is array of character pointers listing all the arguments.

Why do we need command line arguments in Python?

Python Command line arguments are input parameters passed to the script when executing them. Almost all programming language provide support for command line arguments. Then we also have command line options to set some specific options for the program.

What is the significance of argc and argv in command line arguments?

It stores the number of the command line arguments passed by a user from the terminal and also stores the name of the program. The value of argc must not be negative. argv:- It is a pointer array and it points to every argument which is being passed to the program. Argv[0] denotes the name of the program.


1 Answers

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.

like image 187
Lesmana Avatar answered Oct 06 '22 18:10

Lesmana