I am trying to pip install the MySQL-python package, but I get an ImportError.

Jans-MacBook-Pro:~ jan$ /Library/Frameworks/Python.framework/Versions/3.3/bin/pip-3.3 install MySQL-python
Downloading/unpacking MySQL-python
  Running setup.py egg_info for package MySQL-python
    Traceback (most recent call last):
      File "<string>", line 16, in <module>
      File "/var/folders/lf/myf7bjr57_jg7_5c4014bh640000gn/T/pip-build/MySQL-python/setup.py", line 14, in <module>
        from setup_posix import get_config
      File "./setup_posix.py", line 2, in <module>
        from ConfigParser import SafeConfigParser
    ImportError: No module named 'ConfigParser'
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):

  File "<string>", line 16, in <module>

  File "/var/folders/lf/myf7bjr57_jg7_5c4014bh640000gn/T/pip-build/MySQL-python/setup.py", line 14, in <module>

    from setup_posix import get_config

  File "./setup_posix.py", line 2, in <module>

    from ConfigParser import SafeConfigParser

ImportError: No module named 'ConfigParser'

----------------------------------------
Command python setup.py egg_info failed with error code 1 in /var/folders/lf/myf7bjr57_jg7_5c4014bh640000gn/T/pip-build/MySQL-python
Storing complete log in /Users/jan/.pip/pip.log
Jans-MacBook-Pro:~ jan$ 

Any ideas?

share|improve this question
    
what does echo $PATH say? – inspectorG4dget Dec 30 '12 at 1:58
2  
How come in 2017 I get the same error with python3? – Claudiu Creanga Nov 13 '17 at 11:12
up vote 145 down vote accepted

In Python 3, ConfigParser has been renamed to configparser for PEP 8 compliance. It looks like the package you are installing does not support Python 3.

share|improve this answer
28  
Correct. MySQL-python does not currently support Python 3. – Michael Mior Dec 30 '12 at 3:49
2  
What are my alternatives? I tried PyMySQL3-0.5 but its very buggy, it crashes at first execute call. – if __name__ is None Dec 30 '12 at 14:46
9  
@JanBirsa A quick google search found a few possibilities: if PyMySQL doesn't work, there is OurSQL, MySQL Connector/Python, a port of MySQL-Python, and others. – Abe Karplus Dec 30 '12 at 18:34
    
I was getting install error for all the python packages. Your Solution & discussion gave me the hint as to what could be the problem. Then I recalled that I had manually changed the /usr/bin/python from python2.7 to /usr/bin/python3.5, which actually was causing the issue. Once I reverted the same. It got solved. – Fooo Dec 17 '15 at 11:31
5  
A moving target on this one but I use pip3 install mysql-connector. Now available from MySQL for python3 support. release at time of typing is 2.1.3. – Longmang Jun 7 '16 at 9:42

You can instead use the mysqlclient package as a drop-in replacement for MySQL-python. It is a fork of MySQL-python with added support for Python 3.

I had luck with simply

pip install mysqlclient

in my python3.4 virtualenv after

sudo apt-get install python3-dev libmysqlclient-dev

which is obviously specific to ubuntu/debian, but I just wanted to share my success :)

share|improve this answer
    
On CentOS, run "yum install python-devel mysql-devel" before installing mysqlclient. – Joe Cheng Apr 6 '17 at 7:50
    
Note that this is a GPL package, so if you use this module, you need to release your program with GPL as well. – bogdan.mustiata Sep 28 '17 at 13:23
    
On Debian it is sufficient to install libpython3.5-minimal package (sudo apt-get install libpython3.5-minimal). – patryk.beza Oct 4 '17 at 10:12

Here is a code that should work in both Python 2.x and 3.x

Obviously you will need the six module, but it's almost impossible to write modules that work in both versions without six.

try:
    import configparser
except:
    from six.moves import configparser
share|improve this answer
    
Thanks for this, exactly what I was looking for. – Routhinator Aug 15 '15 at 17:44
2  
Why not skip the try block and just use from six.moves import configparser – Matthias Kuhn Nov 10 '15 at 19:20
4  
-1. The whole point of six.moves is to "provide a consistent interface" to renamed modules; wrapping imports with a try/except when using six defeats the point. Just use from six.moves import configparser. Not that six is needed here; you could replace from six.moves import configparser with import ConfigParser as configparser in your except block and achieve the same result without needing six. – Mark Amery Apr 13 '16 at 12:17

if you are using Centos then you need to use

  1. yum install python34-devel.x86_64

  2. yum groupinstall -y 'development tools'

  3. pip3 install mysql-connector

  4. pip install mysqlclient

Hope this would work.

share|improve this answer

MySQL-python is not supported on python3 instead of this you can use mysqlclient

If you are on fedora/centos/Red Hat install following package

  1. yum install python3-devel
  2. pip install mysqlclient
share|improve this answer

how about checking the version of Python you are using first.

import six
if six.PY2:
    import ConfigParser as configparser
else:
    import configparser
share|improve this answer

I run kali linux- Rolling and I came across this problem ,when I tried running cupp.py in the terminal, after updating to python 3.6.0. After some research and trial I found that changing ConfigParser to configparser worked for me but then I came across another issue.

config = configparser.configparser() AttributeError: module 'configparser' has no attribute 'configparser'

After a bit more research I realised that for python 3 ConfigParser is changed to configparser but note that it has an attribute ConfigParser().

share|improve this answer

Kindly to see what is /usr/bin/python pointing to

if it is pointing to python3 or higher change to python2.7

This should solve the issue.

I was getting install error for all the python packages. Abe Karplus's solution & discussion gave me the hint as to what could be the problem. Then I recalled that I had manually changed the /usr/bin/python from python2.7 to /usr/bin/python3.5, which actually was causing the issue. Once I reverted the same. It got solved.

share|improve this answer
2  
Reverting to Python 2 to support MySQL-python is overkill; use the Python 3 version of the library instead. – Mark Amery Apr 17 '16 at 19:06

This worked for me

cp /usr/local/lib/python3.5/configparser.py /usr/local/lib/python3.5/ConfigParser.py
share|improve this answer
    
should not do that. See answer from Abe karplus . ConfigParser has been renamed to configparser in Python3. – Bob Yoplait May 11 '17 at 10:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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