Select Page

How to Solve Python NameError: name ‘sys’ is not defined

by | Programming, Python, Tips

This error occurs when you try to use the sys module without importing it first. You can solve this error by importing the module. For example,

import sys

print(sys.version)

This tutorial will go through how to solve the error with code examples.


NameError: name ‘sys’ is not defined

Python raises the NameError when it cannot recognise a name in our program. In other words, the name we are trying to use is not defined in the local or global scope. A name can be related to a built-in function, module, or something we define in our programs, like a variable or a function.

The error typically arises when:

  • We misspell a name
  • We do not define a variable or function
  • We do not import a module

In this tutorial, the source of the error NameError: name ‘sys’ is not defined is usually due to not importing the module. Let’s look at an example.

Example

The sys module in Python provides access to some of the variables used or maintained by the Python interpreter. The module also provides functions to manipulate different aspects of the Python runtime environment.

The sys module is built-in, which means it comes with Python. One of the most used functions is sys.argv() which provides a list containing the command line arguments passed to a Python script. Let’s look at an example:

n = len(sys.argv)

print('Total arguments passead: ', n,'\n')

print('Name of the Python script: ', sys.argv[0], '\n')

print('Arguments passed to script:', end = ' ')

for i in range(1, n):

    print(sys.argv[i], end=' ')

sum_val = 0

for i in range(1, n):

    sum_val += int(sys.argv[i])

print(f'\n\nSum value: {sum_val}')

In the above code, we use sys.argv to take in several arguments, the first argument is always the name of the Python script. Next, we define a variable called sum_val and incrementally add the numeric arguments to it.

Let’s try to run the script with five integers as arguments:

python sum_script.py 1 2 3 4 5
Traceback (most recent call last):
  File "sum_script.py", line 1, in <module>
    n = len(sys.argv)
NameError: name 'sys' is not defined

The error occurred because we did not import the sys module. Although sys is a built-in module, we still need to import it.

Solution #1: Import sys

We can import the module by putting an import statement at the top of the program. Let’s look at the updated code:

import sys

n = len(sys.argv)

print('Total arguments passead: ', n,'\n')

print('Name of the Python script: ', sys.argv[0], '\n')

print('Arguments passed to script:', end = ' ')

for i in range(1, n):

    print(sys.argv[i], end=' ')

sum_val = 0

for i in range(1, n):

    sum_val += int(sys.argv[i])

print(f'\n\nSum value: {sum_val}')

Let’s run the code to get the sum of the five numbers:

python sum_script.py 1 2 3 4 5
Total arguments passead:  6 

Name of the Python script:  sum_script.py 

Arguments passed to script: 1 2 3 4 5 

Sum value: 15

Solution #2: Use the from keyword

We can also use the from keyword to import a specific variable, class or function from a module. In this case, we want to import the argv() function from the sys module.

Using the from keyword means we do not have to specify the module in the rest of the program, we only need the argv() function.

Let’s look at the updated code:

from sys import argv

n = len(argv)

print('Total arguments passead: ', n,'\n')

print('Name of the Python script: ', argv[0], '\n')

print('Arguments passed to script:', end = ' ')

for i in range(1, n):

    print(argv[i], end=' ')

sum_val = 0

for i in range(1, n):

    sum_val += int(argv[i])

print(f'\n\nSum value: {sum_val}')

Let’s run the code to get the sum of the numbers:

python sum_script.py 1 2 3 4 5
Total arguments passead:  6 

Name of the Python script:  sum_script.py 

Arguments passed to script: 1 2 3 4 5 

Sum value: 15

Summary

Congratulations on reading to the end of this tutorial!

For further reading on NameErrors, go to the articles:

To learn more about Python for data science and machine learning, go to the online courses page on Python for the most comprehensive courses available.

Have fun and happy researching!