How to Fix No module named ‘pymysql’

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'

or

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

Linux Users:

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.

import pymysql

Note: 

  1. If you are using Python 3, replace pip with pip3 in the above commands.
  2. 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

or

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.

cd Downloads

 

5. Extract the contents of the folder.

tar -xf PyMySQL-main.zip

 

6. Get into the folder.

cd PyMySQL-main

 

7. Install the setup.py file using the following command.

python setup.py install
Note: If you are using Python3, change python to python3 

 

Conclusion

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.

If you enjoyed reading, share this article.

Anusha Pai is a Software Engineer having a long experience in the IT industry and having a passion to write. She has a keen interest in writing Python Errorfixes, Solutions, and Tutorials.

Leave a Comment