Ubuntu – MySQL connector/python ImportError: No module named ‘thesql’

MySQLpythonpython3

I'm very new to Ubuntu/Linux and Python so it's entirely possible I'm missing something obvious here. I'm running Ubuntu 16.04 LTS, server version.

I've just installed the official MySQL connector/python (2.1.7, python3 version) using dpkg -i [package-name].deb (obviously I replaced [package-name] with the actual name). It's listed as being compatible with Python 3.5, which is the version I have. When I try to verify the installation through the Python interpreter using import mysql.connector, I just get the following error:

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

On one similar question, I've seen it suggested that the connector may not have installed properly if python is not in the path. I didn't seem to get any issues during the installation; the file python3 is in /usr/bin which is definitely in the path, but seems to be a link to the file python3.5 in the same location. Running the command which python3 gives the output usr/bin/python3 but running which python gives no output.

The package apparently also contained a C library, which seems to have installed in usr/lib/python3/dist-packages. Nothing else in that directory is called anything related to MySQL.

What should I be looking for to know whether it's an issue with the connector installation or with Python itself? And how do I fix this?

Best Answer

The solution is to install corresponding Python 3 module:

sudo apt-get install python3-mysql.connector

It fixes import mysql.connector error:

$ python3
Python 3.5.2 (default, Nov 23 2017, 16:37:01) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import mysql.connector
>>> 

And similar for Python 2:

sudo apt-get install python-mysql.connector

$ python2
Python 2.7.12 (default, Dec  4 2017, 14:50:18) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import mysql.connector
>>> 
Related Question