Python ValueError:需要 1 个以上的值才能解包
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4507515/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ValueError: need more than 1 value to unpack
提问by rayne117
Disclaimer: I have read the thread located at Python Error: "ValueError: need more than 1 value to unpack", and none of the answers explained how to actually get the code to run.
免责声明:我已阅读位于Python Error: "ValueError: need more than 1 value to unpack"的线程,但没有任何答案解释如何实际运行代码。
I am reading Learn Python the Hard Way, and I am on Exercise 13.
我正在阅读《Learn Python the Hard Way》,我正在练习 13。
Here is the code I am attempting to run in the IDLE thing (don't know what it is really called) for Python 2.7:
这是我试图在 Python 2.7 的 IDLE 中运行的代码(不知道它真正叫什么):
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 run, I get the error:
运行时,我收到错误:
"Traceback (most recent call last): File "C:\Python\LPTHW\examples_LPTHW\ex13.py", line 3, in script, first, second, third = argv ValueError: need more than 1 value to unpack"
“回溯(最近一次调用最后一次):文件“C:\Python\LPTHW\examples_LPTHW\ex13.py”,第 3 行,在脚本中,第一、第二、第三 = argv ValueError:需要超过 1 个值才能解包”
I have tried running the program through the Windows command prompt, to no avail. I have also run the program by typing: "python ex13.py first 2nd 3rd", in both the IDLE and the command prompt, also got the same error.
我曾尝试通过 Windows 命令提示符运行该程序,但无济于事。我还通过在 IDLE 和命令提示符下键入:“python ex13.py first 2nd 3rd”来运行该程序,也得到了相同的错误。
How do I get this code to run, and what the heck am I doing wrong?
我如何让这段代码运行,我到底做错了什么?
EDIT: @John Machin, I made a program exactly like the one you posted, saved it as a .py, then went into the correct directory where my file was saved, ran the program using "python yourcode.py B C D" and I received the following message "python is not recognized as an internal or external command..." When I run the program by just typing it's name (which works for all other programs besides this one) I get an error saying "SyntaxError: invalid syntax on line 2: print len(argv), repr(argv)".
编辑:@John Machin,我制作了一个与您发布的程序完全相同的程序,将其另存为 .py,然后进入保存我的文件的正确目录,使用“python yourcode.py BC D”运行该程序,然后我收到以下消息“python 未被识别为内部或外部命令......”当我通过输入它的名称来运行程序时(除了这个程序之外,它适用于所有其他程序)我收到一条错误消息“SyntaxError:无效语法在第 2 行:打印 len(argv), repr(argv)"。
EDIT 2 (With the new code): @John Machin, I copied your program word for word into gedit and made the .py file as usual. I opened my terminal (command prompt) and typed:
编辑 2(使用新代码):@John Machin,我将您的程序逐字复制到 gedit 中,并像往常一样制作了 .py 文件。我打开我的终端(命令提示符)并输入:
python ex13c.py B C D
And received:
并收到:
'python' is not recognized as an internal or external command, operable program or batch file."
Then I typed:
然后我输入:
ex13c.py
And received:
并收到:
3.1.3 (r313:86834, Nov 27 2010, 18:30:53) [MSC v.1500 32 bit (Intel)]
1 ['C:\Python\LPTHW\examples_LPTHW\ex13c.py']
Traceback (most recent call last):
File "C:\Python\LPTHW\examples_LPTHW\ex13c.py". line 4, in (module)
a, b, c, d = argv
ValueError: need more than 1 value to unpack
Keep in mind I am running W7 64bit, and that Zed is teaching in 2.X and I have been using 2.7 the entire book so far. Every other example has worked. Right before I started Example 11, I installed 3.1 for a different book I am about to start reading. Every time I use the IDLE included with 2.7. Perhaps it is possible I am somehow inadvertently using 3.1 while trying to code in 2.7?
请记住,我正在运行 W7 64 位,并且 Zed 在 2.X 中教学,到目前为止我一直在使用 2.7 整本书。每个其他例子都有效。就在我开始示例 11 之前,我为我即将开始阅读的另一本书安装了 3.1。每次我使用 2.7 附带的 IDLE 时。也许我在尝试使用 2.7 编码时无意中使用了 3.1?
Thanks for bearing with me.
谢谢你陪我。
回答by bdew
Try tunning it like "python test.py foo bar baz"
尝试像“python test.py foo bar baz”一样调整它
If it works like that but fails if you call it without specifying the interpreter look here: http://bugs.python.org/issue7936
如果它像那样工作但如果你在没有指定解释器的情况下调用它失败,请看这里:http: //bugs.python.org/issue7936
回答by kriss
Argvis command line arguments, and it misses values (only has one, the script name). If you want to have Nonefor missing values you can try:
Argv是命令行参数,它缺少值(只有一个,脚本名称)。如果您想拥有None缺失值,您可以尝试:
from sys import argv
argv.extend([None, None, None])
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
You could also test for number of missing arguments using len(argv)and you can also provide them as suggested in Learn Python The Hard Way:
您还可以使用测试缺少参数的数量,len(argv)您也可以按照 Learn Python The Hard Way 中的建议提供它们:
Run the program like this:
像这样运行程序:
python ex13.py first 2nd 3rd
回答by John Machin
Boil your code down to these 4 lines:
将您的代码归结为以下 4 行:
from sys import argv
print len(argv), repr(argv)
a, b, c, d = argv
print a, b, c, d
and run it in a Command Prompt window by typing
并通过键入在命令提示符窗口中运行它
python yourcode.py B C D
Then copy/paste the exact resultinto an edit of your question. Ensure that you show the ^produced with any syntax error. Also show the exact code that you ran: type yourcode.py
然后将确切的结果复制/粘贴到您的问题的编辑中。确保您显示的内容^有任何语法错误。还要显示您运行的确切代码:type yourcode.py
EditUse this instead:
编辑改用这个:
from sys import argv, version
print(version)
print(len(argv), repr(argv))
a, b, c, d = argv
print(a, b, c, d)
Edit 2
编辑 2
FIRST TRIAL: You did: python ex13c.py B C Dand got nowhere.
Now try: c:\PYTHON\python ex13c.py B C D(I'm assuming from previous evidence that that's where some version of Python is located)
第一次试验:你做到了:但python ex13c.py B C D一无所获。现在尝试:(c:\PYTHON\python ex13c.py B C D我从之前的证据中假设这是某个版本的 Python 所在的位置)
SECOND TRIAL: You did: ex13c.pywith NO ARGUMENTS (why?) and the result was: (1) It appears that .py is associated with a 3.1.3Python executable somewhere, because it was the last version installed (2) Because you supplied no arguments, len(argv) is 1, and argv contains only the path to your script.
第二次试验:你做了:ex13c.py没有参数(为什么?),结果是:(1)似乎 .py 与某处的3.1.3Python 可执行文件相关联,因为它是安装的最后一个版本(2)因为你提供没有参数,len(argv) 为 1,而 argv 仅包含脚本的路径。
SUGGESTIONS:
建议:
You want to have 2.7 and 3.1 on the same machine. This is quite possible (I have 3.1 and 2.1 to 2.7 inclusive because I support software that runs on those versions, and 1.5.2 for nostalgia) with only a mild amount of care. The main things that you need to do are (1) Install Python X.Y in a directory c:\pythonXY (2) run scripts from the commandline like this: \python27\python myscript.py arg1 arg2 etc(3) Don't (as you have done at the moment) put your own scripts and data in a software directory like c:\Python31.
您希望在同一台机器上拥有 2.7 和 3.1。这是完全有可能的(我有 3.1 和 2.1 到 2.7,因为我支持在这些版本上运行的软件,而 1.5.2 则用于怀旧),只需稍加注意。您需要做的主要事情是 (1) 在目录 c:\pythonXY 中安装 Python XY (2) 像这样从命令行运行脚本:\python27\python myscript.py arg1 arg2 etc(3) 不要(正如您目前所做的那样)将您的在 c:\Python31 等软件目录中拥有自己的脚本和数据。
So: A. Set up new script and data directories for "book1" and "book2". Copy all existing code and data to those new directories. Take a backup. B. Un-install all versions of Python C. Install 2.7 and 3.1 to their respective directories. D. Check that you can run your scripts from their new locations.
所以: A. 为“book1”和“book2”设置新的脚本和数据目录。将所有现有代码和数据复制到这些新目录。备份一下。B. 卸载所有版本的 Python C. 将 2.7 和 3.1 安装到各自的目录中。D. 检查您是否可以从新位置运行您的脚本。
回答by Lennart Regebro
The example works. It seems like you aren't running it correctly.
该示例有效。看起来你没有正确运行它。
Firstly, IDLE is a somewhat special environment that I never saw the point of, just run it with the normal Python interpreter.
首先,IDLE 是一个有点特殊的环境,我从未见过它的意义,只需使用普通的 Python 解释器运行它即可。
Secondly, specify the whole path to your python interpreter. Instead of
其次,指定 python 解释器的整个路径。代替
python ex13c.py B C D
You on Windows need to do something like this:
你在 Windows 上需要做这样的事情:
"C:\Program Filed\Python-2.7.1\Python.exe" ex13c.py B C D
Reading exercise 13 I also see that Zed calls modules "features" up until chapter 13. There is no point in doing that, that's just confusing. He also in some chapter tells people to memorizethe output of logical tables, which is nonsense. You don't need to memorize them, you need to understand them.
阅读练习 13 我还看到 Zed 在第 13 章之前都称模块为“特性”。这样做没有意义,这只是令人困惑。他还在某些章节告诉人们要记住逻辑表的输出,这是无稽之谈。你不需要记住它们,你需要理解它们。
Each time this book pops up in questions here I get less impressed with it. I'm sure there must be a better tutorial. Perhaps Dive into Python?
每次这本书在这里出现问题时,我对它的印象都不那么深刻。我相信一定有更好的教程。也许潜入Python?
回答by Rick
Read farther. Page 36 of Learn Python The Hard Way, Release 1.0 says
读得更远。Learn Python The Hard Way, Release 1.0 的第 36 页说
What You Should See
你应该看到什么
Run the program like this:
像这样运行程序:
python ex13.py first 2nd 3rd
python ex13.py 第一个第二个第三个
ARGV takes from the command line. Run it as above and put in three variables. The script will get the name of the script and then add your three arguments.
ARGV 从命令行获取。如上运行它并放入三个变量。该脚本将获取脚本的名称,然后添加您的三个参数。
[rick@redhat ~]$ python ex1.py 1st 2nd 3rd
[rick@redhat ~]$ python ex1.py 1st 2nd 3rd
The script is called: ex1.py
脚本名为:ex1.py
Your first variable is: 1st
你的第一个变量是:1st
Your second variable is: 2nd
你的第二个变量是:2nd
Your third variable is: 3rd
你的第三个变量是:3rd
[rick@redhat ~]$
[rick@redhat ~]$
回答by Sethdood
I was having this exact same problem, trying all sorts of silly stuff in the registry and what have you as well. Turned out I was just noobing up the actual command to run the script, as stated above. It's calling for values you have to put in when you go to run it. When you get to ex14.pyit would give the same error, unless you use:
我遇到了完全相同的问题,在注册表中尝试了各种愚蠢的东西,还有你有什么。事实证明,如上所述,我只是在使用实际命令来运行脚本。它要求您在运行它时必须输入的值。当你到达ex14.py它时会出现同样的错误,除非你使用:
python ex14.py (your name)
回答by mcnroe
This happened to me. Very simple fix
这发生在我身上。非常简单的修复
You are running your code out of the python software.
您正在使用 python 软件运行您的代码。
All you gotta do is go to all programs > accessories > command prompt
您所要做的就是转到所有程序>附件>命令提示符
when the command prompt window opens, then you just type "python ex13.py first second third"
当命令提示符窗口打开时,您只需键入“python ex13.py first second third”
if, when you just type "python" by itself and hit return you get an "undefined message" you'll need to follow these http://docs.python.org/using/windows.htmlto reset python as an executable "path"
如果,当您单独键入“python”并按回车键时,您会收到“未定义消息”,您需要按照这些http://docs.python.org/using/windows.html将 python 重置为可执行文件“小路”
I had no idea what "path" meant, basically it's just directing the computer where to go when you type "python"
我不知道“路径”是什么意思,基本上它只是在你输入“python”时指导计算机去哪里
回答by Lindsay
I found making a simple tweak to the directions above fixed the whole issue:
1. Open Command Prompt
2. Type in: set path=%path%;C:\python26(<--I am using 2.6.5, edit accordingly)
3. Type in: python C:/Users/research/Desktop/example.py first second third
我发现对上述说明进行简单调整即可解决整个问题: 1. 打开命令提示符 2. 输入:set path=%path%;C:\python26(<--我使用的是 2.6.5,相应地进行编辑) 3. 输入:python C:/Users/research/Desktop/example.py first second third
For me, the process only worked when the FULL FILE NAME, not just example.pyor ex13.py, whatever- was typed in.
Good luck!
对我来说,这个过程只有在输入完整文件名时才有效,而不仅仅是example.py或ex13.py,随便——祝你好运!
回答by input
Windows < Start < Run < CMD (enter)
Windows<开始<运行<CMD(回车)
go to the dir that contains the argv py file, mine was C:\Python33\Python Lessons\Lesson 1\argv.py
转到包含 argv py 文件的目录,我的是 C:\Python33\Python Lessons\Lesson 1\argv.py
then simply type, python argv.py first 2nd 3rd
然后简单地输入,python argv.py first 2nd 3rd
you should see the output in the command shell window
您应该会在命令外壳窗口中看到输出
Linux, use a bash shell and for MaxOS use their terminal window.
Linux,使用 bash shell,对于 MaxOS,使用他们的终端窗口。
argv is passed the argument via the command line, that's what everyone has said here and I am only echoing their sentiments.
argv 是通过命令行传递参数的,这就是这里每个人所说的,我只是回应他们的观点。
is there a better tutorial as the author of Learning Python the Hard Way discourages python 3.x which I think is bad for business.
有没有更好的教程,因为 Learning Python the Hard Way 的作者不鼓励 python 3.x,我认为这对业务不利。
lol, he goes as far as to tell people NO when asked about using Python 3.x and to reply with in 10 years!
大声笑,当被问及使用 Python 3.x 时,他甚至告诉人们“不”,并在 10 年内回复!
回答by ttriet204
I'm reading that same book too! "Learn python the hard way" by Zed.A.Shaw, the second edition. I stuck in the exactly same exercise! It took me days and days to find out just why I couldn't get the program run. I had googled on this alot and was rather surprised to see that I was not the only one to have this problem.
我也在看同一本书!Zed.A.Shaw 的“以艰难的方式学习 Python”,第二版。我坚持完全相同的练习!我花了几天时间才找出为什么我无法运行程序。我在谷歌上搜索了很多,很惊讶地发现我不是唯一一个遇到这个问题的人。
BUT NOW I KNOW HOW TO FIX IT AND IT NOW WORKS ...LIKE A CHARM
但现在我知道如何修复它并且它现在可以工作......就像一个魅力
First, I read Kriss's answered to "what the terminal is?" (Actually, I didn't know what that was too. I've been coding everything in LPTHD in a text editor-which can be run by choosing File --> New --> New Window in "IDLE(pythonGUI)") I then figured out (thanks to Kriss) that the terminal should be the original command prompt (not after you have run python on it)
首先,我阅读了 Kriss 对“终端是什么?”的回答。(实际上,我也不知道那是什么。我一直在文本编辑器中对 LPTHD 中的所有内容进行编码 - 可以通过在“IDLE(pythonGUI)”中选择 File --> New --> New Window 来运行)然后我发现(感谢 Kriss)终端应该是原始的命令提示符(而不是在你运行 python 之后)
After that I typed python D:/abc/ex13.py 1st second third in the command prompt.(I stored the file that I coded using the text editor provided by IDLE in th D:/abc). However, it did not work, saying that there was no such file/ directories.
之后,我在命令提示符中键入 python D:/abc/ex13.py 1st second third。(我将使用 IDLE 提供的文本编辑器编码的文件存储在 D:/abc 中)。但是,它不起作用,说没有这样的文件/目录。
Curious enough, I take a look at my file and noticed that there wasn't the EXTENSION at all (actually, the type of the file was "file"). So I decided to add ".py" in the file name and ,obviously, changed it into a python file.
奇怪的是,我查看了我的文件,注意到根本没有 EXTENSION(实际上,文件的类型是“文件”)。所以我决定在文件名中添加“.py”,显然,将其更改为 python 文件。
I tried again, typing the same command line above in the command prompt and the miracle just happened, it worked!!!
我又试了一次,在命令提示符中输入上面相同的命令行,奇迹就这样发生了,它奏效了!!!
I don't know if any of you might need this or if this maybe the answer for you, but since I've been through such hard time with this problem that it almost made me decided to change the book (cause the later chapter of LPTHD is based on this one), I decided to post this here in hope that it may help someone.
我不知道你们中是否有人可能需要这个,或者这是否可能是你的答案,但由于我在这个问题上经历了如此艰难的时期,以至于我几乎决定改变这本书(因为后面的章节LPTHD 是基于这个),我决定把这个贴在这里,希望它可以帮助别人。

