pythonpython-2.7

Learn Python the hard way Exercise 13 error


Every time I run this program I get "ValueError: need more than 1 variable." But I'm doing what Zed says to do by running ex13.py first 2nd 3rd. I don't need to type python in the terminal before a filename because my computer recognizes python files. I'm on Windows 7 and using python 2.7. Any help would be appreciated. I've tried the most popular answer in this thread: ValueError: need more than 1 value to unpack , but I'm still getting the same error. Any help would be much appreciated

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

Edit: Here's the error I'm getting: Traceback (most recent call last): File "C:\Users\Ian\lpthw\ex13.py", line 3, in script, first, second, third = argv ValueError: need more than 1 value to unpack


Solution

  • It's what Winston Ewert said in the comments. You have to tell your system where's python, otherwise it won't be able to run your script properly. For example, in Linux/Unix you achieve that using a shebang pointing to the python executable, like #!/usr/bin/python, or just running the script like python your_script. Try running it from the command like using python ex13.py first 2nd 3rd and you will see that it works.


    As you are on Windows, here's how to properly configure Python on it:

    3.3. Configuring Python

    3.3.1. Excursus: Setting environment variables

    3.3.2. Finding the Python executable

    3.3.3. Finding modules

    3.3.4. Executing scripts

    ~ Official Documentation