Last modified: Apr 22, 2026 By Alexander Williams

How to Solve modulenotfounderror no module named dataclasses

If you are working with Python and you suddenly see ModuleNotFoundError: No module named 'dataclasses', you have come to the right place. This error is a bit different from most missing module errors because dataclasses is actually part of the Python standard library — but only from Python 3.7 and above.

This means the error almost always points to a Python version problem, not a missing third-party package. In this article, you will learn exactly why this error happens and how to fix it step by step across all environments.

What Is the dataclasses Module?

The dataclasses module was introduced in Python 3.7 as part of PEP 557. It provides a decorator and helper functions for automatically generating special methods like __init__(), __repr__(), and __eq__() for user-defined classes.

Instead of manually writing boilerplate code for simple data-holding classes, you can use the @dataclass decorator to generate them automatically. This makes your code cleaner, shorter, and easier to maintain.

Here is a quick example of what dataclasses does:


from dataclasses import dataclass

# Define a data class using the @dataclass decorator
@dataclass
class Person:
    name: str
    age: int
    email: str

# Create an instance — no need to write __init__ manually
person = Person(name="Alice", age=30, email="[email protected]")
print(person)

Person(name='Alice', age=30, email='[email protected]')

The dataclasses module is built into Python 3.7 and later. You do not need to install anything extra if you are running the correct Python version.

What Causes This Error?

Here is what the full error looks like when it appears:


from dataclasses import dataclass

Traceback (most recent call last):
  File "app.py", line 1, in <module>
    from dataclasses import dataclass
ModuleNotFoundError: No module named 'dataclasses'

The main causes of this error are:

1. You are using Python 3.6 or earlier — The dataclasses module did not exist before Python 3.7. This is by far the most common cause.

2. You are using Python 2 — Python 2 does not have the dataclasses module at all. It was never backported to Python 2 officially.

3. Wrong Python environment is active — Your script is running with an older Python version even though you have Python 3.7+ installed on your machine.

4. Virtual environment uses an old Python version — Your virtual environment was created with Python 3.6 or earlier.

5. Docker or CI/CD uses an old base image — Your containerized environment is built on a Python image older than 3.7.

Fix 1: Check Your Python Version First

Before anything else, check which Python version you are currently running. This single step will confirm the root cause of the error.


python --version

Python 3.6.9

If the output shows Python 3.6.x or earlier, that is your problem. The dataclasses module is simply not available in that version.

Also check if Python 3.7+ is already installed under a different command:


python3 --version
python3.11 --version

If a newer version is already installed, use it to run your script:


python3.11 app.py

Fix 2: Upgrade to Python 3.7 or Later

The cleanest and most recommended fix is to upgrade your Python version to 3.7 or later. The current stable version is Python 3.12, and using a modern version gives you access to many improvements beyond just dataclasses.

On Ubuntu or Debian Linux, install a newer Python version with:


sudo apt update
sudo apt install python3.11

On macOS, use Homebrew:


brew install [email protected]

On Windows, download the latest Python installer from the official Python website at python.org/downloads and run the installer. Make sure to check the option to Add Python to PATH during installation.

After upgrading, confirm the new version is active:


python --version

Python 3.11.5

Now try importing dataclasses again:


from dataclasses import dataclass, field

# Test that dataclasses works after Python upgrade
@dataclass
class Product:
    name: str
    price: float
    quantity: int = 0

p = Product(name="Laptop", price=999.99, quantity=5)
print(p)

Product(name='Laptop', price=999.99, quantity=5)

Fix 3: Install the dataclasses Backport for Python 3.6

If upgrading Python is not an option right now, there is a backport package available on PyPI that brings the dataclasses module to Python 3.6. Install it using pip:


pip install dataclasses

This backport was created specifically for Python 3.6 users who need dataclasses support without upgrading. Once installed, the import works exactly the same way:


from dataclasses import dataclass

# Works on Python 3.6 with the backport installed
@dataclass
class Car:
    make: str
    model: str
    year: int

car = Car(make="Toyota", model="Corolla", year=2022)
print(car)

Car(make='Toyota', model='Corolla', year=2022)

Important: Do not install this backport on Python 3.7 or higher. On Python 3.7+, dataclasses is already built in. Installing the backport on a newer Python version can cause conflicts. The backport is only for Python 3.6.

Fix 4: Recreate Your Virtual Environment With the Right Python Version

If your virtual environment was created with Python 3.6 or earlier, you need to recreate it using a newer Python version. You cannot simply upgrade Python inside an existing virtual environment.

First, deactivate and delete the old environment:


# Deactivate the current virtual environment
deactivate

# Remove the old environment folder
rm -rf myenv

Then create a new virtual environment using Python 3.7 or later:


# Create a new virtual environment with Python 3.11
python3.11 -m venv myenv

# Activate it on Linux or macOS
source myenv/bin/activate

# Activate it on Windows
myenv\Scripts\activate

Verify the Python version inside the new environment:


python --version

Python 3.11.5

Now install your project dependencies and run your script. The dataclasses module will be available automatically.

Fix 5: Fix the Error in Anaconda

If you are using the Anaconda distribution, the environment may be using an older Python version. You can check the current Python version in your active conda environment:


conda activate myenv
python --version

If the version is below 3.7, update Python inside the conda environment:


conda install python=3.11

Or create a brand new conda environment with Python 3.11:


# Create a new conda environment with Python 3.11
conda create -n newenv python=3.11

# Activate the new environment
conda activate newenv

# Verify the version
python --version

Python 3.11.5

After this, the dataclasses module will be available with no additional installation needed.

Fix 6: Fix the Error in Jupyter Notebook

If you are encountering this error inside a Jupyter Notebook, the issue is likely that the notebook kernel is running an older Python version.

First, check the Python version being used by the kernel:


import sys

# Check the Python version inside Jupyter Notebook
print(sys.version)

3.6.9 (default, Nov 7 2019, 10:44:02)

If it shows Python 3.6 or earlier, you need to add a newer Python kernel to Jupyter. First, install the ipykernel package in your Python 3.7+ environment:


# Activate your Python 3.11 environment first
source myenv/bin/activate

# Install ipykernel
pip install ipykernel

# Add it as a Jupyter kernel
python -m ipykernel install --user --name=python311 --display-name "Python 3.11"

Now restart Jupyter Notebook and select the Python 3.11 kernel from the kernel menu. The dataclasses module will then be available.

Alternatively, if you only need to support Python 3.6 for now, install the backport inside the notebook:


import sys

# Install the dataclasses backport for Python 3.6 in Jupyter
!{sys.executable} -m pip install dataclasses

Fix 7: Fix the Error in Docker

If your application runs in a Docker container and you see this error, your Dockerfile is likely using an outdated Python base image.

Check your Dockerfile for a line like this:


# Old base image - Python 3.6 does not have dataclasses
FROM python:3.6

Update it to use Python 3.7 or later:


# Use a modern Python version with dataclasses built in
FROM python:3.11-slim

WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["python", "app.py"]

Then rebuild the Docker image:


docker build -t myapp .
docker run myapp

Using python:3.11-slim gives you a lightweight image with a modern Python version. The dataclasses module will be available automatically.

Fix 8: Fix the Error in VS Code or PyCharm

If you see the error in your IDE even though Python 3.7+ is installed, the IDE may be configured to use an older Python interpreter.

In VS Code, press Ctrl + Shift + P (or Cmd + Shift + P on Mac), type Python: Select Interpreter, and choose a Python 3.7 or later version from the list.

In PyCharm, go to File → Settings → Project → Python Interpreter. Click the gear icon and select Add Interpreter. Choose the correct Python 3.7+ interpreter from your system.

After switching the interpreter, reload your project and the error should disappear.

Advanced Usage of the dataclasses Module

Once the error is fixed, here is a more complete example showing the powerful features of the dataclasses module:


from dataclasses import dataclass, field
from typing import List

# A complete data class with default values and nested lists
@dataclass
class Student:
    name: str
    age: int
    grade: float = 0.0
    # Use field() for mutable default values like lists
    courses: List[str] = field(default_factory=list)

    def add_course(self, course: str):
        """Add a course to the student's list."""
        self.courses.append(course)

    def summary(self):
        """Return a summary of the student's data."""
        return f"{self.name} (Age {self.age}) - Grade: {self.grade}"


# Create student instances
student1 = Student(name="Bob", age=20, grade=3.8)
student1.add_course("Mathematics")
student1.add_course("Physics")

student2 = Student(name="Carol", age=22, grade=3.5)
student2.add_course("History")

# Print instances
print(student1)
print(student2)
print(student1.summary())

# Dataclasses support equality comparison automatically
student3 = Student(name="Bob", age=20, grade=3.8)
print("student1 == student3:", student1 == student3)

Student(name='Bob', age=20, grade=3.8, courses=['Mathematics', 'Physics'])
Student(name='Carol', age=22, grade=3.5, courses=['History'])
Bob (Age 20) - Grade: 3.8
student1 == student3: False

Notice the use of field() with default_factory for mutable default values like lists. Never use a plain list as a default value directly in a dataclass — always use field(default_factory=list) instead to avoid shared state bugs.

Using frozen=True for Immutable Data Classes

You can also create immutable data classes by setting frozen=True in the decorator. This makes instances behave like tuples — they cannot be modified after creation:


from dataclasses import dataclass

# Immutable data class using frozen=True
@dataclass(frozen=True)
class Point:
    x: float
    y: float

p = Point(x=3.0, y=4.5)
print(p)

# Attempting to modify a frozen dataclass raises an error
try:
    p.x = 10.0
except Exception as e:
    print(f"Error: {e}")

Point(x=3.0, y=4.5)
Error: cannot assign to field 'x'

Frozen data classes are also hashable, which means you can use them as dictionary keys or store them in sets.

Related Errors You May Encounter

If you are working on larger Python projects, you may come across similar module errors. For example, ModuleNotFoundError: No module named 'IPython' is commonly seen in interactive Python workflows and is caused by the package not being present in the active environment.

Another common one is ModuleNotFoundError: No module named 'MySQLdb', which appears frequently in database-driven projects. While the root causes vary, the diagnostic approach is always the same — check your Python version and environment first.

Quick Reference: When to Use Each Fix

Here is a quick summary to help you choose the right fix for your situation:

Using Python 3.6 and can upgrade: Upgrade to Python 3.7 or later. This is always the best long-term solution.

Using Python 3.6 and cannot upgrade: Install the backport with pip install dataclasses.

Virtual environment issue: Recreate the virtual environment using Python 3.7 or later.

Anaconda environment: Update the environment with conda install python=3.11 or create a new one.

Docker container: Update the base image in your Dockerfile to python:3.11-slim or newer.

IDE issue: Switch the interpreter in VS Code or PyCharm to point to Python 3.7 or later.

Conclusion

The ModuleNotFoundError: No module named 'dataclasses' error is unique because it is almost always caused by running an outdated Python version rather than a missing third-party package.

The most important thing to remember is that the dataclasses module is built into Python 3.7 and above. If you are running Python 3.7 or later and still see this error, the issue is almost certainly an environment mismatch — your script is being executed by an older Python binary.

The best long-term fix is to upgrade to a modern Python version. If upgrading is not possible right now, the official backport package available via pip install dataclasses gives you full dataclasses support on Python 3.6.

Once the error is resolved, the dataclasses module is an incredibly powerful tool. It eliminates boilerplate code, improves readability, and makes your Python classes cleaner and more expressive with very little effort.