1

I'm trying to add 'PyMySQL, but it keeps throwing

Traceback (most recent call last):
  File "/home/john/workspace/Python/sql/sql.py", line 1, in <module>
    import PyMySQL
ImportError: No module named 'PyMySQL'

I tried installing it via pip and running it again, but the error doesnt go away. Any ideas on what is wrong ?

john@john-PC:$ sudo pip install PyMySQL
The directory '/home/john/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/home/john/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting PyMySQL
/usr/local/lib/python2.7/dist-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:318: SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject Name Indication) extension to TLS is not available on this platform. This may cause the server to present an incorrect TLS certificate, which can cause validation failures. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#snimissingwarning.
  SNIMissingWarning
/usr/local/lib/python2.7/dist-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:122: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
  InsecurePlatformWarning
  Downloading PyMySQL-0.7.9-py2-none-any.whl (78kB)
    100% |████████████████████████████████| 81kB 1.3MB/s 
Installing collected packages: PyMySQL
Successfully installed PyMySQL-0.7.9

john@john-PC:$ pip install PyMySQL
Requirement already satisfied (use --upgrade to upgrade): PyMySQL in /usr/local/lib/python2.7/dist-packages

john@john-PC:$ sudo apt-get install python3-pymysql
Reading package lists... Done
Building dependency tree       
Reading state information... Done
python3-pymysql is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 56 not upgraded.

sql.py

import PyMySQL
print("Here")

Solution. It was a combination of two answers below, plus a bug report online.

apt-get install software-properties-common
add-apt-repository cloud-archive:mitaka
apt-get update && apt-get dist-upgrade 
sudo apt-get install python3-pymysql
import pymysql
AJ_
  • 3,787
  • 10
  • 47
  • 82
  • how do you run your script? – Loïc Oct 23 '16 at 20:16
  • @Loïc "/usr/bin/python3 sql.py" – AJ_ Oct 23 '16 at 20:22
  • Python 3.4.3 (default, Sep 14 2016, 12:36:27) [GCC 4.8.4] on linux Type "help", "copyright", "credits" or "license" for more information. >>> pymysql Traceback (most recent call last): File "", line 1, in NameError: name 'pymysql' is not defined >>> – AJ_ Oct 23 '16 at 20:35

8 Answers8

3

Try running

sudo apt-get install python3-pymysql

for python3.

Already answered here at stackoverflow.com

Bora
  • 1,402
  • 16
  • 19
warl0ck
  • 3,356
  • 4
  • 27
  • 57
  • sudo apt-get install python3-pymysql Reading package lists... Done Building dependency tree Reading state information... Done E: Unable to locate package python3-pymysql – AJ_ Oct 23 '16 at 20:46
  • 1
    I had to run some upgrades/and other libs. Then change the name to pymysql – AJ_ Oct 23 '16 at 21:00
  • Thanks @John, my issue was also using uppercase letters – Levi Dec 14 '16 at 20:33
2

After installed "PyMySQL",you also need update the _init__.py,adding import pymysql
pymysql.install_as_MySQLdb()
and replace MySQLdb as pymysql in the base.py.

aMJay
  • 2,215
  • 6
  • 22
  • 34
panzd
  • 21
  • 4
0

I had this same problem recently. And I solved it using this:

sudo pip-3.2 install pymysql**3**

That 3 in the pymysql3 made all the difference.

It was on a raspberry pi with wheezy

Kenzo_Gilead
  • 2,187
  • 9
  • 35
  • 60
0

The reason it is throwing the error, even after installing the correct packages is because of the name that is being used. It should be "import pymysql" instead of "import PyMySQL". This worked for me.

abhishek kumar
  • 339
  • 3
  • 11
0

Using Debian 9 & calling python3 from BASH.

import pymysql

worked for me

import PyMySql

failed with

Traceback (most recent call last): File "", line 1, in NameError: name 'PyMySQL' is not defined

0

This worked for me:

#!/usr/bin/env python3

#Importing needed modules of PyMySQL
from pymysql import connect, err, sys, cursors

#Doing our connection
conn = connect( host = 'localhost',

cursor = conn.cursor( cursors.DictCursor );
Artemis
  • 2,553
  • 7
  • 21
  • 36
0

I found that if I used PyCharm's Interpreter Add Package GUI then my computer could find pymysql.

I only had this issue when using VSCode.

thunt
  • 89
  • 1
  • 11
0

Use pip or pipenv to install the PyMySQL dependency:

pip install PyMySQL

OR

pipenv install PyMySQL

Then add this to your settings.py (django) file:

import pymysql
pymysql.install_as_MySQLdb()

That's all! This worked for me.

omeatai
  • 1,013
  • 8
  • 8