5

我正在关注 django 教程,很多人都问过这个问题,但我认为我的情况有点独特,因为在安装 python-mysql 后,当我尝试执行 python manage.py syncdb 时仍然出现此错误,

我在 virtualenv 中,因为我使用 macports 来管理我的 python 安装我创建了我的 virtualenv

virtualenv code/vdjango --no-site-packages --python=/opt/local/bin/python

在此之前使用 macports 我安装了 django py27-django,但是在创建了我的 virtualenv 之后,我认为将 django 安装到 virtualenv 上会更好,所以我使用了pip install django==1.5

根据 django 教程,我编辑了我的 settings.py 文件,然后执行 python manage.py syncdb,我最终得到以下错误:

[~/code/vdjango/newsite]$python manage.py syncdb
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/mac-pro/code/vdjango/lib/python2.7/site-packages/django/core/management/__init__.py", line 453, in execute_from_command_line
    utility.execute()
  File "/Users/mac-pro/code/vdjango/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/mac-pro/code/vdjango/lib/python2.7/site-packages/django/core/management/__init__.py", line 272, in fetch_command
    klass = load_command_class(app_name, subcommand)
  File "/Users/mac-pro/code/vdjango/lib/python2.7/site-packages/django/core/management/__init__.py", line 77, in load_command_class
    module = import_module('%s.management.commands.%s' % (app_name, name))
  File "/Users/mac-pro/code/vdjango/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File "/Users/mac-pro/code/vdjango/lib/python2.7/site-packages/django/core/management/commands/syncdb.py", line 8, in <module>
    from django.core.management.sql import custom_sql_for_model, emit_post_sync_signal
  File "/Users/mac-pro/code/vdjango/lib/python2.7/site-packages/django/core/management/sql.py", line 9, in <module>
    from django.db import models
  File "/Users/mac-pro/code/vdjango/lib/python2.7/site-packages/django/db/__init__.py", line 40, in <module>
    backend = load_backend(connection.settings_dict['ENGINE'])
  File "/Users/mac-pro/code/vdjango/lib/python2.7/site-packages/django/db/__init__.py", line 34, in __getattr__
    return getattr(connections[DEFAULT_DB_ALIAS], item)
  File "/Users/mac-pro/code/vdjango/lib/python2.7/site-packages/django/db/utils.py", line 93, in __getitem__
    backend = load_backend(db['ENGINE'])
  File "/Users/mac-pro/code/vdjango/lib/python2.7/site-packages/django/db/utils.py", line 27, in load_backend
    return import_module('.base', backend_name)
  File "/Users/mac-pro/code/vdjango/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File "/Users/mac-pro/code/vdjango/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 17, in <module>
    raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: dlopen(/Users/mac-pro/code/vdjango/lib/python2.7/site-packages/_mysql.so, 2): Library not loaded: libmysqlclient.18.dylib
  Referenced from: /Users/mac-pro/code/vdjango/lib/python2.7/site-packages/_mysql.so
  Reason: image not found

为了确认我已经安装了 python-mysql 包,我再次运行了 pip install,

(vdjango)mac-pro@localhost:[~/code/vdjango/newsite]$pip install python-mysqldb
Downloading/unpacking python-mysqldb
  Could not find any downloads that satisfy the requirement python-mysqldb
No distributions at all found for python-mysqldb
Storing complete log in /Users/mac-pro/.pip/pip.log

那么这里发生了什么?看起来我拥有进一步学习本教程所需的一切,但有些 python/django 抱怨没有 mysqldb。

4

1 回答 1

18

我的 libmysqlclient.18.dylib 位于 /usr/local/mysql/lib/ 但我的系统正在 /usr/lib/ 中寻找它。我最终在 /usr/lib 中创建了 libmysqlclient.18.dylib 的符号链接,从而解决了这个问题。

1.) 确保 libmysqlclient.18.dylib 存在于 /usr/local/mysql/lib/ 中。

打开你的外壳。

sudo -s
ls /usr/local/mysql/lib/ | grep libmysqlclient.18.dylib

您应该看到该文件:

libmysqlclient.18.dylib

如果没有,请在您的系统中搜索文件的位置:

find / -name libmysqlclient.18.dylib

2.) 在 /usr/lib 中创建 libmysqlclient.18.dylib 的符号链接

在您的 shell 中输入以下命令:

ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib

如果您的 libmysqlclient.18.dylib 文件不在 /usr/local/mysql/lib 中,请将第一个路径替换为 libmysqlclient.18.dylib 的正确路径。

希望这会有所帮助。

于 2013-09-15T16:09:22.003 回答