MacBook Pro下Python连接MySQL时pip install mysql报错求助
ModuleNotFoundError: No module named 'ConfigParser' when installing MySQL for Python on MacBook Pro Hey there, let's break down what's going on and fix this issue quickly:
Why you're seeing this error
The mysql package you're trying to install (pip install mysql) is the old MySQL-python library, built exclusively for Python 2. In Python 3, the module name changed from ConfigParser to configparser (lowercase), so this outdated package throws an error when trying to run on your Python 3 environment.
Solutions to get connected to MySQL
Here are the two most reliable ways to set up a Python 3-compatible MySQL connection on your MacBook Pro:
1. Use mysqlclient (Recommended, drop-in replacement for MySQL-python)
This is the official Python 3 fork of MySQL-python, and it works seamlessly with most code that expects the old library.
- First, install the required system dependencies using Homebrew:
brew install mysql-client - If you get a "mysql_config not found" error during installation, add the mysql-client bin directory to your PATH temporarily:
export PATH="/usr/local/opt/mysql-client/bin:$PATH" - Now install the Python package:
pip install mysqlclient - Note: If you hit compilation errors, make sure you have Xcode Command Line Tools installed:
xcode-select --install
2. Use pymysql (Pure-Python, no compilation needed)
If you prefer a library that doesn't require compiling system-level code, pymysql is a great alternative.
- Install the package:
pip install pymysql - If your code was written to use the old
MySQLdbmodule (from MySQL-python), add this at the top of your script to make pymysql act as a drop-in replacement:import pymysql pymysql.install_as_MySQLdb()
Important Note
Never use pip install mysql for Python 3 projects — that package is abandoned and incompatible with modern Python versions. Stick to either mysqlclient or pymysql instead.
内容的提问来源于stack exchange,提问作者tmcdo13

