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.
Table of contents
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!
Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.