ModuleNotFoundError - PyMySQL for python 3
Asked Answered
A

2

-1

I am trying to get a simple test program working on my machine that connects to a SQL DB. I pip installed and then uninstalled and then installed with pip3: pymysql. the issue I'm getting:

import PyMySQL ModuleNotFoundError: No module named 'PyMySQL'.

it can be found when i run the command pip list, but not found when running the program itself. I was perusing over other SO Q&A's but nothing helped. Thanks

Acrodrome answered 6/2, 2018 at 16:29 Comment(2)
Exactly which version of Python are you using? 3.X?Muttonhead
I am using 3.6.4 @MuttonheadAcrodrome
H
3

First insall PyMySQL using:

pip install PyMySQL

In python 3 it is pymysql, all in lower case

import pymysql
Howlond answered 10/2, 2018 at 21:17 Comment(0)
W
0

Module names are case-sensitive, and if you take a look at this example in the GitHub repo, you'll see that the module should be imported as pymysql, which is consistent with the all-lowercase convention for modules spelled out in the PEP 8 style guide.

import pymysql.cursors

# Connect to the database
connection = pymysql.connect(host='localhost',
                             user='user',
                             password='passwd',
                             db='db',
                             charset='utf8mb4',
                             cursorclass=pymysql.cursors.DictCursor)
# ...
Walton answered 6/2, 2018 at 16:48 Comment(7)
Here is the result: File "capstonetest.py", line 2, in <module> import pymysql ModuleNotFoundError: No module named 'pymysql' Acrodrome
@Acrodrome could you please double-check that you have pymysql module installed, not pymssql?Muttonhead
Instead of trying to import the module, try importing sys, and then print(sys.version) and print([key for key in sys.modules.keys() if 'py' in key.lower()]). The first will help confirm that you're running the program with the expected version of python; the second should give you a list showing that pymysql is available as a module.Walton
@Muttonhead The pip list states that PyMySQL (0.8.0) is installed. i got it working in anaconda though.Acrodrome
@Walton it gives me an empty array :( this is the output: 3.6.3 |Anaconda custom (64-bit)| (default, Oct 15 2017, 03:27:45) [MSC v.1900 64 bit (AMD64)] []Acrodrome
@Acrodrome I imagine that the problem is that you used pip to install PyMySQL, and are then running your program with an Anaconda distribution of python. Read this answer for more info on the difference. Anaconda is not looking for modules using the same path that pip uses when installing them, so if you want to run your program with Anaconda python, you'll need to conda install PyMySQL for it to work.Walton
@Walton yea i also got this running on google cloud platform. Just was annoyed that conda can run it, but not through something else. Thank you though!Acrodrome

© 2022 - 2024 — McMap. All rights reserved.