2012-12-30 38 views
182

我試圖pip installMySQL-python包,但我得到一個ImportErrorPython 3 ImportError:沒有名爲'ConfigParser'的模塊

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$ 

任何想法?

+0

要解決什麼'回聲$ PATH'說? – inspectorG4dget

+2

2017年怎麼樣我得到了與python3相同的錯誤? –

回答

167

在Python 3中,ConfigParser已重命名爲configparser以符合PEP 8。它看起來像你正在安裝的軟件包不支持Python 3.

+35

正確。 MySQL-python目前不支持Python 3. –

+2

我的替代方案是什麼?我嘗試了PyMySQL3-0.5,但它的錯誤,它首先執行調用崩潰。 –

+11

@JanBirsa快速谷歌搜索發現了一些可能性:如果[PyMySQL](https://github.com/petehunt/PyMySQL/)不起作用,那麼[OurSQL](https://launchpad.net/oursql /),[MySQL Connector/Python](http://dev.mysql.com/doc/connector-python/en/index.html),[MySQL-Python的一個端口](http://sourceforge.net/) p/mysql-python/discussion/70460/thread/61e3a3c9)等等。 –

259

你可以改爲使用mysqlclient包作爲MySQL-python的插入替代品。這是MySQL-python添加了支持叉子爲Python 3

我在python3.4 virtualenv中的運氣與簡單

pip install mysqlclient 

sudo apt-get install python3-dev libmysqlclient-dev 

後,這顯然是專門針對Ubuntu的/ debian,但我只是想分享我的成功:)

+0

在CentOS上,在安裝mysqlclient之前運行「yum install python-devel mysql-devel」。 –

+0

請注意,這是一個GPL包,因此如果您使用此模塊,則需要使用GPL發佈程序。 –

+0

在[* Debian *](https://en.wikipedia.org/wiki/Debian)上安裝['libpython3.5-minimal']就足夠了(https://packages.debian.org/search?searchon = names&keywords = libpython3.5-minimal)package('sudo apt-get install libpython3.5-minimal')。 –

6

這是一個代碼,應該在Python 2.x和3.x中工作

很明顯,您需要six模塊,但幾乎不可能編寫在兩個版本中都不工作的模塊。

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

感謝您的支持,正是我一直在尋找的。 – Routhinator

+3

爲什麼不跳過try塊,只使用'from six.moves import configparser' –

+4

-1。 「six.moves」的重點是[*]爲重命名的模塊提供一致的界面*(https://pythonhosted.org/six/#module-six.moves);當使用'6'時,用'try' /''裝飾進口,否則就會失敗。只需使用'from 6.moves import configparser'。這裏不需要六個;你可以在'except'塊中用'import ConfigParser as configparser'替換'from six.moves import configparser',並且在不需要'six'的情況下獲得相同的結果。 –

-2

請看看什麼/usr/bin/python指向

如果它指向python3 or higher變化python2.7

這應該解決的問題。

我得到所有python軟件包的安裝錯誤。 Abe Karplus的解決方案&討論給了我一個暗示,可能是什麼問題。 然後我回想起我手動將/usr/bin/pythonpython2.7更改爲/usr/bin/python3.5,這實際上是造成這個問題。一旦我reverted一樣。它解決了。

+2

恢復到Python 2以支持'MySQL-python'是矯枉過正;改爲使用庫的Python 3版本。 –

-2

這爲我工作

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

不應該這樣做。查看Abe karplus的答案。 ConfigParser已在Python3中重命名爲configparser。 –

3

如果你正在使用的Centos,那麼你需要使用

  1. 百勝安裝python34-devel.x86_64

  2. 百勝groupinstall -y「發展工具'

  3. pip3安裝mysql-connector

  4. PIP安裝的mysql

希望這會工作。

6

的MySQL-python的不支持python3,而不是這個,你可以使用mysqlclient

如果你是fedora/centos/Red Hat安裝下面的包

  1. yum install python3-devel
  2. pip install mysqlclient
0

如何檢查版本你首先使用的Python的離子。

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

這將是非常簡單'從six.moves進口configparser' – Akif

+0

這也很有效,簡短和精確 –

0

我運行Linux的卡利滾動軸承和我碰到這個問題,當我試圖在終端運行cupp.py,更新到3.6.0蟒蛇後。經過一些研究和試用後,我發現改變ConfigParserconfigparser爲我工作,但後來我遇到了另一個問題。

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

更多的研究後,我意識到,蟒蛇3 ConfigParser改爲configparser但要注意,它有一個屬性ConfigParser()

-1

這對我有效。 Python的2/3 configparser

from ConfigParser import ConfigParser

0

兼容性可以簡單地通過six

from six.moves import configparser 
相關問題