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?


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
| improve this answer | |
  • Thank you for the suggestion. That seems to require Python 2.6 or 2.7: You might want to run 'apt-get -f install' to correct these: The following packages have unmet dependencies: mysql-connector-python-cext : Depends: python2.6 but it is not installable or python2.7 but it is not going to be installed Depends: python (>= 2.6.3) but it is not going to be installed E: Unmet dependencies. Try 'apt-get -f install' with no packages (or specify a solution). – purplecho Mar 14 '18 at 17:59
  • I do not understand where you got problems. On clean installation both Python 2 and 3 modules are installed successfully. Do you have any PPAs added? Did you tried sudo apt-get install -f? – N0rbert Mar 14 '18 at 18:04
  • Running sudo apt-get -f install then re-running the command you first suggested seems to have worked for me, thank you! As a side note, I have one PPA added but that one doesn't seem to contain anything directly related to this issue. – purplecho Mar 14 '18 at 18:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.