Win10系统Python3.6环境下,如何解决‘No module named pymysql’报错?
Hey there, let's work through why you're hitting those frustrating "module not found" errors even after installing the packages. Here are the most common fixes to try out:
1. Ensure you're installing packages for your actual Python 3.6 instance
Windows often has multiple Python versions floating around (system defaults, Anaconda, separate 3.6/3.9 installs, etc.). It’s super easy to install packages to one version but run your code with another.
- Open Command Prompt and run
python --versionto confirm which Python your terminal is using. If it’s not 3.6, target your Python 3.6 directly:- Use the full path to your Python 3.6 executable (e.g.,
C:\Python36\python.exe -m pip install pymysql) - Or use the Python Launcher for Windows:
py -3.6 -m pip install pymysql(this works if you enabled the launcher during Python 3.6 installation)
- Use the full path to your Python 3.6 executable (e.g.,
- Verify the installation worked by running
py -3.6 -c "import pymysql; print('Success!')"— if no error pops up, the module is installed correctly for 3.6.
2. Fix MySQLdb compatibility for Python 3.6
Important note: MySQLdb is a Python 2-only library — it does NOT support Python 3.x. Any "MySQLdb" installation you see likely isn’t the real thing, or won’t work properly with your 3.6 setup. Here’s how to fix this:
- If your code uses
import MySQLdb, replace it withpymysqland add this line at the top of your script to make pymysql act as a drop-in replacement for MySQLdb:import pymysql pymysql.install_as_MySQLdb() - Alternatively, install
mysqlclient— the official Python 3-compatible successor to MySQLdb. For Windows 10 + Python 3.6, you’ll need a pre-built wheel file (compiling from source is tricky):- Grab the correct
.whlfile (matching your system’s 32/64-bit architecture and Python 3.6) from the unofficial Windows binaries repository - Install it via pip:
py -3.6 -m pip install path\to\mysqlclient-xxx.whl
- Grab the correct
3. Check your IDE’s interpreter configuration
If you’re using an IDE like PyCharm or VS Code, it might be using a different Python interpreter than your terminal:
- In PyCharm: Go to File > Settings > Project: [Your Project] > Python Interpreter. Make sure the selected interpreter is Python 3.6, and check that
pymysql(ormysqlclient) appears in the installed packages list. If not, click the+button to install it directly in the IDE. - In VS Code: Open the command palette (
Ctrl+Shift+P), select Python: Select Interpreter, and choose your Python 3.6 installation. Then runpip install pymysqlin the integrated terminal (it will use the selected interpreter).
4. Double-check pip’s target version
Run pip --version in your terminal. The output should show it’s associated with Python 3.6 (e.g., pip 21.3.1 from C:\Python36\lib\site-packages\pip (python 3.6)). If it points to a different Python version, use python3.6 -m pip instead of plain pip to install packages.
Give these steps a shot — one of them should get your modules working properly!
内容的提问来源于stack exchange,提问作者SuYeon Choi

