TrolliOlli TrolliOlli - 1 year ago 87
MySQL Question

Python won't recognize mysql.connector even though I have it installed?

I installed mysql connector with pip, so now I have:

$ pip install mysql-connector
Requirement already satisfied: mysql-connector in /usr/local/lib/python2.7/dist-packages

I also tried:

$ pip install mysql-connector-python-rf
Requirement already satisfied: mysql-connector-python-rf in /usr/local/lib/python2.7/dist-packages

Now in my test file I just have:

import mysql.connector
from mysql.connector import errorcode

But when I run this I get:

$ python
Traceback (most recent call last):
File "", line 1, in <module>
import mysql.connector
File "/home/user/", line 1, in <module>
ImportError: No module named connector

I'm running on ubuntu and just to verify, here's my python version:

$ python --version
Python 2.7.12

Any ideas?

Answer Source

Sounds like there is something wrong with your module's path. Maybe you're importing the wrong module. I would try to print mysql.__file__ to make sure you're importing the right module.

In addition, using virtualenv is almost guaranteed to solve this kind of problems which usually stem from collisions between different Python interpreters and / or modules with similar names.

Using virtualenv

  1. Run pip install virtualenv to install virtualenv.
  2. Run virtualenv venv to create a Python virtual environment in your current directory.
  3. Run source venv/bin/activate to activate the virtual environment.
  4. Install your dependencies (mysql) using pip as you normally would and try running the script again.

More info on virtualenv can be found in the official docs.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download