No module named 'pymysql'
Question:
I’m trying to use PyMySQL on Ubuntu.
I’ve installed pymysql
using both pip
and pip3
but every time I use import pymysql
, it returns ImportError: No module named 'pymysql'
I’m using Ubuntu 15.10 64-bit and Python 3.5.
The same .py works on Windows with Python 3.5, but not on Ubuntu.
Answers:
Sort of already answered this in the comments, but just so this question has an answer, the problem was resolved through running:
sudo apt-get install python3-pymysql
If even sudo apt-get install python3-pymysql
does not work for you try this:
- Go to the PyMySQL page and download the zip file.
- Then, via the terminal, cd to your Downloads folder and extract
the folder
- cd into the newly extracted folder
- Install the setup.py file with:
sudo python3 setup.py install
After trying a few things, and coming across PyMySQL Github, this worked:
sudo pip install PyMySQL
And to import use:
import pymysql
-
Make sure that you’re working with the version of Python that think you are. Within Python run import sys
and print(sys.version)
.
-
Select the correct package manager to install pymysql with:
- For Python 2.x
sudo pip install pymysql
.
- For Python 3.x
sudo pip3 install pymysql
.
- For either running on Anaconda:
sudo conda install pymysql
.
- If that didn’t work try APT:
sudo apt-get install pymysql
.
-
If all else fails, install the package directly:
- Go to the PyMySQL page and download the zip file.
- Then, via the terminal, cd to your Downloads folder and extract the folder.
- cd into the newly extracted folder.
- Install the setup.py file with:
sudo python3 setup.py install
.
This answer is a compilation of suggestions. Apart from the other ones proposed here, thanks to the comment by @cmaher on this related thread.
sudo apt-get install python3-pymysql
This command also works for me to install the package required for Flask app to tun on ubuntu 16x with WISG module on APACHE2 server.
BY default on WSGI uses python 3 installation of UBUNTU.
Anaconda custom installation won’t work.
I had this same problem just now, and found the reason was my editor (Visual Studio Code) was running against the wrong instance of python; I had it set to run again python bundled with tensorflow, I changed it to my Anaconda python and it worked.
Use:
import pymysql
Not:
import PyMySQL
That works for me.
I ran into the same problem earlier, but solved it in a way slightly different from what we have here. So, I thought I’d add my way as well. Hopefully, it will help someone!
sudo apt-get install mysql-client
didn’t work for me. However, I have Homebrew already installed. So, instead, I tried:
brew install mysql-client
Now, I don’t get the error any more.
Just a note:
for Anaconda install packages command:
- python setup.py install
fwiw, for a conda env:
conda install -c anaconda pymysql
To get around the problem, find out where pymysql is installed.
If for example it is installed in /usr/lib/python3.7/site-packages
, add the following code above the import pymysql
command:
import sys
sys.path.insert(0,"/usr/lib/python3.7/site-packages")
import pymysql
This ensures that your Python program can find where pymysql is installed.
if you are using SPYDER IDE , just try to restart the console or restart the IDE, it works
For windows or one using google colab, you can try this
!pip install pymysql
import pymysql
I also got this error recently when using Anaconda on a Mac machine.
Here is what I found:
- After running
python3 -m pip install PyMySql,
pymysql module is under /Library/Python/3.7/site-packages
- Anaconda wants this module to be under
/opt/anaconda3/lib/python3.8/site-packages
Therefore, after copying pymysql module to the designated path, it runs correctly.
I tried installing pymysql on command prompt by typing
pip install pymysql
But it still dont work on my case, so I decided to try using the terminal IDE and it works.
Another common issue causing the error message to appear is related to conda
environments in jupyter notebook
and jupyter lab
.
After successfully installing a module (pymysql
in this case) in one environment, import
may seem to fail because the environment has not been correctly registered. To solve this it is necessary to manually add kernels for each environment.
Solution and more details can be found here.
You can also visit this lien pip install PyMySQL
You just need to install:
python3 -m pip install PyMySQL
The following pymysql
version worked for me:
pip install pymysql==1.0.2
For anaconda user,
I got this error message.
ModuleNotFoundError: No module named ‘pymysql’
So tried ‘pip install pymysql’ but got below.
Requirement already satisfied: pymysql
What worked for me is,
python file.py (NOT py file.py)
I’m trying to use PyMySQL on Ubuntu.
I’ve installed pymysql
using both pip
and pip3
but every time I use import pymysql
, it returns ImportError: No module named 'pymysql'
I’m using Ubuntu 15.10 64-bit and Python 3.5.
The same .py works on Windows with Python 3.5, but not on Ubuntu.
Sort of already answered this in the comments, but just so this question has an answer, the problem was resolved through running:
sudo apt-get install python3-pymysql
If even sudo apt-get install python3-pymysql
does not work for you try this:
- Go to the PyMySQL page and download the zip file.
- Then, via the terminal, cd to your Downloads folder and extract
the folder - cd into the newly extracted folder
- Install the setup.py file with:
sudo python3 setup.py install
After trying a few things, and coming across PyMySQL Github, this worked:
sudo pip install PyMySQL
And to import use:
import pymysql
-
Make sure that you’re working with the version of Python that think you are. Within Python run
import sys
andprint(sys.version)
. -
Select the correct package manager to install pymysql with:
- For Python 2.x
sudo pip install pymysql
. - For Python 3.x
sudo pip3 install pymysql
. - For either running on Anaconda:
sudo conda install pymysql
. - If that didn’t work try APT:
sudo apt-get install pymysql
.
- For Python 2.x
-
If all else fails, install the package directly:
- Go to the PyMySQL page and download the zip file.
- Then, via the terminal, cd to your Downloads folder and extract the folder.
- cd into the newly extracted folder.
- Install the setup.py file with:
sudo python3 setup.py install
.
This answer is a compilation of suggestions. Apart from the other ones proposed here, thanks to the comment by @cmaher on this related thread.
sudo apt-get install python3-pymysql
This command also works for me to install the package required for Flask app to tun on ubuntu 16x with WISG module on APACHE2 server.
BY default on WSGI uses python 3 installation of UBUNTU.
Anaconda custom installation won’t work.
I had this same problem just now, and found the reason was my editor (Visual Studio Code) was running against the wrong instance of python; I had it set to run again python bundled with tensorflow, I changed it to my Anaconda python and it worked.
Use:
import pymysql
Not:
import PyMySQL
That works for me.
I ran into the same problem earlier, but solved it in a way slightly different from what we have here. So, I thought I’d add my way as well. Hopefully, it will help someone!
sudo apt-get install mysql-client
didn’t work for me. However, I have Homebrew already installed. So, instead, I tried:
brew install mysql-client
Now, I don’t get the error any more.
Just a note:
for Anaconda install packages command:
- python setup.py install
fwiw, for a conda env:
conda install -c anaconda pymysql
To get around the problem, find out where pymysql is installed.
If for example it is installed in /usr/lib/python3.7/site-packages
, add the following code above the import pymysql
command:
import sys
sys.path.insert(0,"/usr/lib/python3.7/site-packages")
import pymysql
This ensures that your Python program can find where pymysql is installed.
if you are using SPYDER IDE , just try to restart the console or restart the IDE, it works
For windows or one using google colab, you can try this
!pip install pymysql
import pymysql
I also got this error recently when using Anaconda on a Mac machine.
Here is what I found:
- After running
python3 -m pip install PyMySql,
pymysql module is under/Library/Python/3.7/site-packages
- Anaconda wants this module to be under
/opt/anaconda3/lib/python3.8/site-packages
Therefore, after copying pymysql module to the designated path, it runs correctly.
I tried installing pymysql on command prompt by typing
pip install pymysql
But it still dont work on my case, so I decided to try using the terminal IDE and it works.
Another common issue causing the error message to appear is related to conda
environments in jupyter notebook
and jupyter lab
.
After successfully installing a module (pymysql
in this case) in one environment, import
may seem to fail because the environment has not been correctly registered. To solve this it is necessary to manually add kernels for each environment.
Solution and more details can be found here.
You can also visit this lien pip install PyMySQL
You just need to install:
python3 -m pip install PyMySQL
The following pymysql
version worked for me:
pip install pymysql==1.0.2
For anaconda user,
I got this error message.
ModuleNotFoundError: No module named ‘pymysql’
So tried ‘pip install pymysql’ but got below.
Requirement already satisfied: pymysql
What worked for me is,
python file.py (NOT py file.py)