Trying to fix the Python nameerror: name sns is not defined error message…
However, you are struggling to fix it? Then, keep on reading.
In this article, we’ll show how to fix nameerror name sns is not defined in simple and easy way.
What is “sns” module?
The “sns” module is the abbreviation for the seaborn library, which is a Python data visualization library based on matplotlib.
This module provides a high-level interface for drawing attractive and informative statistical graphics.
In addition to that, this library is commonly imported with the shorthand sns, like the following:
✅ import seaborn as snsWhat is “nameerror name sns is not defined”?
The error message nameerror: name sns is not defined occurs when you’re trying to use the “sns” module without importing it first or you did not install the seaborn library.
For example:
import matplotlib.pyplot as plt
plt.plot([1, 2, 3, 4, 5])
sns.set_style("whitegrid") # This line will cause the error
plt.show()
If you try to run this code, it will throw a NameError that indicates name sns is not defined.
This error message indicates that you’re trying to use a variable or function named sns.
However, the Python interpreter does not recognize what sns is, because it is not defined or imported.
Why does this error occur?
The nameerror name sns is not defined error message can be raised if you forget to define a variable or if you’re trying to use a library like seaborn (which is commonly imported as sns) but haven’t imported it correctly.
How to fix “nameerror: name sns is not defined”?
To fix the Python nameerror name ‘sns’ is not defined error, you need to ensure that the seaborn library is installed and it is imported correctly and assigned to the variable “sns.”
Here are the following solutions that you use to finally resolved the error.
Solution 1: Install seaborn library
When you haven’t installed the seaborn library yet, you can use the “pip install” command to install the library in your environment.
Here are the steps to install the seaborn library:
- Go to Anaconda Command Prompt.
- Input activate tensorflow or conda activate.
- Then, install the seaborn.
You can also install it directly in your command prompt or terminal. Here’s the command that you can use to install the seaborn library:
✅ pip install seabornSolution 2: Import seaborn as sns
After the seaborn library is installed you may now Import seaborn and assign it to the variable sns.
You can do this by adding the following line at the beginning of your code.
For example:
✅ import seaborn as sns
import matplotlib.pyplot as plt
# Set the style of the plot
sns.set_style("darkgrid")
# Create some data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
# Create a plot
plt.plot(x, y)
# Show the plot
plt.show()
As you have noticed, you simply need to import Seaborn as “sns” in your code.
Once you have done that, the “sns.set_style()” function will be recognized by Python, and the code will run without any errors.
Output:

Solution 3: Import seaborn with a different variable name
If you don’t want to use the variable name sns, you can import seaborn with a different variable name.
For example:
import seaborn as sb
import matplotlib.pyplot as plt
# Set the style of the plot
sb.set_style("darkgrid")
# Create some data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
# Create a plot
plt.plot(x, y)You could use import seaborn and assign it to the variable sb to fix the error. The output would be the same as what you can see in the solution 2 above.
Conclusion
In conclusion, the error message nameerror: name sns is not defined occurs when you’re trying to use the “sns” module without importing it first or you did not install the seaborn library.
This article explores what this error is all about and already provides solutions to help you fix this error.
You could also check out other “nameerror” articles that may help you in the future if you encounter them.
- Nameerror name ‘json’ is not defined
- Nameerror name data is not defined
- Nameerror name requests is not defined
We are hoping that this article helps you fix the error. Thank you for reading itsourcecoders 😊
Function-related NameError
NameError on a function usually means the function was not defined yet, the wrong name was used, or the function is in a different scope.
Common triggers
- Function name typo.
caclulate()instead ofcalculate(). - Called before def. At module level.
- Function in wrong module. Imported from a different module than expected.
- Decorated function shadowed. Decorator returned wrong name.
Diagnostic pattern
# BAD — undefined function
def main():
result = compute_total() # NameError if compute_total not defined
main()
# GOOD — check spelling and scope
def compute_total():
return 42
def main():
result = compute_total()
print(result)
main()
# For decorated functions, make sure functools.wraps is used
from functools import wraps
def my_decorator(fn):
@wraps(fn)
def wrapper(*args, **kwargs):
return fn(*args, **kwargs)
return wrapper
Best practices
- Use editor autocomplete. Prevents most typo NameError.
- Order definitions before calls at module level.
- Use type hints. Editor flags undefined names immediately.
- Use functools.wraps in decorators to preserve the original function name.
Official documentation
Frequently Asked Questions
What is Python NameError and what causes it?
NameError is raised when Python encounters a name (variable, function, class) that hasn’t been defined in the current scope. Most common causes: typo in variable name, using a variable before assigning it, missing import, or referencing a variable that was defined inside a function but accessed outside it.
How do I fix ‘name X is not defined’?
Check three things: (1) Is the name a typo? Compare with the spelling where you defined it. (2) Did you import it? Add ‘from module import X’ or ‘import module’ at the top. (3) Is X defined in a different scope (inside a function, conditional branch, or with-block) that hasn’t executed yet at the point you’re using it? Move the definition before the use.
Why does my variable work in one cell but not another (Jupyter)?
Jupyter kernels keep state between cells. If you defined X in cell 5 and run cell 3 later, X exists. But after Kernel-Restart, only the cells you re-run define their variables. Always run cells top-to-bottom on a fresh kernel before submitting. Use ‘Restart and Run All’ to verify your notebook is reproducible.
What is the difference between NameError and AttributeError?
NameError: the name itself doesn’t exist anywhere in scope (typo, missing import, scope issue). AttributeError: the name exists and points to an object, but that object has no such attribute/method (typo on method name, wrong object type). NameError is about the variable; AttributeError is about what’s inside it.
Where can I find more NameError fixes?
Browse the NameError reference hub for 49+ specific fixes (NumPy, pandas, Jupyter, Python 2 to 3 migration). For Python scope rules see the Python Tutorial hub. For attribute-level errors see AttributeError.
