'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.
Solution 1:[1]
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
Solution 2:[2]
Use
pip3 install mysql-connector
to install the python packaged (if you are using Python 3. For Python 2 you can use pip).
Solution 3:[3]
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)
Solution 4:[4]
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.
Solution 5:[5]
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
Solution 6:[6]
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
Solution 7:[7]
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
Solution 8:[8]
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 returnedImportError: 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 withsudo 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.gzcd 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.
Solution 9:[9]
sudo python3 -m pip install mysql-connector-python
Solution 10:[10]
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
Solution 11:[11]
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:
- Import statement
- The connector
- The cursor
After that everything worked like a charm. Hope this helps!
Solution 12:[12]
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
Solution 13:[13]
I have found another reason: If your program file's path contains space or special characters, then you'd get this error.
Solution 14:[14]
@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
Solution 15:[15]
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 !
Solution 16:[16]
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.
Solution 17:[17]
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 ...)
Solution 18:[18]
I just moved source folder connector from folder mysql to site-packages.
And run import connector
Solution 19:[19]
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
Solution 20:[20]
My project was using pipenv. The following command worked for me:
pipenv install mysql-connector-python
Solution 21:[21]
I did try re-install with apt and pip. But the only thing that worked was install from source.
On Ubuntu 18.04 x64
Solution 22:[22]
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
- mysql
- mysql-connector-python
- 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:
- https://dev.mysql.com/doc/connector-python/en/connector-python-cext-module.html
- https://www.reddit.com/r/learnpython/comments/a0q4bq/mysqlconnector_how_to_use_use_puretrue/
- https://dev.mysql.com/doc/connector-python/en/connector-python-connectargs.html
- https://overiq.com/mysql-connector-python-101/installing-mysql-connector-python/
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
