Python ValueError: Not Enough Values to Unpack

Python ValueError: Not Enough Values to Unpack

This tutorial will discuss solving the value error using the same number of arguments with the correct data types required by the script to run in Python.

Python ValueError: Not Enough Values to Unpack

The ValueError arises because the number of arguments required by a script to run is not provided, or the arguments data type is different from the data type required by the script. For example, let’s discuss an example using the argv library, which converts arguments from the command prompt to objects that can be used later in the code.

Using the argv library to write a script file, we will set two arguments from which one is required to run the script file. See the code below.

from sys import argv

script, user_name =argv
print(f"Hi {user_name}, I'm the {script} script")

Output:

> python test.py
Traceback (most recent call last):
  File "C:\Users\ammartest.py", line 3, in <module>
    script, user_name =argv
ValueError: not enough values to unpack (expected 2, got 1)

To run the above script, we have to write python test.py in the cmd or command prompt and make sure Python is added to the system path or environmental variables on Windows, and if it is not added, we can use a full path to the python.exe file. We also have to make sure the current directory of the command prompt is set to the Python file’s directory; if it’s not, we have to provide the full path to the Python file.

For example, suppose we have to add Python to the system path, and we are also not in the current directory of the Python file. We must change the first line in the above output to the below line.

& C:/Users/ammar/python.exe "c:/Users/ammar/test.py"

The above line is tested in Windows PowerShell, and for cmd, we have to remove the & at the start of the line.

We can notice in the output above that there is an error that says that 2 arguments are required, but only one is passed, which is the name of the script file. The second argument is a string that must be provided in the command prompt to run the script file.

For example, let’s repeat the above example and provide a string when running the script in the command prompt. See the code below.

from sys import argv

script, user_name =argv
print(f"Hi {user_name}, I'm the {script} script")

Output:

> python test.py ben
Hi ben, I'm the test.py script

In the code above, we can notice that the error is resolved. Now, the script works, displaying the user’s name passed as a string and the script file’s name in the command prompt.

The argv module belongs to the sys library. Check this link for more details about the sys library.

Author: Ammar Ali
Ammar Ali avatar Ammar Ali avatar

Hello! I am Ammar Ali, a programmer here to learn from experience, people, and docs, and create interesting and useful programming content. I mostly create content about Python, Matlab, and Microcontrollers like Arduino and PIC.

LinkedIn Facebook

Related Article - Python Error

  • Local Variable Referenced Before Assignment Error in Python
  • Python Handling Socket.Error: [Errno 104] Connection Reset by Peer
  • Python Is Not Recognized in Windows 10
  • NameError: The OS Module Is Not Defined in Python
  • Resolve the TypeError: Can't Multiply Sequence by Non-Int of Type STR in Python