Python 3
ImportError
ConfigParser
ModuleNotFoundError
Python Error

Python 3 ImportError No module named 'ConfigParser'

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

This error happens when Python 2 style import code is executed in Python 3. The old module name ConfigParser was renamed to lowercase configparser in Python 3. Most fixes are simple, but environment mismatches can make the problem appear unresolved even after changing imports.

Core Sections

1. Understand the module rename

Python 2 code often contains:

python
import ConfigParser

Python 3 requires:

python
import configparser

The class name is still ConfigParser, but the module name changed. That distinction causes confusion in older codebases.

2. Correct Python 3 usage pattern

Basic config parsing example:

python
1import configparser
2
3cfg = configparser.ConfigParser()
4cfg.read("app.ini")
5
6host = cfg.get("database", "host", fallback="localhost")
7port = cfg.getint("database", "port", fallback=5432)
8print(host, port)

If your project targets Python 3 only, this should be your default pattern everywhere.

3. Compatibility shim for mixed-runtime code

If legacy code still needs Python 2 and 3 support, centralize compatibility import once.

python
1try:
2    import configparser as configparser_module
3except ImportError:
4    import ConfigParser as configparser_module
5
6
7def load_config(path):
8    parser = configparser_module.ConfigParser()
9    parser.read(path)
10    return parser

Centralizing compatibility avoids scattered conditional imports across many files.

4. Confirm interpreter and executable path

A common issue is fixing code but running with an unexpected interpreter.

bash
1python - <<'PY'
2import sys
3print(sys.version)
4print(sys.executable)
5PY

If this shows Python 2, your import change may still fail in runtime scripts launched by old shebangs or scheduler configs.

5. Check for module shadowing

Local files can shadow standard library modules. A project file named configparser.py can break imports unexpectedly.

bash
find . -maxdepth 2 -name 'configparser.py'

Rename conflicting files and clear bytecode caches if needed.

6. Virtual environment consistency

In multi-project machines, interpreter confusion is common. Use virtual environments and run scripts with explicit interpreter.

bash
python3 -m venv .venv
source .venv/bin/activate
python -c "import configparser; print(configparser.__file__)"

This ensures import behavior is tied to your intended runtime.

7. Migration checklist for old codebases

When modernizing legacy code, include:

  1. replace ConfigParser imports
  2. update deprecated Python 2 syntax nearby
  3. run test suite under target Python 3 versions
  4. verify deployment runtime images

Fixing one import line without full runtime checks often leaves hidden migration issues.

8. Add CI guard for future regressions

Include a small test that confirms interpreter major version and module import availability.

python
1import importlib
2import sys
3
4
5def test_configparser_available():
6    assert sys.version_info.major == 3
7    module = importlib.import_module("configparser")
8    assert hasattr(module, "ConfigParser")

This prevents accidental reintroduction of Python 2 assumptions.

Sometimes the initial error message is followed by additional import problems. Treat each one explicitly rather than trying random package installs. configparser in Python 3 is standard library, so pip-installing unrelated packages usually does not solve root cause.

10. Keep launcher shebangs aligned with Python 3

Legacy scripts may still start with Python 2 shebang lines. Even with correct imports in source code, old shebangs can force wrong runtime at execution time. Update script headers to a Python 3 launcher and ensure scheduler or cron jobs call the same interpreter used in local testing.

Common Pitfalls

  • Updating import statement but still running Python 2 interpreter.
  • Confusing module rename with class rename.
  • Creating local files that shadow configparser module.
  • Applying compatibility hacks in many files instead of one shared module.
  • Assuming missing module always means missing pip dependency.

Summary

  • Python 3 uses configparser module name, not ConfigParser.
  • Most fixes are import updates plus interpreter verification.
  • Use compatibility shim only when truly supporting mixed runtimes.
  • Validate environment path and module shadowing before deeper debugging.
  • Add CI checks so import assumptions stay correct over time.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.