Search code examples
pythonmysql-pythonmysql-connectorpython-3.5mysql-connector-python

Python3.5 Refuse to Work with MySQL Connector


I've read a few questions on here that people are having the same situation as me but no matter what method I tried, it just wouldn't work!

According to MySQL documentation, MySQL-connector-python-2.x.x.x should work with Python 3+. I've downloaded the RPM package and installed it inside my virtualenv. However, upon importing the library, I get the error, "No module named MySQL".

Checking to see if I've installed the package:

(virtual)[centos ~]$ rpm -ql mysql-connector-python-2.1.3-1.el6.x86_64.rpm
package mysql-connector-python-2.1.3-1.el6.x86_64.rpm is not installed

Attempting to install the package:

(virtual)[centos ~]$ sudo rpm -i ~/Documents/python/mysql-connector-python-2.1.3-1.el6.x86_64.rpm 
[sudo] password for <username>: 
warning: /home/<username>/Documents/python/mysql-connector-python-2.1.3-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
    package mysql-connector-python-2.1.3-1.el6.x86_64 is already installed

This works on Python2.6:

[centos ~]$ python
Python 2.6.6 (r266:84292, Jul 23 2015, 15:22:56) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
>>> import mysql.connector
>>> 

Doesn't on Python3.5:

[centos ~]$ python3.5
Python 3.5.1 (default, Jan 13 2016, 17:43:34) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import mysql.connector
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named 'mysql'
>>> 

Verified the package through MySQL documentation:

(virtual)[centos]$ python
Python 3.5.1 (default, Jan 13 2016, 17:43:34) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 
>>> from distutils.sysconfig import get_python_lib
>>> print (get_python_lib ())
/home/<username>/Documents/redhat-server/python_project/virtual/lib/python3.5/site-packages
>>> 
>>> import mysql.connector
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named 'mysql'
>>> 

Making sure that python3.5 is in the same directory as MySQL.connector

(virtual)[centos]$ which python3.5
~/Documents/redhat-server/python_project/virtual/bin/python3.5

Update:

(virtual)[centos]$ pip install mysql-connector-python
Collecting mysql-connector-python
  Could not find a version that satisfies the requirement mysql-connector-python (from versions: )
No matching distribution found for mysql-connector-python
(virtual)[centos]$

What am I doing wrong?


Solution

  • I've figured it out. Anzel was right about the fact that it's installed globally in the default python2.6 directory. Here's how I fixed it:

    Launch the default python interpreter, in my case "python" -> python2.6, and run the commands below to see where "MySQL.connector" was installed.

    >>> from distutils.sysconfig import get_python_lib
    >>> print get_python_lib ()
    /usr/lib/python2.6/site-packages
    >>> 
    

    Navigate to the above directory, "/usr/lib/python2.6/site-packages/" reveal a directory called MySQL, and a file called, MySQL_connector_python-2.1.3-py2.6.egg-info.

    Launching python3.5 interpreter leads me to a different path, which I checked and sure enough, the two MySQL and MySQL_connector_python-2.1.3-py2.6.egg-info file/directory wasn't there.

    >>> from distutils.sysconfig import get_python_lib
    >>> print (get_python_lib ())
    /home/<username>/Documents/redhat-server/python_project/virtual/lib/python3.5/site-packages
    >>> 
    

    Next, I copied those 2 files to Python3.5 site-packages directory, tested the import, database connections, and it worked!

    Python 3.5.1 (default, Jan 13 2016, 17:43:34) 
    [GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux
    Type "copyright", "credits" or "license()" for more information.
    >>> 
    >>> import mysql.connector
    >>> 
    >>> cnx = mysql.connector.connect (user = 'username', password = 'password', host = 'localhost',
                   database = 'testdb')
    >>> cnx
    <mysql.connector.connection.MySQLConnection object at 0x7ffa4c1009b0>