If you are a Python developer, you may have encountered the NameError name 'glob' is not defined
error at some point in your coding journey. This error occurs when you try to use the glob
module in your code, but Python cannot find it.

In this tutorial, we will cover common causes of the error and provide solutions to help you get your code up and running quickly. So, let’s get started!
Why does the NameError: name 'glob' is not defined
error occur?
This error occurs when you try to use the glob
library in your Python code, but Python cannot find the glob
library in its namespace. The following are some of the scenarios in which this error usually occurs.
- You have not imported the glob module.
- You have imported the glob module using a different name.
How to fix the NameError: name 'glob' is not defined
?
The glob
module in Python is used to retrieve files/pathnames matching a specified pattern. It uses wildcards such as *
and ?
to match filenames.
Since this module is part of the Python Standard Library, you don’t need to separately install it. You can simply import it and start using it. Let’s now look at the above scenarios that may result in the above error in detail.
Highlighted programs for you
Flatiron School
Flatiron School
University of Maryland Global Campus
University of Maryland Global Campus
Creighton University
Creighton University
The glob
module is not imported
It can happen that you are trying to use the glob
module without even importing it. This is because Python does not recognize the statistics library and its functions until it is imported into the code.
For example, let’s try to use the glob
module without importing it and see what we get.
# note that glob is not imported # Get all files in the current directory with .txt extension txt_files = glob.glob('*.txt') print(txt_files)
Output:
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[3], line 4 1 # note that glob is not imported 2 3 # Get all files in the current directory with .txt extension ----> 4 txt_files = glob.glob('*.txt') 5 print(txt_files) NameError: name 'glob' is not defined
We get a NameError
stating that the name glob
is not defined. To use the glob
library, you need to import it first.
import glob # Get all files in the current directory with .txt extension txt_files = glob.glob('*.txt') print(txt_files)
Output:
['file2.txt', 'file1.txt', 'get_cwd.py.txt', 'learn.txt']
Here, we are importing the glob
module first and then using it to get the filenames of all the text files (having .txt
extension) in the current working directory. You can see that we did not get any errors here.
The glob
module is imported using a different name
If you import the glob
module using a different name, for example import glob as gb
, and then try to use the name “glob” to use it, you will get a NameError
because the name “glob” is not recognized in your current namespace.
Let’s look at an example.
import glob as gb # Get all files in the current directory with .txt extension txt_files = glob.glob('*.txt') print(txt_files)
Output:
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[1], line 4 1 import glob as gb 3 # Get all files in the current directory with .txt extension ----> 4 txt_files = glob.glob('*.txt') 5 print(txt_files) NameError: name 'glob' is not defined
We get a NameError: name 'glob' is not defined
. This is because we have imported the glob
module with the name gb
but we’re trying to use it using the name glob
.
To fix this error, you can either use the name that you have used in the import statement or import glob
without an alias. Note that the convention is to use import glob
(without an alias) as it is recommended that you follow this convention that it makes your code more standardized and makes it easy to read by other developers.
Conclusion
In conclusion, the “NameError name ‘glob’ is not defined” error can be fixed by importing the glob module in your Python script. This error occurs when the interpreter cannot find the definition of the glob module. By importing the module, you can access its functions and use them in your code. Remember to check your spelling and syntax when importing the module, as errors in the import statement can also cause this error. With these steps, you should be able to fix the “NameError name ‘glob’ is not defined” error and continue with your Python programming.
You might also be interested in –
- How to Fix – SyntaxError: EOL while scanning string literal
- How to Fix – NameError name ‘exit’ is not defined