1

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

Community
  • 1
  • 1
i32505
  • 37
  • 1
  • 7
  • Can you copy/paste the entire output from python? – Winston Ewert Jun 26 '14 at 04:42
  • I added it to my question. – i32505 Jun 26 '14 at 04:52
  • 3
    I'm going to guess here that whatever configured your computer to run .py files from the command line didn't configure it to pass along the command line arguments. Can you try explicitly calling python and passing it the script and arguments to see if I'm right? – Winston Ewert Jun 26 '14 at 04:57
  • @i32505 I cannot replicate your problem on my machine - your code works perfectly for me when invoked from the command line with the commands: `python ex13.py first 2nd third` – Matt Coubrough Jun 26 '14 at 05:02
  • 1
    It seems like your assumption is correct. I did what you said to do and it returned "'python' is not recognized as an internal or external command, operable program, or batch file." – i32505 Jun 26 '14 at 05:04
  • Try: C:\python27\python ex13.py first 2nd third – Winston Ewert Jun 26 '14 at 05:08
  • That worked. So will I have to do that with other programs like this, or is there a way that i can set up my terminal so I can type python before a .py file and get it to run ? – i32505 Jun 26 '14 at 05:13
  • 1
    @i32505 this thread talks about ways to add Python to your Path on Windows (apologies if you're using another OS): http://stackoverflow.com/a/4855685/3651800 – Matt Coubrough Jun 26 '14 at 05:36

3 Answers3

3

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

jimm-cl
  • 5,124
  • 2
  • 25
  • 27
  • I tried running python and passing the arguments in terminal, but I got this in return: "'python' is not recognized as an internal or external command, operable program, or batch file." How do I tell the system where python is ? Do you mean running python in terminal ? Because I've done that before all this and I still don't need to type python in the terminal before I run a .py file. – i32505 Jun 26 '14 at 05:06
  • There you have it! Windows does not know where to look for `python` – jimm-cl Jun 26 '14 at 05:07
0

you need to use terminal to do this excersise. lets say your 'ex13.py' is saved in a folder called 'coding' on 'desktop'.

in terminal, first you would need to write: cd desktop

then, you would need to write: cd coding

finally, you would write: python ex13.py first second third

if your file or folder or location is different, change this accordingly

-1

This could be due to an error caused by incorrect copying from the Learn Python the Hard Way PDF. When you copy the code, make sure that you do not also copy the numbers on the left-hand side. If you copy the numbers too, that could cause your program to crash.

Robert Columbia
  • 6,313
  • 15
  • 32
  • 40
  • Hey, Robert thank you for correcting my English. I was just trying to be more like a friend. Not like PRO or Professional coder. Thanks :) – Arghadeep Kar Sep 21 '19 at 10:31