Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ValueError: not enough values to unpack (expected 4, got 1)

Tags:

python

pycharm

The error is at script, first, second, third = argv. I would like to understand why I am getting the error and how to fix it.

from sys import argv

script, first, second, third = argv
print("The script is called: ", script)
print("The first variable is: ", first)
print("The second variable is: ", second)
print("The third variable is: ", third)
like image 461
sed174 Avatar asked Mar 18 '16 19:03

sed174


People also ask

How do I fix not enough values to unpack Python?

Verify the assignment variables. If the number of assignment variables is greater than the total number of variables, delete the excess variable from the assignment operator. The number of objects returned, as well as the number of variables available are the same. This will resolve the value error.

How do I fix ValueError too many values to unpack expected 2?

Solution. While unpacking a list into variables, the number of variables you want to unpack must equal the number of items in the list. If you already know the number of elements in the list, then ensure you have an equal number of variables on the left-hand side to hold these elements to solve.

How do I fix too many values to unpack in python?

We can solve this by ensuring the number of variables equals the number of items in the list to unpack. The error can also happen when trying to iterate over the items in a dictionary. To solve this, you need to use the items() method to iterate over a dictionary.

How do I fix ValueError is not enough values to unpack expected 2 got 0?

The “ValueError: not enough values to unpack” error is raised when you try to unpack more values from an iterable object than those that exist. To fix this error, make sure the number of values you unpack from an iterable is equal to the number of values in that iterable.


2 Answers

Run it from the shell like this:

python script.py arg1 arg2 arg3
like image 101
dnit13 Avatar answered Oct 04 '22 14:10

dnit13


argv variable contains command line arguments. In your code you expected 4 arguments, but got only 1 (first argument always script name). You could configure arguments in pycharm. Go to Run -> Edit Configurations. Then create a new python configuration. And there you could specify Script parameters field. Or you could run your script from command line as mentioned by dnit13.

like image 22
kvorobiev Avatar answered Oct 04 '22 14:10

kvorobiev