user3765790
user3765790

Reputation:

Writing Python script in PowerShell: how to use argv?

I'm a beginner to Python. I've already tried to search for the answer to this question for a long time but had no luck with that. Anyhow here's the code that I want to run (I saved it as test.py):

    from sys import argv
    script, firstArgument = argv
    print "Hello %s!" %firstArgument

It works perfectly fine if I type this into PowerShell:

    >> cd ~
    >> Python test.py "StackOverflow"

However if I try to type the code into PowerShell like this:

    >> cd ~
    >> Python
    >> from sys import argv
    >> script, firstArgument = argv

I get a huge error at that point... I'm just wondering, is there a way to declare argv directly into the PowerShell or is it just not possible? -Thanks

Upvotes: 0

Views: 955

Answers (1)

khampson
khampson

Reputation: 15306

When you invoke python on its own to get it into interactive mode, there are no arguments passed in. Therefore, argv is empty.

I'm running on Ubuntu, but it's the same general concept in Windows:

:~  $ python
Python 2.7.6 (default, Mar 22 2014, 22:59:56) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from sys import argv
>>> print argv
>>> ['']

Powershell and Python are two separate things, each with their own concepts of the arguments passed to them. Just as a bash script has the concept of passed-in arguments, so too does Powershell, though they are handled differently. However, neither is directly connected to Python's argv.

In general, one can use environment variables to pass things back and forth between Python and the shell (i.e. you could set environment variables inside of Python that could be read in the shell or vice versa, but in either case, these would be different from argv, which is specifically the arguments passed in to the python executable). Or one could use environment variables from the shell to determine what arguments should be passed to python, thereby determining what argv will be.

See this link for more detail: https://docs.python.org/2/library/sys.html#sys.argv

Edit in response to comment from OP:

You can't declare argv, per se. It's representative of how the interpreter was invoked, and in interactive mode, by definition, it has no arguments.

For your specific example, it has to do with the way tuples are unpacked in Python and the current state of argv, which is empty string.

:~  $ python
Python 2.7.6 (default, Mar 22 2014, 22:59:56) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from sys import argv
>>> firstArgument=argv 
>>> print firstArgument
['']
>>> firstArgument, secondArgument=argv 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: need more than 1 value to unpack

Since argv only has one element in it, you can't assign it to two variables.

You could, for example, do this:

>>> firstArgument, secondArgument=argv,1 
>>> print secondArgument
1

Now there actually are two values -- the tuple ('', 1), and so it can be unpacked into the two different variables.

Upvotes: 3

Related Questions