Melanie
Melanie

Reputation: 1987

ImportError: No module named 'MySQL'

I have downloaded the Connector/Python for MySQL successfully. I used the following code in Python's shell to test my connection:

import mysql.connector

I received the following error message:

Traceback (most recent call last):
  File "<pyshell#8>", line 1, in <module>
    import mysql.connector
ImportError: No module named 'mysql'

I can't figure out why MySQL is not being recognized.

Upvotes: 164

Views: 619561

Answers (28)

Deepak Chauhan
Deepak Chauhan

Reputation: 932

The silly mistake I had done was keeping mysql.py in same directory. Try renaming mysql.py to another name so python don't recognize that as module.

Upvotes: 47

MD. SHIFULLAH
MD. SHIFULLAH

Reputation: 1731

For Python 3 version:

Python Command: pip3 install mysql-connector-python

OR

Python Command: pip3 install pymysql

For Python 2 version:

Python Command: pip install mysql-connector-python

OR

Python Command: pip install pymysql

Then your can import any of these package inside python code and then use it:

import mysql.connector

OR

import pymysql

Upvotes: 0

MrSaLeH
MrSaLeH

Reputation: 149

i have the same problem and it's just you need to change the file name I use mysql.py and I change it to same things ales and it's work fine

Upvotes: 0

May be simple install from cli?

pip3 install mysql-connector-python-rf

Package name differs from import library name

Or my universal variant in code:

import pip
pip.main(['install','mysql-connector-python-rf'])

For new version of pip:

from pip._internal import main
main(['install','mysql-connector-python-rf'])

It's better - install needed modules in running python installation (if many)

Upvotes: 37

rubStackOverflow
rubStackOverflow

Reputation: 6163

My project was using windows+pipenv+vscode. I had to activate the virtualenv via prompt and then open my project

pipenv shell
code .

Upvotes: 0

Ashish Gupta
Ashish Gupta

Reputation: 1241

For 64-bit windows

install using wheel

pip install wheel download from http://www.lfd.uci.edu/~gohlke/pythonlibs/#mysql-python

For python 3.x:

pip install mysqlclient-xxxxxxxxx-win_amd64.whl

For python 2.7:

pip install mysqlclient-xxxxxxxxx-win_amd64.whl

Upvotes: 0

akshat thakar
akshat thakar

Reputation: 1526

You need to use anaconda to manage python environment dependencies. MySQL connector can be installed using conda installer

conda install -c anaconda mysql-connector-python

Upvotes: 5

Rishi
Rishi

Reputation: 6189

I was facing the similar issue. My env details - Python 2.7.11 pip 9.0.1 CentOS release 5.11 (Final)

Error on python interpreter -

>>> import mysql.connector
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named mysql.connector
>>>

Use pip to search the available module -

$ pip search mysql-connector | grep --color mysql-connector-python



mysql-connector-python-rf (2.2.2)        - MySQL driver written in Python
mysql-connector-python (2.0.4)           - MySQL driver written in Python

Install the mysql-connector-python-rf -

$ pip install mysql-connector-python-rf

Verify

$ python
Python 2.7.11 (default, Apr 26 2016, 13:18:56)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-54)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import mysql.connector
>>>

Thanks =)

For python3 and later use the next command: $ pip3 install mysql-connector-python-rf

Upvotes: 182

Vishal Singh
Vishal Singh

Reputation: 62

I am facing the same issue, search a lot but didn't found any useful answer. Finally I found a mistake that first you should check your script name. Your script name shouldn't be mysql.py.

Upvotes: 1

Michał G
Michał G

Reputation: 2302

in my case - for python 3.x works

pip3 install mysql-connector-python

Upvotes: 1

Chaki_Black
Chaki_Black

Reputation: 932

👇️ in a virtual environment or using Python 2

pip install mysql-connector-python

👇️ for python 3 (could also be pip3.10 depending on your version)

pip3 install mysql-connector-python

👇️ if you get permissions error

sudo pip3 install mysql-connector-python pip install mysql-connector-python --user

👇️ if you don't have pip in your PATH environment variable

python -m pip install mysql-connector-python

👇️ for python 3 (could also be pip3.10 depending on your version)

python3 -m pip install mysql-connector-python

👇️ using py alias (Windows)

py -m pip install mysql-connector-python

👇️ for Anaconda

conda install -c anaconda mysql-connector-python

👇️ for Jupyter Notebook

!pip install mysql-connector-python

Source: https://bobbyhadz.com/blog/python-no-module-named-mysql

Upvotes: 4

Fabricio Biron
Fabricio Biron

Reputation: 71

Here is how I fixed the issue on my end.

Issue I couldn't neither compile nor have the package available on VSCode.

Context System: Ubuntu 18.04 Python version: 3.6.* Installed Packages

  1. mysql
  2. mysql-connector-python
  3. mysql-connector-python-rf

How I solved: Nothing really worked until I see this article: https://dev.mysql.com/doc/connector-python/en/connector-python-cext-development.html

On python console, just set

use_pure = True

With that, I could use python based connector instead.

import mysql.connector

However, if something goes wrong, the C Extension can be used through the _mysql_connector module.

References:

  1. https://dev.mysql.com/doc/connector-python/en/connector-python-cext-module.html
  2. https://www.reddit.com/r/learnpython/comments/a0q4bq/mysqlconnector_how_to_use_use_puretrue/
  3. https://dev.mysql.com/doc/connector-python/en/connector-python-connectargs.html
  4. https://overiq.com/mysql-connector-python-101/installing-mysql-connector-python/

Upvotes: 0

irsis
irsis

Reputation: 1054

I was facing the same issue on mac and linux both despite installing the connector using the command as mentioned in the other answers. What worked for me the following commands.

sudo python3 -m ensurepip --default-pip
python3 -m pip --version
sudo python3 -m pip install mysql-connector-python

I don't know what difference do they make since I am not a python programmer but would like to know if someone can enlighten.

Upvotes: 3

Dimuthu89
Dimuthu89

Reputation: 23

I had the same issue I resolved it using the following steps: Step 1 - in terminal type echo %path% look for a file that's similar "C:\Users\AppData\Local\Programs\Python\Python39" Step 2 - in vs code type "ctrl+shift+p" select python interpreter Step 3 - make sure you select the interpreter with the correct path, you found yours when you used echo %path%

Now run the program !

Upvotes: 1

Mr. B7
Mr. B7

Reputation: 159

This problem was a plague to me!!! The 100% solution is to forget using the mysql module: import mysql.connector, instead use pymysql via import pymysql. I installed it via the instructions: python3 -m pip install PyMySQL

made a change to the:

  1. Import statement
  2. The connector
  3. The cursor

After that everything worked like a charm. Hope this helps!

Upvotes: 3

Joseph Chillemi
Joseph Chillemi

Reputation: 188

@nembokid Thanks, I wanted to build on this answer. If you're using anaconda or any other form of environment management, be sure to select the python from that environment.

sudo /home/joseph/anaconda3/envs/stocks/bin/python -m pip install mysql-connector-python

Upvotes: 1

DeDenker
DeDenker

Reputation: 404

I did try re-install with apt and pip. But the only thing that worked was install from source.

On Ubuntu 18.04 x64

Upvotes: 0

ytdm
ytdm

Reputation: 1179

run

pip list 

to see list of packages you have installed. If it has mysql-connector-python then that is fine.

Remember not to name your python script file as mysql.py

Upvotes: 5

Haris ur Rehman
Haris ur Rehman

Reputation: 2663

My project was using pipenv. The following command worked for me:

pipenv install mysql-connector-python

Upvotes: 0

NemboKid
NemboKid

Reputation: 105

sudo python3 -m pip install mysql-connector-python

Upvotes: 2

Mohannd
Mohannd

Reputation: 1438

I tried all the answers but not worked for me. It is a python version problem, in the end, I realized that python 3 scripts need explicit pip command for python 3, at least on ubuntu 18.

python3 -m pip install mysql-connector

Upvotes: 39

G&#225;bor So&#243;s
G&#225;bor So&#243;s

Reputation: 11

I have found another reason: If your program file's path contains space or special characters, then you'd get this error.

Upvotes: 1

Горыныч
Горыныч

Reputation: 1

I just moved source folder connector from folder mysql to site-packages. And run import connector

Upvotes: 0

ilexcel
ilexcel

Reputation: 977

Use pip3 install mysql-connector to install the python packaged (if you are using Python 3. For Python 2 you can use pip).

Upvotes: 72

Ashish Gupta
Ashish Gupta

Reputation: 1241

You need to use one of the following commands. Which one depends on different-2 OS and software you have and use.

sudo easy_install mysql-python (mix os)
sudo pip install mysql-python (mix os)
sudo apt-get install python-mysqldb (Linux Ubuntu, ...)
cd /usr/ports/databases/py-MySQLdb && make install clean (FreeBSD)
yum install MySQL-python (Linux Fedora, CentOS ...)

Upvotes: 0

cristian
cristian

Reputation: 87

just a note, I just installed mysql on an ubuntu 16.04 server. I tried different options in the following order:

  • using the package from repository sudo apt-get install python-mysqldb: Was installed correctly, but unfortunatelly python returned ImportError: No module named 'mysql'
  • using the ubuntu package from Mysql: wget http://dev.mysql.com/get/Downloads/Connector-Python/mysql-connector-python_2.1.4-1ubuntu16.04_all.deb. Installed correctly with sudo dpckg -i package_name, but python returned the same error.
  • using tar.gz file, installing with python, worked ok.
    • wget http://dev.mysql.com/get/Downloads/Connector-Python/mysql-connector-python-2.1.6.tar.gz (used 2.1.4 at that time)
    • tar -xf mysql-connector-python-2.1.6.tar.gz
    • cd mysql-connector-python-2.1.6/
    • sudo python3 setup.py install

Did not investigate why the first two failed, might try later. Hope this helps someone.

Upvotes: 3

Spac3
Spac3

Reputation: 19

I found that @gdxn96 solution worked for me, but with 1 change.

sudo wget http://cdn.mysql.com//Downloads/Connector-Python/mysql-connector-python-2.1.3.tar.gz
tar -zxvf mysql-connector-python-2.1.3.tar
cd mysql-connector-python-2.1.3
sudo python3 setup.py install

Upvotes: 2

gdxn96
gdxn96

Reputation: 371

Try that out bud

sudo wget http://cdn.mysql.com//Downloads/Connector-Python/mysql-connector-python-2.1.3.tar.gz

gunzip mysql-connector-python-2.1.3.tar.gz

tar xf mysql-connector-python-2.1.3.tar

cd mysql-connector-python-2.1.3

sudo python3 setup.py install

Upvotes: 5

Related Questions