How to Fix ImportError in Python [Beginner Guide]



You tried to run your Python script and got an “ImportError” — maybe “cannot import name” or “DLL load failed.”

This error means Python found the module (package of code) you asked for, but something went wrong when trying to load it. It’s a close cousin of ModuleNotFoundError, but the cause is slightly different.

Don’t worry, this is very common. This guide explains why ImportError happens and how to fix it step by step.

Why ImportError happens in Python

ImportError has several common causes:

  • You’re importing a name that doesn’t exist in the module: For example, trying to import a function that was renamed or doesn’t exist
  • Circular imports: Two files try to import from each other, creating a loop
  • Version mismatch: The installed version of the library doesn’t have the feature you’re trying to use

Solution 1: Check the import name (most common fix)

The most frequent cause is trying to import something that doesn’t exist in the module.

1. Read the error message carefully

# Example error
ImportError: cannot import name 'Markup' from 'jinja2'
# → The name 'Markup' doesn't exist in the current version of jinja2

The error tells you exactly which name failed and from which module.

2. Verify the correct import name

# Check what's available in the module
import jinja2
print(dir(jinja2))

This prints a list of all names you can import from that module. Check if the name you want is in the list. If it’s not there, the function may have been moved or renamed in a newer version.

3. Check the library’s documentation for the correct import

# Example: the name may have moved to a different submodule
# Old way (might cause ImportError in newer versions)
from jinja2 import Markup

# New way
from markupsafe import Markup

If the import works without errors, you’ve fixed it.

Solution 2: Reinstall or update the library

If the import name is correct but still fails, the library installation might be corrupted or outdated.

1. Uninstall and reinstall the library

# Uninstall first
pip uninstall jinja2 -y

# Then reinstall
pip install jinja2

If you see “Successfully installed”, the reinstall worked.

2. If you need a specific version, pin it

# Install a specific version that has the feature you need
pip install jinja2==3.0.3

Check the library’s changelog or documentation to find which version includes the function you need. Run your code again to confirm the error is gone.

Solution 3: Fix circular import issues

If the error happens in your own code (not a third-party library), you might have a circular import — two files importing from each other.

1. Identify the circular dependency

# file: a.py
from b import function_b  # a imports from b

# file: b.py
from a import function_a  # b imports from a ← circular!

2. Move the import inside the function that needs it

# file: b.py — fix by importing inside the function
def my_function():
    from a import function_a  # Import here instead of at the top
    function_a()

Moving the import inside the function breaks the circular dependency. If your code runs without ImportError, the fix worked.

3. Another option: restructure your code

# Move shared code into a third file (c.py)
# Then have both a.py and b.py import from c.py instead of each other

If nothing works

If the above solutions don’t help, try these:

  • Make sure your file name doesn’t shadow a library: If you named your file something like random.py or email.py, Python will try to import your file instead of the built-in module. Rename your file to something unique
  • Use a virtual environment: This gives you a clean slate with no conflicting packages
# Create a fresh virtual environment
python -m venv myenv

# Activate it (Windows)
myenv\Scripts\activate

# Activate it (Mac)
source myenv/bin/activate

# Install what you need and run your code
pip install jinja2
python your_script.py
  • Tip for asking for help: Include the full error traceback, your Python version (python --version), and the library version (pip show library_name)

Summary

  • ImportError usually means you’re importing a name that doesn’t exist in the module — check spelling and documentation
  • Reinstalling or updating the library often fixes version-related ImportErrors
  • If the error is in your own code, check for circular imports or file name conflicts with built-in modules

Related articles:

  • module-not-found-error.html (How to fix ModuleNotFoundError)
  • pip-install-error.html (How to fix pip install errors)