ChatGPT解决这个技术问题 Extra ChatGPT

Python 3 ImportError: No module named 'ConfigParser'

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?

what does echo $PATH say?
How come in 2017 I get the same error with python3?
Its 2020 and still the same
2021 and still the same too :)
2022, any python imports are still unbelievably broken compared to node.js / C / C++ / literally anything else

J
Javad Nikbakht

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 :)


On CentOS, run "yum install python-devel mysql-devel" before installing mysqlclient.
Note that this is a GPL package, so if you use this module, you need to release your program with GPL as well.
On Debian it is sufficient to install libpython3.5-minimal package (sudo apt-get install libpython3.5-minimal).
On OSX you should run "brew install mysql" before installing mysqlclient.
I used pip3 install mysqlclient and it worked like a charm! Thanks!
M
Mark Amery

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.


Correct. MySQL-python does not currently support Python 3.
What are my alternatives? I tried PyMySQL3-0.5 but its very buggy, it crashes at first execute call.
@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.
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.
Just tried sudo python3 -m pip install mysql-connector and now it's working. Thanks for the hint @Longmang
a
aircraft

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

Why not skip the try block and just use from six.moves import configparser
-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.
@MatthiasKuhn I guess the intend was to make six only a requirement for Python 2.X, but not Python 3.X.
@MartinThoma This defeats the whole point (as Mark Amery wrote). If the try/except check is in place there is no more reason to use six.moves and the native name can be used directly for Python 2 as well.
j
jakub
pip install configparser
sudo cp /usr/lib/python3.6/configparser.py /usr/lib/python3.6/ConfigParser.py

Then try to install the MYSQL-python again. That Worked for me


Super idea, this should be an accepted answer although it is a geeky way.
K
Kaushal

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

yum install python3-devel pip install mysqlclient


raise EnvironmentError("%s not found" % (mysql_config.path,)) OSError: mysql_config not found
@PirateApp possibly mysql isn't install. check here stackoverflow.com/questions/7475223/…
sudo yum install python3-devel Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirror.web-ster.com * epel: mirrors.cat.pdx.edu * extras: mirror.web-ster.com * ius: mirrors.kernel.org * updates: mirror.web-ster.com No package python3-devel available. Error: Nothing to do
A
Akif

Compatibility of Python 2/3 for configparser can be solved simply by six library

from six.moves import configparser

D
Davis Herring

If you are using CentOS, then you need to use

yum install python34-devel.x86_64 yum groupinstall -y 'development tools' pip3 install mysql-connector pip install mysqlclient


S
Siddhi Kiran Bajracharya

I was having the same problem. Turns out, I needed to install python3 devel on my centos. First, you need to search for the package that is compatible with your system.

yum search python3 | grep devel

Then, install the package as:

yum install -y python3-devel.x86_64

Then, install mysqlclient from pip

pip install mysqlclient

R
Roy Ash

Do pip3 install PyMySQL and then pip3 install mysqlclient. Worked for me


A
Abhijit Manepatil

Additional info:

Python 2x

import ConfigParser

Python 3x

import configparser

ǝ
ǝlpoodooɟƃuooʞ

I got further with Valeres answer:

pip install configparser sudo cp /usr/lib/python3.6/configparser.py /usr/lib/python3.6/ConfigParser.py Then try to install the MYSQL-python again. That Worked for me

I would suggest to link the file instead of copy it. It is save to update. I linked the file to /usr/lib/python3/ directory.


E
Eje

Try this solution which worked fine for me.

Basically it's to reinstall/upgrade to latest version of mysql from brew, and then installing mysqlclient or MySQL-Python from global pip3 instead of virtualenv pip3.

Then accessing the virtualenv and successfully install mysqlclient or MySQL-Python.


B
Bart Mensfort

I still have this issue, so I go to /usr/lib/python3.8 and type as sudoer:

cp configparser.py ConfigParser.py

You may have another python version than 3.8.


R
Rene B.

For me the following command worked:

sudo python3 -m pip install mysql-connector

T
Troll

Following @MaciejNg I tried making a copy, which didn't work:

sudo cp ./env/lib/python3.8/site-packages/configparser.py ./env/lib/python3.8/site-packages/ConfigParser.py

Because configparser.py and ConfigParser.py are identical, I renamed the file:

sudo mv ./env/lib/python3.8/site-packages/configparser.py ./env/lib/python3.8/site-packages/ConfigParser.py

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
G
Gideon Kimutai

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

import six
if six.PY2:
    import ConfigParser as configparser
else:
    import configparser

This would be much simple from six.moves import configparser
O
Oliver Wahome

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().


D
DKMDebugin

I was getting the same error on Mac OS 10, Python 3.7.6 & Django 2.2.7. I want to use this opportunity to share what worked for me after trying out numerous solutions.

Steps

Installed Connector/Python 8.0.20 for Mac OS from link Copy current dependencies into requirements.txt file, deactivated the current virtual env, and deleted it using; create the file if not already created with; touch requirements.txt copy dependency to file; python -m pip3 freeze > requirements.txt deactivate and delete current virtual env; deactivate && rm -rf Created another virtual env and activated it using; python -m venv && source /bin/activate Install previous dependencies using; python -m pip3 install -r requirements.txt


A
AnotherDeveloper

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.


Reverting to Python 2 to support MySQL-python is overkill; use the Python 3 version of the library instead.
V
Vojtech Ruzicka

This worked for me

cp /usr/local/lib/python3.5/configparser.py /usr/local/lib/python3.5/ConfigParser.py

should not do that. See answer from Abe karplus . ConfigParser has been renamed to configparser in Python3.