Last updated: Apr 8, 2024
Reading timeยท3 min

The Python "NameError: name 'os' is not defined" occurs when we use the os
module without importing it first.
To solve the error, import the os module before using it - import os.

Here is an example of how the error occurs.
BASE = '/user' # โ๏ธ NameError: name 'os' is not defined print(os.path.join(BASE, 'articles')) print(os.environ['VIRTUAL_ENV_PROMPT'])
os module before using itTo solve the error, we have to import the os module.
# ๐๏ธ import os module first import os BASE = '/user' # ๐๏ธ /user/articles print(os.path.join(BASE, 'articles')) # ๐๏ธ (venv) print(os.environ['VIRTUAL_ENV_PROMPT'])

Even though the os module is in the Python standard library, we still have to
import it before using it.
o when importing os because module names are case-sensitive.os module in a nested scopeAlso, make sure you haven't imported os in a nested scope, e.g. a function.
def join_path(): import os BASE = '/user' print(os.path.join(BASE, 'articles')) print(os.environ['VIRTUAL_ENV_PROMPT']) # โ๏ธ NameError: name 'os' is not defined print(os.environ['VIRTUAL_ENV_PROMPT'])

We imported the os module in a function, so we aren't able to use it outside
of the function.
Import the module at the top level to be able to use it throughout your code.
import os def join_path(): BASE = '/user' print(os.path.join(BASE, 'articles')) print(os.environ['VIRTUAL_ENV_PROMPT']) print(os.environ['VIRTUAL_ENV_PROMPT'])

The import statement for the os module has to come at the top of the file
before any code that makes use of it.
os module in a try/except statementYou also should be importing the os module in a
try/except statement.
try: # ๐๏ธ Code here could raise an error import os print(os.environ['VIRTUAL_ENV_PROMPT']) except ImportError: print(os.environ['VIRTUAL_ENV_PROMPT']) print(os.environ['VIRTUAL_ENV_PROMPT'])
The code sample works, however, if the code in the try statement raises an
error, the os module won't get imported successfully.
This would cause the error because we are trying to access properties on the
os module in the outer scope and the except block.
Instead, move the import statement to the top level of the file.
import os try: print(os.environ['VIRTUAL_ENV_PROMPT']) except ImportError: print(os.environ['VIRTUAL_ENV_PROMPT']) print(os.environ['VIRTUAL_ENV_PROMPT'])
os moduleAn alternative to importing the entire os module is to import only the
functions and constants that your code uses.
from os import path, environ BASE = '/user' # ๐๏ธ /user/articles print(path.join(BASE, 'articles')) # ๐๏ธ (venv) print(environ['VIRTUAL_ENV_PROMPT'])

The example shows how to import the path module and the environ mapping
object from the os module.
Instead of accessing the members on the module, e.g. os.environ, we now access
them directly.
This should be your preferred approach because it makes your code easier to read.
import os, it is much harder to see which functions from the os module are being used in the file.Conversely, when we import specific functions, it is much easier to see which
functions from the os module are being used.
The os module provides a portable way of using operating system-dependent
functionality.
You can view all of the functions and constants the os module provides by
visiting the official docs.