我试图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$ 

什么好主意吗?


当前回答

这对我很有效

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

其他回答

请看看/usr/bin/python指向什么

如果指向python3或更高,则更改为python2.7

这应该能解决问题。

我得到的所有python包的安装错误。Abe Karplus的解决方案和讨论给了我关于问题所在的提示。 然后我想起我手动将/usr/bin/python从python2.7更改为/usr/bin/python3.5,这实际上是导致问题的原因。有一次我又变回原来的样子。问题解决了。

我在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

下面的代码应该在Python 2中都能工作。X和3.x

显然,您将需要six模块,但如果没有six模块,几乎不可能编写在两个版本中都可以工作的模块。

try:
    import configparser
except:
    from six.moves import configparser

我进一步回答了瓦莱勒的问题:

PIP安装configparser sudo cp /usr/lib/python3.6/configparser.py /usr/lib/python3.6/configparser.py 然后尝试重新安装MYSQL-python。这对我很有用

我建议链接这个文件,而不是复制它。这是保存更新。我将文件链接到/usr/lib/python3/目录。

configparser的Python 2/3兼容性可以简单地通过六个库来解决

from six.moves import configparser