What does 'sys.argv' mean?
Asked Answered
B

4

5

I am learning from code, and I am get confused by one of its lines which is:

things = [float(arg) for arg in sys.argv[1:]]
Omega_a, Omega_b, Delta_a, Delta_b, \
init_pop_a, init_pop_b, tstep, tfinal = things

I have searched online and tried to understand what sys.arg means, and here is my understanding:

So sys.argv[0] is the file name, and sys.argv[1:] is the rest of the parameters which should given by users. I am not sure am I understood it right, and if it is, then I don't understand why cant it be like:

Omega_a = input() 
Omega_b = input()
etc...

What's the difference between these two ways of giving parameters?

Also, if I run the code (press F5), the Python shell give me an error like:

Traceback (most recent call last):

File "C:\Users\testcode.py", line 55, in <module>
init_pop_a, init_pop_b, tstep, tfinal = things
ValueError: need more than 0 values to unpack

I wasn't even given a chance to give parameters (sys.argv[1:]) before it gave me an error. So I searched online. It looks like I need to run this code in cmd which confused me more, why should it and how should I put into cmd in order to run it?

Blackthorn answered 26/2, 2012 at 17:6 Comment(0)
I
5

The difference is, that sys.argv (command line) parameters are given before the program is running (while starting it):

python testcode.py arg1 arg2 arg3 arg4 and so on ...

This would result in your variables being:

Omega_a = 'arg1'
Omega_b = 'arg2'
Delta_a = 'arg3'
Delta_b = 'arg4'
init_pop_a = 'and'
init_pop_b = 'so'
tstep = 'on'
tfinal = '...'

While the input()s are given when the program is running.

As you do not start the program with parameters it gives you the error, because there are not enough (exactly 0) parameters to be unpacked into the variables.

Ithyphallic answered 26/2, 2012 at 17:11 Comment(0)
C
2

sys.argv are called "command line parameters". If you want to pass them, you should run you script from a command line. On a Windows system the command would look like:

cmd> python C:\Users\testcode.py arg1 args2

where "cmd>" is the prompt you get after using "Start"->"Run".

Celebration answered 26/2, 2012 at 17:12 Comment(0)
T
2

All of the other answers explained sys.argv just fine, but I think there was a piece of fundamental terminology that was missing. I just wanted to add that...

input() tells your program to read from stdin.

It's like reading from a file and is a stream. The input() call reads until a newline is reached. You can also read stdin until an EOF is reached (end of file).

sys.argv on the other hand is simply a list that is made available to you from the system containing all the arguments that were used to call the command from the shell. Technically there is some type of limit (on a system-by-system basis) to the maximum number of arguments that can be passed on the command line, which is why the xargs command exists (to call your command with batches of your arguments, split up).

stdin

echo "I am stdin" | myCommand.py

... Which is the same concept under the hood as doing this AFTER your program is running:

read_from_stdin = input()

args

myCommand.py "I am an arg"

And finally, reading from stdin/input() will not automatically split your words into a list. There are extra methods for reading by line that you can use. But you can also read by character, a specific amount of characters at a time, or the entire amount of data at once.

Tresatrescha answered 26/2, 2012 at 17:29 Comment(0)
M
1

Parameters aren't the same as program input. For example, here's wget used with parameters:

$ wget "I am a parameter!"

Here's cat used with input:

$ cat
Now you type. This is the input.

That's the reason for your error, too - you can't specify the parameters as such after you run the program.

Muscarine answered 26/2, 2012 at 17:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.