Select Page

How to Solve Python ModuleNotFoundError: No module named ‘ConfigParser’

by | Programming, Python, Tips

This error can occur if you are trying to import a package not supported by Python 3. In Python 3, ConfigParser has been renamed configparser, so any Python 2 packages using ConfigParser will throw the ModuleNotFoundError.

To solve this error, you can use the Python 3 equivalent package, for example, mysqlclient, instead of MySQL-python. Otherwise, if you are directly importing the configparser module, you can import it using:

import configparser

This tutorial will go through the error in detail with code examples.


Example #1: Installing MySQL-python

Let’s look at an example where we want to install MySQL-python using pip. The version of Python we will use is Python 3.

python -VV

Let’s try to install MySQL-python:

python3 -m pip install MySQL-python
      File "/private/var/folders/bt/nx2lz4mx4z33rgbhbdt750pr0000gq/T/pip-install-k8uyreo7/mysql-python_ce1e9d5d31b74a89869bf286b41ee6c6/setup_posix.py", line 2, in <module>
        from ConfigParser import SafeConfigParser
    ModuleNotFoundError: No module named 'ConfigParser'

We get the ModuleNotFoundError because MySQL-python is only supported from Python 2.4 to 2.7. As MySQL-python is a Python 2 module, it tries to import ConfigParser. In Python 2, ConfigParser was renamed configparser in Python 3.

Solution

To solve this error, we can install mysqlclient instead of MySQL-python. Let’s look at the revised code:

python3 -m pip install mysqlclient
Successfully built mysqlclient
Installing collected packages: mysqlclient
Successfully installed mysqlclient-2.1.0

Note that you may have to install other packages relevant to mysqlclient before installing it, which you can find under the full installation documentation.

Example #2: Importing ConfigParser Instead of configparser

Generally, if you want to import configparser in Python 3, you need to import configparser instead of ConfigParser. We can then create a ConfigParser object using ConfigParser().

Let’s look at an example where we want to create a basic configuration file programmatically.

import ConfigParser
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Input In [1], in <cell line: 1>()
----> 1 import ConfigParser

ModuleNotFoundError: No module named 'ConfigParser'

Solution

Importing ConfigParser does not work, we have to import configparser. Let’s look at the revised code:

import configparser

# Create a ConfigParser object to write ini file

config = configparser.ConfigParser()

config['DEFAULT'] = {'ServerAliveInterval': '45',
                    'Compression': 'yes',
                      'CompressionLevel': '9'}
config['bitbucket.org'] = {}
config['bitbucket.org']['User'] = 'ab'
config['supersecret.server.com'] = {}
supersecret = config['supersecret.server.com']
supersecret['Port'] = '50022'     # mutates the parser
supersecret['ForwardX11'] = 'no'  # same here
config['DEFAULT']['ForwardX11'] = 'yes'
with open('example.ini', 'w') as configfile:
    config.write(configfile)

When we run the above code, we will get an example.ini file in our working directory that looks like this:

[DEFAULT]
serveraliveinterval = 45
compression = yes
compressionlevel = 9
forwardx11 = yes

[bitbucket.org]
user = ab

[supersecret.server.com]
port = 50022
forwardx11 = no

Summary

Congratulations on reading to the end of this tutorial! The ModuleNotFoundError: No module named ‘ConfigParser’ occurs when attempting to use the outdated naming for the configparser package. In Python 3, ConfigParser has been renamed to configparser. Ensure that the package you want to install that depends on configparser supports Python 3. You can check package documentation for deprecation notices and Python 3 supported alternatives.

For further reading on MySQL, go to the article: How to Install MySQL on Mac.

Have fun and happy researching!