Last updated: Apr 10, 2024
Reading timeยท2 min
The "ModuleNotFoundError: No module named 'ConfigParser'" occurs because the
MySQL-python
package does not support Python 3.
To solve the error, install the mysqlclient
by running
pip install mysqlclient
.
The mysqlclient module is a drop-in
replacement for MySQL-python
and supports Python 3.
Open your terminal in your project's root directory and install the module with the following command.
# ๐๏ธ In a virtual environment pip install mysqlclient # ๐๏ธ For Python 3 (could also be pip3.10 depending on your version) pip3 install mysqlclient
If pip
is not set up in your PATH environment variable, you might have to try
one of the python -m pip
commands.
# ๐๏ธ If you don't have pip in your PATH environment variable python -m pip install mysqlclient # ๐๏ธ For Python 3 (could also be pip3.10 depending on your version) python3 -m pip install mysqlclient # ๐๏ธ Using py alias (Windows) py -m pip install mysqlclient
If you get a permissions error, try running the command with the --user
option
or prefix it with sudo
on macOS and Linux.
# ๐๏ธ If you get a permissions error pip install mysqlclient --user pip3 install mysqlclient --user # ๐๏ธ On macOS or Linux sudo pip install mysqlclient sudo pip3 install mysqlclient
If the error persists, try to install the mysqlclient
package with an
operating system-specific command.
# ๐๏ธ For macOS brew install mysql pip install mysqlclient # ๐๏ธ For Debian/Ubuntu sudo apt-get install python3-dev default-libmysqlclient-dev build-essential pip install mysqlclient # ๐๏ธ For RedHat/CentOS sudo yum install python3-devel mysql-devel pip install mysqlclient
If you use Anaconda, use the following command to install mysqlclient
.
# ๐๏ธ For Anaconda conda install -c conda-forge mysqlclient
# ๐๏ธ For Jupyter Notebook !pip install mysqlclient
The mysqlclient module is a fork of
the unmaintained MySQL-python
package and adds support for Python 3.
configparser
module from six.moves
If the error persists, try to import the configparser
module from six.moves
.
First, make sure you have the six module installed by running the following command.
pip install six # ๐๏ธ Or with pip3 pip3 install six
Now you can import the configparser
module from the six.moves
module.
from six.moves import configparser print(configparser)
Alternatively, you can use the built-in configparser module.
The module provides the ConfigParser class.
Here is an example from the docs.
import configparser sample_config = """ [mysqld] user = mysql pid-file = /var/run/mysqld/mysqld.pid skip-external-locking old_passwords = 1 skip-bdb # we don't need ACID today skip-innodb """ config = configparser.ConfigParser(allow_no_value=True) config.read_string(sample_config) print(config["mysqld"]["user"]) # ๐๏ธ mysql print(config["mysqld"]["skip-bdb"]) # ๐๏ธ None
The ConfigParser
class implements a basic configuration language that provides
a structure similar to that in Windows INI files.
ConfigParser classes are basically used to read and write INI files.
The official docs page has many examples of the supported INI file structure.