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.
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 ...)
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
The silly mistake I had done was keeping mysql.py
in same dir. Try renaming mysql.py
to another name so python
don't consider that as module.
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)
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
I just moved source folder connector
from folder mysql
to site-packages
.
And run import connector
I have found another reason: If your program file's path contains space or special characters, then you'd get this error.
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
My project was using pipenv
. The following command worked for me:
pipenv install mysql-connector-python
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!
just a note, I just installed mysql on an ubuntu 16.04 server. I tried different options in the following order:
sudo apt-get install python-mysqldb
: Was installed correctly, but unfortunatelly python returned ImportError: No module named '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. 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.
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
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
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
Use
pip3 install mysql-connector
to install the python packaged (if you are using Python 3. For Python 2 you can use pip
).
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
sudo python3 -m pip install mysql-connector-python
Source: Stackoverflow.com