36

I'm using Amazon Linux AMI release 2013.09. I've install virtualenv and after activation then I run pip install mysql-connector-python, but when I run my app I get an error: ImportError: No module named mysql.connector. Has anyone else had trouble doing this? I can install it outside of virtualenv and my script runs without issues. Thanks in advance for any help!

Drewness
  • 5,004
  • 4
  • 32
  • 50
slim
  • 2,545
  • 1
  • 24
  • 38
  • What version of Python are you running? – Drewness Feb 28 '14 at 16:41
  • 2.6.9 (unknown, Oct 29 2013, 19:58:13) [GCC 4.6.3 20120306 (Red Hat 4.6.3-2)] – slim Feb 28 '14 at 17:39
  • 1
    I was able to run my app inside of my venv by installing the python connector outside of virtualenv and then manually copying what was installed in the site-packages folder into my virtual environments site-packages folder. This is no solution however as it defeats the purpose of virtualenv. It's interesting that pip didn't already do this. – slim Feb 28 '14 at 17:51
  • 1
    Yeah, this is a hassle since `yum` relies on Python 2.6.x. I would recommend setting up a `virtualenv` with 2.7, that way you get the best of both worlds. [Here](http://tumblr.kurttheviking.com/post/30920138314/painless-python-2-7-on-aws-ec2) is a blog describing the process. – Drewness Feb 28 '14 at 17:51
  • See also https://stackoverflow.com/questions/43029672/unable-to-find-protobuf-include-directory – Scott Kaiser Jul 24 '17 at 16:40

14 Answers14

60

Several things. There is an inconsistency in package naming so you may want to do:

pip search mysql-connector

to find out what it is called on your platform. I got two results mysql-connector-python and mysql-connector-repackaged.

so try this first:

pip install mysql-connector-python

this may additionally give an error like this:

Some externally hosted files were ignored (use 
    --allow-external mysql-connector-python to allow).

so finally this should do the job:

pip install mysql-connector-python --allow-external mysql-connector-python
Kinjal Dixit
  • 7,777
  • 2
  • 59
  • 68
  • I had to move on in my project, but I will make an effort to test this out and get back to you. Thank you for your answer. – slim Apr 03 '14 at 13:23
  • `mysql-connector-python` comes before `--allow-external` **and after**!! – J-Dizzle Dec 12 '15 at 01:28
  • It looks like --allow-external is not supported in some versions of pip? I'm using pip 1.3.1 in a python 2.7.10 venv and get "no such option: --allow-external" – Corbell Feb 02 '16 at 21:15
  • Follow-up: I also updated pip to 8.0.2 and get the same result, no such option! But I know this worked before, under python 2.7.8. – Corbell Feb 02 '16 at 21:21
  • Could not find a version that satisfies the requirement mysql-connector-python (from versions: ) No matching distribution found for mysql-connector-python – CodeGuru Feb 05 '16 at 05:31
  • This no longer works. Apparently, allow-external is deprecated – Jorick Spitzen Feb 11 '16 at 20:43
  • Answer by @slim worked for me. sudo easy_install mysql-connector – John Kentucky Dec 21 '16 at 13:47
31

Solution I found:

sudo pip install mysql-connector-python-rf

If you see this error: option --single-version-externally-managed not recognized, try this:

sudo pip install --egg mysql-connector-python-rf
George Chalhoub
  • 14,968
  • 3
  • 38
  • 61
19
pip install mysql-connector

This worked for me!

prashanth
  • 4,197
  • 4
  • 25
  • 42
7

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
>>>
Rishi
  • 5,869
  • 7
  • 34
  • 45
  • Do you know the difference between the two packages? What's does the "-rf" stand for? – slim Feb 09 '17 at 14:49
  • @slim TBH even I am having the same question! could not find the answer yet.. For sure its not something random fancy stuff =) – Rishi Feb 09 '17 at 17:27
  • 4
    mysql-connector-python-random-fix ;) – slim Feb 09 '17 at 17:53
  • 1
    @slim see ["What are the differences..."](https://stackoverflow.com/q/34168651/673991). Summarizing, (1) it's Required For Django 1.8, (2) it's Retrieved the most Frequently, (3) it's the veRsion that actually Functions. – Bob Stein Nov 07 '17 at 16:13
6

I'd like to add that

sudo easy_install mysql-connector

worked for me after pip kept crashing no matter what I did.

slim
  • 2,545
  • 1
  • 24
  • 38
5

I've battled with this and tried upgrading pip and setuptools but actually it seems all you need to do is:

sudo pip install virtualenv --upgrade

Once you've upgraded virtualenv, create a new virtual environment, activate it and try installing with:

pip install https://cdn.mysql.com/Downloads/Connector-Python/mysql-connector-python-2.1.3.tar.gz

(you might need to update the url, but that's the current one)

DenisH
  • 859
  • 8
  • 12
5

Command

pip install mysql-connector-python-rf

worked for me in my pyenv python version 2.7.12 and pip version 9.0.1.

Thomas Ducrot
  • 2,841
  • 1
  • 16
  • 14
4

I have managed to overcome this problem by entering mysql-connector-python package URL, taken from MySQL page, directly to the requirements file, instead of dependency name.

My requirements file looks like this:

bson==0.4.2
https://cdn.mysql.com/Downloads/Connector-Python/mysql-connector-python-2.0.4.tar.gz
pymongo==3.2.1

After creating virtualenv and switching into it I am executing

$ pip install -r ./requirements

And pip is doing the rest of the work, i.e., downloading, extracting and installing.

wpodgorski
  • 785
  • 9
  • 12
4

It works for me!

pip install mysql-connector-python-rf

If you get the error, install the latest version of setuptools and wheel:

pip install -U setuptools
pip install -U wheel
gehbiszumeis
  • 3,525
  • 4
  • 24
  • 41
pinple
  • 41
  • 3
3

Also something that can go wrong: Don't name your own module mysql

import mysql.connector will fail because the import gives the module in the project precedence over site packages and yours likely doesnt have a connector.py file.

Mario
  • 2,619
  • 1
  • 24
  • 22
2

try my answer here. Though i meant it for Python3, u can just modify the command python3->python to make it work for python2

Community
  • 1
  • 1
SanD
  • 473
  • 7
  • 11
0

I just had the same problem and none of the solutions below worked. Some was already installed and the last one returned me an error:

    pip install mysql-connector
    pip install mysql-connector-python
    pip install mysql-connector-python-rf

It looks like all I had to do was run the code in a different IDLE version, moving from 3.6 to 3.7. Before doing that, I also used the windows repair installation tool in two different python versions I have installed.

Sometimes the simplest solutions work. Good luck

Youki
  • 33
  • 8
0

Make sure that you've installed the mysql connector from within the venv. In VSCode, what worked for me is I selected the venv interpreter as the python interpreter.

https://code.visualstudio.com/docs/python/environments

  • An answer is a direct response that sincerely attempts to provide the information/analysis requested by the question. Hence remember link is not an answer. – Kaushik Burkule Nov 01 '19 at 09:26
  • A link to a solution is welcome, but please ensure your answer is useful without it: add some more text and examples or code that someone can use to solve the problem asked in the OP post. – Ioannis Barakos Nov 01 '19 at 09:45
0

You can download official connector from here -

Choose Platform independent version.

Unzip and cd to the folder.

run sudo -H python3 setup.py install from your environment.

Done

octobus
  • 1,246
  • 1
  • 14
  • 20
Eugen
  • 19
  • 5