No module named 'pymysql'
Asked Answered
T

20

75

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.

Torrid answered 30/10, 2015 at 23:34 Comment(7)
You ran sudo pip3 install pymysql then tried to import pymysql running python3?Nikethamide
Yup, i'm running it from sudo.Torrid
Odd, works just fine on my system. Try running sudo apt-get install python3-pymysql.Nikethamide
That's it! Thanks mate!Torrid
Answer is found, but It would be nice to learn why this happened. Are you sure that python3 and sudo python3 are the same python?Longoria
These solutions didn't work for me, only Yash Gupta's suggestion (below) worked. I'm running ubuntu 14.04, Django 2.0, Python 3.6.Parkerparkhurst
Same issue with Mac machine. pymysql is installed. but module error when we run python script. apt-get is not supported for mac. Used brew instead of apt-get but brew doesn't have package python3-pymysqlLaurustinus
N
80

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
Nikethamide answered 30/10, 2015 at 23:53 Comment(3)
If the problem still not goes away, try this on debian systems, apt install python-mysqldbSuggestible
Same issue with Mac machine. pymysql is installed. but module error when we run python script. apt-get is not supported for mac. Used brew instead of apt-get but brew doesn't have package python3-pymysqlLaurustinus
This line of code made my day. Thanks a lot!Filmy
L
35

Use:

import pymysql

Not:

import PyMySQL

That works for me.

Lundell answered 16/5, 2018 at 0:4 Comment(4)
This works on Ubuntu 16.04 and Red Hat Enterprise Linux 6.9.Etom
Wish I could up-vote this twice. Such a silly mistake and contrary to any tutorial I've seen on this.Geotaxis
Ow DUDE. You saved my life. Worked on Debian 9Mouflon
This ones the time-saver~Machinist
D
26

After trying a few things, and coming across PyMySQL Github, this worked:

sudo pip install PyMySQL

And to import use:

import pymysql

Deena answered 10/1, 2018 at 4:17 Comment(2)
This did not work on Mac. Install ok - Import not ok.Lines
If you get the error: awscli 1.15.47 has requirement PyYAML<=3.12,>=3.10, but you'll have pyyaml 4.1 which is incompatible. Then you have dependency issues and have to run: apt-get update && apt-get install -y python-pip libpython-dev and pip install awscliPoop
W
9

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.

Warmonger answered 16/12, 2019 at 21:36 Comment(0)
C
8

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
Cimbalom answered 14/6, 2017 at 4:55 Comment(2)
The other solutions offered on this page didn't work for me, only this one. Thanks @Yash Gupta !Parkerparkhurst
Neither this nor the above worked for me, will try a different connectorIndisposed
I
7
  1. Make sure that you're working with the version of Python that think you are. Within Python run import sys and print(sys.version).

  2. 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.
  3. 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.

Indisposed answered 19/3, 2018 at 0:55 Comment(0)
J
3

fwiw, for a conda env:

 conda install -c anaconda pymysql 
Juristic answered 23/11, 2019 at 15:19 Comment(0)
P
2

For windows or one using google colab, you can try this

!pip install pymysql
import pymysql
Presidio answered 30/4, 2020 at 3:17 Comment(0)
L
1

You just need to install:

python3 -m pip install PyMySQL
Lapillus answered 30/3, 2022 at 19:47 Comment(0)
M
1

The following pymysql version worked for me:

pip install pymysql==1.0.2
Manicdepressive answered 17/5, 2022 at 7:43 Comment(0)
F
0

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.

Fer answered 13/4, 2018 at 13:1 Comment(0)
A
0

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.

Aglitter answered 3/5, 2018 at 14:47 Comment(0)
E
0

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.

Eelpout answered 8/7, 2018 at 15:6 Comment(0)
A
0

Just a note: for Anaconda install packages command:

  1. python setup.py install
Arduous answered 31/8, 2019 at 2:1 Comment(0)
O
0

if you are using SPYDER IDE , just try to restart the console or restart the IDE, it works

Onfre answered 17/12, 2019 at 5:47 Comment(0)
C
0

I also got this error recently when using Anaconda on a Mac machine.

Here is what I found:

  1. After running python3 -m pip install PyMySql, pymysql module is under /Library/Python/3.7/site-packages
  2. 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.

Coy answered 1/8, 2020 at 2:49 Comment(0)
H
0

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.

Heuristic answered 1/1, 2021 at 7:31 Comment(0)
C
0

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.

Cinematograph answered 18/9, 2021 at 14:39 Comment(0)
P
0

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)

Philately answered 19/9, 2022 at 2:45 Comment(0)
A
0

When attempting to execute c = connection.cursor() within the Python shell in the context of my Django project, I encountered the same error.

enter image description here

pymysql has already been installed using pip in my environment

enter image description here

I have discovered that it's essential to install the PyMySQL package using the Python interpreter associated with the project's environment. This ensures the package is correctly installed and readily available for import within the project's context.

To verify that you're using the correct Python interpreter, you can follow these steps

enter image description here

In my case, I am using Python 3.12.2, which corresponds to the command: C:\Users\brand\AppData\Local\Programs\Python\Python312\python.exe.

Since I'm using Git Bash in VSCode, the appropriate command to install PyMySQL would be: C:/Users/brand/AppData/Local/Programs/Python/Python312/python.exe -m pip install pymysql. Executing this command in the Git Bash terminal will install the PyMySQL package.

enter image description here

I successfully resolved the issue by directly installing PyMySQL using the Python interpreter.

A Python shell is initiated within a Django project, facilitating the establishment of a database connection. This setup enables interaction with the database through Django's ORM (Object-Relational Mapper).

enter image description here

Alvinaalvine answered 26/2 at 10:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.