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

by | Programming, Python, Tips

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

import os

os.cwd()

This tutorial will detail the error and how to solve it with code examples.


What is a NameError?

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 ‘os’ is not defined is usually due to not importing the module. Let’s look at an example.

Example

The os module in Python provides a set of functions for interacting with the operating system (os stands for Operating System). The os module is built-in, which means it comes with Python. One of the most used methods is os.cwd() which retrieves the Current Working Directory (CWD). Let’s try to use the cwd() method:

print(os.getcwd())
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Input In [1], in <cell line: 1>()
----> 1 print(os.getcwd())

NameError: name 'os' is not defined

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

Solution #1: Import os module

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

import os

print(os.getcwd())

Let’s run the code to see the current work directory.

/Users/admin

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 getcwd() method from the os module. Using the from keyword means we do not have to specify the module in the rest of the program, we only need to call the getcwd() method. Let’s look at the revised code:

from os import getcwd

print(getcwd())

Let’s run the code to see the current work directory.

/Users/admin

Summary

Congratulations on reading to the end of this tutorial!

For further reading on NameErrors, go to the article: How to Solve Python NameError: name ‘sys’ is not defined

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!

Research Scientist at Moogsoft | + posts

Suf is a research scientist at Moogsoft, specializing in Natural Language Processing and Complex Networks. Previously he was a Postdoctoral Research Fellow in Data Science working on adaptations of cutting-edge physics analysis techniques to data-intensive problems in industry. In another life, he was an experimental particle physicist working on the ATLAS Experiment of the Large Hadron Collider. His passion is to share his experience as an academic moving into industry while continuing to pursue research. Find out more about the creator of the Research Scientist Pod here and sign up to the mailing list here!

Follow the Research Scientist Pod on Social media!