If you are a Python developer, you may have encountered the error message “NameError name ‘pprint’ is not defined” while working with the pprint module in Python. In this tutorial, we will explore the possible causes of this error and provide step-by-step instructions on how to fix it.
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 'pprint' is not defined
error occur?
This error occurs when you try to use the pprint library in your Python code, but Python cannot find the pprint module in its namespace. The following are some of the scenarios in which this error usually occurs.
- You have not imported the pprint module.
- You have imported the pprint module using a different name or are not using the correct function inside the pprint module.
How to fix the NameError: name 'pprint' is not defined
?
The pprint
module in Python stands for “pretty-print” and provides a way to format and display complex data structures in a more readable and organized way. It is particularly useful when working with nested data structures such as dictionaries and lists. Since this library is part of the Python Standard Library, you don’t need to separately install it. You can simply import it and start using it.
The pprint
module provides a pprint()
function that takes a Python object as input and outputs a formatted representation of the object as a string. The output is designed to be easy to read and understand, with each element of the data structure on a separate line and nested elements indented to show their relationship to the parent element.
Let’s now look at the above scenarios that may result into the above error in detail.
The pprint
module is not imported
It can happen that you are trying to use the pprint
module without even importing it. This is because Python does not recognize the pprint library and its functions until it is imported into the code.
For example, let’s try to use the pprint
module without importing it and see what we get.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
# note that pprint is not imported # define a dictionary with nested lists and dictionaries data = { 'name': 'John', 'age': 30, 'hobbies': ['reading', 'coding', 'hiking'], 'address': { 'street': '123 Main St', 'city': 'Anytown', 'state': 'CA', 'zip': '12345' } } # use pprint to pretty-print the data pprint.pprint(data)
Output:
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[1], line 17 4 data = { 5 'name': 'John', 6 'age': 30, (...) 13 } 14 } 16 # use pprint to pretty-print the data ---> 17 pprint.pprint(data) NameError: name 'pprint' is not defined
We get a NameError
stating that the name pprint
is not defined. To use the pprint
library, you need to import it first.
import pprint # define a dictionary with nested lists and dictionaries data = { 'name': 'John', 'age': 30, 'hobbies': ['reading', 'coding', 'hiking'], 'address': { 'street': '123 Main St', 'city': 'Anytown', 'state': 'CA', 'zip': '12345' } } # use pprint to pretty-print the data pprint.pprint(data)
Output:
{'address': {'city': 'Anytown', 'state': 'CA', 'street': '123 Main St', 'zip': '12345'}, 'age': 30, 'hobbies': ['reading', 'coding', 'hiking'], 'name': 'John'}
Here, we are importing the pprint
module first and then using it to pretty print our nested dictionary. You can see that we did not get any errors here.
Note that the pprint()
function is defined in the pprint
module. This could be confusing because they both have the same name. If you want to use the pprint()
function, you can either use the above syntax of importing the entire pprint
module and then using pprint.pprint()
function or you could only import the pprint()
function like in the example below.
from pprint import pprint # define a dictionary with nested lists and dictionaries data = { 'name': 'John', 'age': 30, 'hobbies': ['reading', 'coding', 'hiking'], 'address': { 'street': '123 Main St', 'city': 'Anytown', 'state': 'CA', 'zip': '12345' } } # use pprint to pretty-print the data pprint(data)
Output:
{'address': {'city': 'Anytown', 'state': 'CA', 'street': '123 Main St', 'zip': '12345'}, 'age': 30, 'hobbies': ['reading', 'coding', 'hiking'], 'name': 'John'}
We get the same result.
The pprint
module is imported using a different name
If you import the pprint module using a different name, for example import pprint as pp
, and then try to use the name “pprint” to use it, you will get a NameError
because the name “pprint” is not defined in your current namespace.
Let’s look at an example.
import pprint as pp # define a dictionary with nested lists and dictionaries data = { 'name': 'John', 'age': 30, 'hobbies': ['reading', 'coding', 'hiking'], 'address': { 'street': '123 Main St', 'city': 'Anytown', 'state': 'CA', 'zip': '12345' } } # use pprint to pretty-print the data pprint.pprint(data)
Output:
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[1], line 17 4 data = { 5 'name': 'John', 6 'age': 30, (...) 13 } 14 } 16 # use pprint to pretty-print the data ---> 17 pprint.pprint(data) NameError: name 'pprint' is not defined
We get a NameError: name 'pprint' is not defined
. This is because we have imported the pprint
module with the name pp
but we’re trying to use it using the name pprint
.
To fix this error, you can either access pprint using the name that you have used in the import statement or import pprint without an alias.
import pprint as pp # define a dictionary with nested lists and dictionaries data = { 'name': 'John', 'age': 30, 'hobbies': ['reading', 'coding', 'hiking'], 'address': { 'street': '123 Main St', 'city': 'Anytown', 'state': 'CA', 'zip': '12345' } } # use pprint to pretty-print the data pp.pprint(data)
Output:
{'address': {'city': 'Anytown', 'state': 'CA', 'street': '123 Main St', 'zip': '12345'}, 'age': 30, 'hobbies': ['reading', 'coding', 'hiking'], 'name': 'John'}
In the above example, we are importing pprint
as pp
and then using pp
to access the pprint
module’s methods.
Alternatively, as seen in the example in the previous section, you can import pprint
without any aliases and simply use pprint
to avoid the NameError
.
Conclusion
In conclusion, encountering a NameError: name 'pprint' is not defined
error can be frustrating, but it is a common issue that can be easily fixed. By ensuring that the pprint
module is imported correctly and that the correct syntax is used when calling its functions, you can avoid this error and successfully execute your code.
You might also be interested in –