我试图pip安装MySQL-python包,但我得到一个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$ 

什么好主意吗?


当前回答

我也有同样的问题。结果,我需要在我的centos上安装python3 devel。首先,您需要搜索与您的系统兼容的软件包。

yum search python3 | grep devel

然后,按照如下方式安装包:

yum install -y python3-devel.x86_64

然后,从pip安装mysqlclient

pip install mysqlclient

其他回答

先pip3安装PyMySQL,再pip3安装mysqlclient。 为我工作

我在Mac OS 10, Python 3.7.6和Django 2.2.7上得到了同样的错误。我想利用这个机会分享在尝试了许多解决方案后对我有效的方法。

步骤

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 <virtual-env-name> Created another virtual env and activated it using; python -m venv <virtual-env-name> && source <virtual-env-name>/bin/activate Install previous dependencies using; python -m pip3 install -r requirements.txt

下面是@MaciejNg,我试着复制了一份,但没有成功:

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

因为configparser.py和configparser.py是相同的,所以我重命名了这个文件:

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

在Python 3中,ConfigParser已重命名为ConfigParser以符合PEP 8。看起来您正在安装的包不支持Python 3。

您可以使用mysqlclient包代替MySQL-python。它是MySQL-python的一个分支,增加了对Python 3的支持。

我的运气很简单

pip install mysqlclient

在我的python3.4 virtualenv之后

sudo apt-get install python3-dev libmysqlclient-dev

这显然是针对ubuntu/debian的,但我只是想分享我的成功:)