这是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适配器,但没有效果(可能我安装不正确?)

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


当前回答

Sudo apt-get install python-mysqldb在ubuntu中完美工作

pip install mysql-python会引发一个环境错误

其他回答

运行Ubuntu 13.10 64位时,以下操作对我来说非常有效:

sudo apt-get install libmysqlclient-dev
sudo apt-get install python-dev

现在,导航到你的virtualenv(例如env文件夹)并执行以下命令:

sudo ./bin/pip install mysql-python

实际上,我在一个单独的问题中找到了解决方案,下面引用它:

If you have created the virtualenv with the --no-site-packages switch (the default), then system-wide installed additions such as MySQLdb are not included in the virtual environment packages. You need to install MySQLdb with the pip command installed with the virtualenv. Either activate the virtualenv with the bin/activate script, or use bin/pip from within the virtualenv to install the MySQLdb library locally as well. Alternatively, create a new virtualenv with system site-packages included by using the --system-site-package switch.

我认为这也适用于OSX。唯一的问题是获得用于安装libmysqlclient-dev和python-dev的等效命令,因为它们需要编译 我猜是mysql-python。

希望这能有所帮助。

加上其他答案,以下帮助我完成安装mysql-python:

Virtualenv, mysql-python, pip:有人知道怎么做吗?

在Ubuntu上……

apt-get install libmysqlclient-dev
apt-get install python-dev
pip install mysql-python

如果您没有适当的权限,不要忘记在命令的开头添加'sudo'。

此处引发的错误是在导入python模块时。这可以通过在OS x上将python site-packages文件夹添加到环境变量$PYTHONPATH中来解决。因此我们可以在.bash_profile文件中添加以下命令:

export PYTHONPATH="$PYTHONPATH:/usr/local/lib/pythonx.x/site-packages/"

*将x.x替换为您正在使用的python版本

pip install mysql-python

引发一个错误:

mysql_config未找到

sudo apt-get install python-mysqldb

修复了问题。

如果你正在使用python3,那么试试这个(我的操作系统是Ubuntu 16.04):

sudo apt-get install python3-mysqldb