-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

cmaher
  • 5,100
  • 1
  • 22
  • 34

2 Answers2

3

First insall PyMySQL using:

pip install PyMySQL

In python 3 it is pymysql, all in lower case

import pymysql
Murat Nafiz
  • 1,438
  • 17
  • 28
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)
# ...
cmaher
  • 5,100
  • 1
  • 22
  • 34
  • Here is the result: `File "capstonetest.py", line 2, in import pymysql ModuleNotFoundError: No module named 'pymysql' ` – ConfuzzledandPuzzled Feb 06 '18 at 16:50
  • 1
    @ConfuzzledandPuzzled could you please double-check that you have pymysql module installed, not pymssql? – GSazheniuk Feb 06 '18 at 16:55
  • 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. – cmaher Feb 06 '18 at 16:55
  • @GSazheniuk The pip list states that PyMySQL (0.8.0) is installed. i got it working in anaconda though. – ConfuzzledandPuzzled Feb 06 '18 at 18:09
  • @cmaher 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)] []` – ConfuzzledandPuzzled Feb 06 '18 at 18:12
  • 3
    @ConfuzzledandPuzzled 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](https://stackoverflow.com/questions/20994716/what-is-the-difference-between-pip-and-conda) 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. – cmaher Feb 06 '18 at 18:54
  • @cmaher 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! – ConfuzzledandPuzzled Feb 07 '18 at 19:32