Reputation: 281
I don't understand the difference between sys.argv
and just argv
, Nothing online gives me the concept that I understand. if both are the same! When do we use sys.argv
and when to use argv
?
if not what is the sys.argv
. I've idea what is the argv
.
Upvotes: 2
Views: 2822
Reputation: 187
its the same thing, ex; of different arguments to see what happens.
from __future__ import print_function
import sys
print(sys.argv, len(sys.argv))
> python print_args.py
['print_args.py'] 1
> python print_args.py foo and bar
['print_args.py', 'foo', 'and', 'bar'] 4
> python print_args.py "foo and bar"
['print_args.py', 'foo and bar'] 2
> python print_args.py "foo and bar" and baz
['print_args.py', 'foo and bar', 'and', 'baz'] 4
As you can see, the command-line arguments include the script name but not the interpreter name. In this sense, Python treats the script as the executable. If you need to know the name of the executable (python in this case), you can use sys.executable.
Upvotes: 1
Reputation: 361739
They're the same thing, it just depends on how you write the import statement.
import sys
If you write this, then you must reference sys.argv
.
from sys import argv
from sys import *
If you write either of those, then you can write simply argv
without the sys.
qualifier.
Upvotes: 9