The error No module named ‘pymysql’ occurs when you import the PyMySQL package. The installation of the PyMySQL package will be successful. However, when you try to import, you see one of the following errors :
Import Error: No module named 'pymysql'
ModuleNotFoundError: No module named 'pymysql'
Firstly, let’s understand why this error is seen.
What causes the No module named ‘pymysql’ error?
If you have installed the pymysql library using pip in the command prompt, the library would be located in, C:\Users\%username%\AppData\Local\Programs\Python\Python310\Lib\site-packages
If you are using some IDE or using a venv (Virtual environment), the library won’t be accessible as IDE fetches the libraries from the venv, and there is no library named pymysql in the venv, resulting in the No module named ‘pymysql’
The error can also be seen if you have installed the ‘pymysql’ library in some custom location and your program checks for the package elsewhere.
To solve this error, check where the package/library/module is installed. That is,
- Check the version of Python you are using.
- Verify if the library is installed globally or is local to some environment.
- Ensure your program can access the package.
Now, let us discuss various ways to fix this issue.
Fix 1: Install and import the library using the right commands.
1. Determine the version of Python you are using.
2. Based on the version of python in the system, install the ‘pymysql’ using one of the following :
Windows Users :
pip install pymysql
pip install pymysql
If this doesn’t work, try installing the package globally using the below command.
sudo pip install pymysql
If pip commands aren’t working, try using apt-
sudo apt-get install pymysql
After installing the package successfully, import the package using the below command.
- If you are using Python 3, replace pip with pip3 in the above commands.
- pip is not case sensitive. So, using PyMySQL or pymysql will cause no harm. On the other hand, Python statements are case-sensitive. Make sure you use pymysql in the import statement.
Fix 2: Ensure the program can access the package.
1. Determine where the PyMySQL package is installed.
For demonstration purposes, let’s say it is located in the below location. C:\Users\%username%\AppData\Local\Programs\Python\Python310\Lib\site-packages
2. Add this location to PYTHONPATH by using the below command before calling the import statement as shown below
import sys sys.path.insert(0,"C:\\Users\\%username%\\AppData\\Local\\Programs\\Python\\Python310\\Lib\\site-packages\\") import pymysql
import sys sys.path.append("C:\\Users\\%username%\\AppData\\Local\\Programs\\Python\\Python310\\Lib\\site-packages\\") import pymysql
Fix 3: Install the package directly.
1. Open the PyMySQL Page
2. Click on the Code. From the drop-down, choose Download ZIP.
4. Now, open your Command Prompt and navigate to the Downloads folder as shown below.
5. Extract the contents of the folder.
tar -xf PyMySQL-main.zip
6. Get into the folder.
7. Install the setup.py file using the following command.
python setup.py install
In this article, we have discussed what causes the error, “No module named ‘pymysql'” and we have also discussed different ways to fix the error.
We hope this article has been informative. Thank you for reading. Kindly comment and let us know if you found it helpful.