这是OSX 10.6用户经常讨论的问题,但我还没有找到一个有效的解决方案。下面是我的设置:

Python 2.6.1 64bit Django 1.2.1 " MySQL 5.1.47 osx10.6 64bit

我用——no-site-packages创建了一个virtualenvwrapper,然后安装Django。当我激活virtualenv并运行python manage.py syncdb时,我得到这个错误:

Traceback (most recent call last):
File "manage.py", line 11, in <module>
  execute_manager(settings)
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/core/management/__init__.py", line 438, in execute_manager
  utility.execute()
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/core/management/__init__.py", line 379, in execute
  self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/core/management/__init__.py", line 257, in fetch_command
  klass = load_command_class(app_name, subcommand)
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/core/management/__init__.py", line 67, in load_command_class
  module = import_module('%s.management.commands.%s' % (app_name, name))
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/utils/importlib.py", line 35, in import_module
  __import__(name)
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/core/management/commands/syncdb.py", line 7, in <module>
from django.core.management.sql import custom_sql_for_model, emit_post_sync_signal
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/core/management/sql.py", line 5, in <module>
from django.contrib.contenttypes import generic
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/contrib/contenttypes/generic.py", line 6, in <module>
  from django.db import connection
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/db/__init__.py", line 75, in <module>
  connection = connections[DEFAULT_DB_ALIAS]
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/db/utils.py", line 91, in __getitem__
  backend = load_backend(db['ENGINE'])
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/db/utils.py", line 32, in load_backend
  return import_module('.base', backend_name)
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/utils/importlib.py", line 35, in import_module
  __import__(name)
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/db/backends/mysql/base.py", line 14, in <module>
  raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb

我还安装了MySQL for Python适配器,但没有效果(可能我安装不正确?)

有人之前处理过这个问题吗?


当前回答

试试这个:这为我解决了问题。

pip安装MySQL-python

其他回答

我在OSX 10.6.6上遇到了同样的问题。但只是在终端上简单的easy_install mysql-python并不能解决这个问题,因为另一个问题随之而来:

错误:命令'gcc-4.2'失败,退出状态为1。

显然,这个问题是在从XCode3 (OSX 10.6自带)升级到XCode4之后出现的。这个更新的版本删除了对建造ppc拱门的支持。如果是同样的情况,请尝试在easy_install mysql-python之前执行以下操作

sudo bash
export ARCHFLAGS='-arch i386 -arch x86_64'
rm -r build
python setup.py build
python setup.py install

非常感谢Ned Deily提供的解决方案。检查在这里

我是如何让它工作的:

virtualenv -p python3.5 env/test

在找到我的环境后:

pip install pymysql
pip install django

然后,我运行startproject,在manage.py中,我添加了这个:

+ try:
+     import pymysql
+     pymysql.install_as_MySQLdb()
+ except:
+     pass

同时,更新了内部设置:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'foobar_db',
        'USER': 'foobaruser',
        'PASSWORD': 'foobarpwd',
    }
}

我也有configparser==3.5.0安装在我的virtualenv,不确定这是否是必需的…

希望能有所帮助,

试试下面的命令。他们为我工作:

brew install mysql-connector-c 
pip install MySQL-python

此问题是由于MySQL for Python适配器安装不完整/不正确造成的。具体来说,我必须编辑mysql_config文件的路径,使其指向/usr/local/mysql/bin/mysql_config——本文将更详细地讨论:http://dakrauth.com/blog/entry/python-and-django-setup-mac-os-x-leopard/

我也有同样的错误,pip install MySQL-python为我解决了这个问题。

备选安装:

如果没有pip, easy_install MySQL-python应该可以工作。 如果你的python是由打包系统管理的,你可能不得不使用 该系统(例如sudo apt-get install…)

下面,Soli指出,如果你收到以下错误:

mysql_config未找到

... 然后还有一个进一步的系统依赖问题。解决方法因系统而异,但对于debian衍生系统:

使用apt-get install python-mysqldb