2

Had a quick question regarding running a script in Python. I have the following code typed into a file saved as "ex13.py":


from sys import argv

script, first, second, third = argv

print "The script is called:", script
print "Your first variable is:", first
print "Your second variable is:", second
print "Your third variable is:", third

When I try to simply "run" it from the dropdown menu in IDLE, it gives me this error:


Traceback (most recent call last):
  File "/Users/brianerke 2/Desktop/ex13.py", line 3, in <module>
    script, first, second, third = argv
ValueError: need more than 1 value to unpack

What do I need to type into the interpreter/terminal to run this script? Thanks so much!

Brian

Ned Deily
  • 83,389
  • 16
  • 128
  • 151
Brian Erke
  • 21
  • 1
  • 2
  • See [this question](http://stackoverflow.com/questions/2148994/when-running-a-python-script-in-idle-is-there-a-way-to-pass-in-command-line-arg) for information regarding passing command-line arguments to a script to be run from within IDLE. – Dennis Williamson Dec 03 '11 at 22:37
  • The method described there wouldn't quite work on standard OS X installations of Python as IDLE isn't installed that way, i.e it is installed as just `idle`. But IDLE is usually launched on OS X by double-clicking an icon. – Ned Deily Dec 03 '11 at 22:41

3 Answers3

2

If you want to run from terminal you would write

python Desktop/ex13.py 1 2 3

assuming you are in your home folder and want to pass arguments 1, 2 and 3.

However, your print line does not seem to be valid, separating the prints to separate lines

print "The script is called:", script 
print "Your first variable is:", first 
print "Your second variable is:", second 
print "Your third variable is:", third

I get

python work/t.py 1 2 3
The script is called: work/t.py
Your first variable is: 1
Your second variable is: 2
Your third variable is: 3
Roger Lindsjö
  • 11,330
  • 1
  • 42
  • 53
1

In order for that script to work, you need to provide 3 positional parameters on the command line.

python /Users/brianerke 2/Desktop/ex13.py option1 option2 option3

To avoid the error, you can check the length of argv:

if len(sys.argv) < 3:
    print "You must supply three arguements"
    sys.exit(1)
jordanm
  • 33,009
  • 7
  • 61
  • 76
1

Unfortunately there isn't an easy way to directly supply arguments when running a Python script from within the OS X version of IDLE. As others point out, the simplest solution is to edit in IDLE, save the script, then run the script directy from within a Terminal session window using Terminal.app using the python command.

Another approach would be to add some scaffolding code to allow you to simulate passing command line arguments. A simple (and not the best) example:

from sys import argv

def main(argv):
    script, first, second, third = argv

    print "The script is called:", script
    print "Your first variable is:", first
    print "Your second variable is:", second
    print "Your third variable is:", third

if __name__ == '__main__':
    if len(argv) < 4:
        argv = ['script', '1', '2', '3']
    main(argv)
Ned Deily
  • 83,389
  • 16
  • 128
  • 151