How to Resolve Python "ModuleNotFoundError: No module named 'ConfigParser'"
The ModuleNotFoundError: No module named 'ConfigParser'
typically arises when running Python 3 code that attempts to import a module named ConfigParser
with a capital 'C' and 'P'. This happens because the module for handling configuration files was renamed in Python 3. Older code or tutorials written for Python 2, or sometimes older third-party libraries, might use the outdated name.
This guide explains the module renaming and provides solutions for updating your code or handling incompatible libraries.
Understanding the Error: Python 2 vs. Python 3 Renaming
Python 2 had a standard library module named ConfigParser
for reading and writing configuration files similar to Windows INI files.
In Python 3, as part of various standard library reorganizations and naming convention updates, this module was renamed to configparser
(all lowercase). The functionality remains largely the same, but the import name changed.
Therefore, trying to import ConfigParser
or access ConfigParser.SomeClass
in a Python 3 environment will fail because that specific name no longer exists in the standard library structure.
Cause 1: Using the Python 2 Name (ConfigParser
) in Python 3 Code
The most direct cause is having code like this running under a Python 3 interpreter:
# Error Scenario (running with Python 3)
try:
import ConfigParser # Using the Python 2 capitalized name
config = ConfigParser.ConfigParser()
# ... rest of the code ...
except ModuleNotFoundError as e:
# ⛔️ ModuleNotFoundError: No module named 'ConfigParser'
print(e)
This fails because Python 3's standard library does not contain a top-level module named ConfigParser
.
Solution 1: Use the Python 3 Name (configparser
) (Recommended)
If you are writing or maintaining Python 3 code, the correct solution is to use the lowercase module name configparser
.
# ✅ Corrected Code (for Python 3)
import configparser # Use the lowercase name
# Example Usage
sample_config = """
[Section1]
option1 = value1
option2 = true
[Section2]
path = /usr/local/bin
"""
config = configparser.ConfigParser()
config.read_string(sample_config)
print(f"Sections found: {config.sections()}") # Output: Sections found: ['Section1', 'Section2']
print(f"Option 1: {config['Section1']['option1']}") # Output: Option 1: value1
print(f"Path: {config.get('Section2', 'path')}") # Output: Path: /usr/local/bin
Simply changing the import statement and subsequent references to use the lowercase configparser
resolves the error for code directly under your control.
Cause 2: Using Outdated Libraries (e.g., MySQL-python
)
Sometimes, the ModuleNotFoundError: No module named 'ConfigParser'
doesn't come directly from your code but from a third-party library you are using. If that library was originally written for Python 2 and hasn't been updated or isn't fully compatible with Python 3, it might internally still contain import ConfigParser
statements.
A classic example mentioned in the original prompt is the old, unmaintained MySQL-python
library. It does not support Python 3 and contains Python 2-style imports. When you try to use it with a Python 3 interpreter, it fails internally when trying to import ConfigParser
.
Solution 2: Update/Replace the Incompatible Library (e.g., mysqlclient
)
If the error originates from a third-party library:
- Identify the Library: The error traceback should indicate the file within the library causing the
ModuleNotFoundError
. - Find a Python 3 Compatible Version/Alternative: Search for actively maintained versions or forks of the library that support Python 3.
- Install the Compatible Version: Uninstall the old library and install the new one.
-
Example: Replacing
MySQL-python
withmysqlclient
Themysqlclient
package is a commonly used, actively maintained fork ofMySQL-python
that supports Python 3.# Uninstall the old one if present (optional, but good practice)
pip uninstall MySQL-python
# Install the compatible fork
pip install mysqlclient
# Or using python -m pip
python -m pip install mysqlclient
# Or using conda
conda install -c conda-forge mysqlclientAfter installing
mysqlclient
, code that previously failed due toMySQL-python
's internalConfigParser
import should now work (assumingmysqlclient
successfully replacedMySQL-python
for your database connection needs). -
General Library Updates: For other libraries, try updating them to their latest versions:
pip install --upgrade name_of_library
Check the library's documentation for Python 3 compatibility notes.
Solution 3: Using Compatibility Layers (six.moves
)
For codebases that need to support both Python 2 and Python 3, the six
library provides compatibility utilities. The six.moves
submodule includes aliases that point to the correct module name depending on the Python version being run.
First, install six
:
pip install six
Then, import configparser
via six.moves
:
# Code intended to run on both Python 2 and Python 3
try:
from six.moves import configparser
print("Imported configparser via six.moves")
config = configparser.ConfigParser()
# ... use config ...
except ImportError:
print("Could not import configparser via six.moves (is six installed?)")
This approach is primarily useful when maintaining code that must explicitly run across Python 2 and 3 versions and is already using the six
library. For purely Python 3 projects, using the direct import configparser
is preferred.
Debugging the Import Location
The traceback associated with the ModuleNotFoundError
is crucial. It will show the exact file and line number where the problematic import ConfigParser
statement exists. This tells you whether the issue is in your own code (requiring Solution 1) or within an installed third-party library (requiring Solution 2).
Conclusion
The ModuleNotFoundError: No module named 'ConfigParser'
in a Python 3 environment primarily occurs because:
- The module was renamed: The Python 2
ConfigParser
module is calledconfigparser
(lowercase) in Python 3. Update yourimport
statements accordingly. - An outdated library is being used: A third-party package written for Python 2 might still internally try to
import ConfigParser
. Find and install a Python 3-compatible version or fork of that library (e.g., usemysqlclient
instead ofMySQL-python
).
Using the correct lowercase name (configparser
) in your Python 3 code and ensuring your dependencies are Python 3 compatible are the standard ways to resolve this error.