2013-02-28 149 views
2

我想在MySQL數據庫中使用SQLAlchemy。我創建了引擎並嘗試連接,但出現如下錯誤。Python3.2:安裝MySQL-python失敗,出現錯誤「No module named ConfigParser」

from sqlalchemy import create_engine 
from sqlalchemy.ext.declarative import declarative_base 
Engine = create_engine('mysql+mysqldb://dbuser:[email protected]/mydb') 
Base = declarative_base(Engine) 

失敗,出現錯誤:

File "/usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.8.0b2-py3.2.egg/sqlalchemy/connectors/mysqldb.py", line 57, in dbapi 
return __import__('MySQLdb') 
ImportError: No module named MySQLdb 

搜索,我發現,你必須有安裝MySQLdb的Python的發揮好與MySQL。所以試圖用pip-3.2或easy_install-3.2安裝它會失敗,如下所示。

sudo pip-3.2 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 "/tmp/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 "/tmp/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 

然後我想,我必須安裝configparser所以當我嘗試這樣做,它也失敗

Original exception was: 
Traceback (most recent call last): 
File "<string>", line 3, in <module> 
File "/usr/local/lib/python3.2/dist-packages/distribute-0.6.34-py3.2.egg/setuptools/__init__.py", line 2, in <module> 
from setuptools.extension import Extension, Library 
File "/usr/local/lib/python3.2/dist-packages/distribute-0.6.34-py3.2.egg/setuptools/extension.py", line 2, in <module> 
import distutils.core 
File "/usr/lib/python3.2/distutils/core.py", line 19, in <module> 
from distutils.config import PyPIRCCommand 
File "/usr/lib/python3.2/distutils/config.py", line 8, in <module> 
from configparser import ConfigParser 
File "configparser.py", line 122 

所以,我原來的問題是試圖讓Python來使用MySQL的。但試圖解決,我無法安裝MySQLdb。

編輯 按照以下@robertklep的指示前進並安裝了pymsql3。

sudo pip-3.2 install pymysql3 

問候,

回答

4

MySQLdb的沒有Python 3的支持。嘗試一種替代方案,如PyMySQL(即supported by SQLAlchemy as well)。

編輯:還有this project

+0

感謝您的。昨天浪費了整整一天。我看到有pymysql3和pymysql_sa,它聲明pymysql_sa是SQLAlchemy的PyMySQL方言。那麼我應該安裝哪個?鑑於pymysql_sa與sqlalchemy特別合作,pymysql_sa更有優勢嗎? – lukik 2013-03-01 10:17:36

+0

發現了「硬道路」。嘗試安裝pymysql_sa並失敗。最有可能的是它與Python3不兼容。所以,是的,堅持與pymysql3。謝謝。 – lukik 2013-03-01 10:23:09

+0

我嘗試使用pymysql3和我[遇到問題](http://stackoverflow.com/questions/15170450/sqlalchemypymysql-error-sqlalchemy-util-queue-empty),正如指出sqlalchemy有一個錯誤。你有解決方法嗎? – lukik 2013-03-03 04:45:20

2

https://www.pythonanywhere.com/wiki/UsingMySQL

pip install --user https://dev.mysql.com/get/Downloads/Connector-Python/mysql-connector-python-1.1.6.tar.gz 

然後更新您的settings.py使用Oracle Django的後端, 「mysql.connector.django」:

DATABASES = { 
    'default': { 
     'ENGINE': 'mysql.connector.django', 
     ... 
+0

這將引發錯誤以及.. – SexyBeast 2015-04-10 20:30:21

+0

試圖在此之後,我得到了「django.core.exceptions.ImproperlyConfigured:‘mysql.connector.django’不是一個可用的數據庫後端」 – 2015-08-10 12:50:11