djangoでpython manage.py runserverしたら libmysqlclient.18.dylib がないとか言われた

macのbrewをupdateしたらmysqlがつかえなくなってしまって涙目になったぷぎえもんです。
仕方ないのでbrewのmysqlを削除してdmg版を入れ直しました。
http://www.mysql.com/downloads/mysql/
mysqlのサイトではmac10.6用のmysqlしかダウンロードできないようになっていましたが、10.7のmacに入れてもいまのところ動いています。

djangoが起動しなくなっちゃった

何気なしにrunserverしたら起動してくれませんでした。

$ python manage.py runserver
Traceback (most recent call last):
File “manage.py”, line 14, in
execute_manager(settings)
File “/usr/local/Cellar/python/2.7.2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py”, line 438, in execute_manager
utility.execute()
File “/usr/local/Cellar/python/2.7.2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py”, line 379, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File “/usr/local/Cellar/python/2.7.2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/base.py”, line 191, in run_from_argv
self.execute(*args, **options.__dict__)
File “/usr/local/Cellar/python/2.7.2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/base.py”, line 209, in execute
translation.activate(‘en-us’)
File “/usr/local/Cellar/python/2.7.2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/translation/__init__.py”, line 100, in activate
return _trans.activate(language)
File “/usr/local/Cellar/python/2.7.2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/translation/trans_real.py”, line 202, in activate
_active.value = translation(language)
File “/usr/local/Cellar/python/2.7.2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/translation/trans_real.py”, line 185, in translation
default_translation = _fetch(settings.LANGUAGE_CODE)
File “/usr/local/Cellar/python/2.7.2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/translation/trans_real.py”, line 162, in _fetch
app = import_module(appname)
File “/usr/local/Cellar/python/2.7.2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/importlib.py”, line 35, in import_module
__import__(name)
File “/usr/local/Cellar/python/2.7.2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/contrib/admin/__init__.py”, line 3, in
from django.contrib.admin.helpers import ACTION_CHECKBOX_NAME
File “/usr/local/Cellar/python/2.7.2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/contrib/admin/helpers.py”, line 3, in
from django.contrib.admin.util import (flatten_fieldsets, lookup_field,
File “/usr/local/Cellar/python/2.7.2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/contrib/admin/util.py”, line 1, in
from django.db import models
File “/usr/local/Cellar/python/2.7.2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/__init__.py”, line 78, in
connection = connections[DEFAULT_DB_ALIAS]
File “/usr/local/Cellar/python/2.7.2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/utils.py”, line 93, in __getitem__
backend = load_backend(db[‘ENGINE’])
File “/usr/local/Cellar/python/2.7.2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/utils.py”, line 33, in load_backend
return import_module(‘.base’, backend_name)
File “/usr/local/Cellar/python/2.7.2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/importlib.py”, line 35, in import_module
__import__(name)
File “/usr/local/Cellar/python/2.7.2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/backends/mysql/base.py”, line 14, in
raise ImproperlyConfigured(“Error loading MySQLdb module: %s” % e)
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: dlopen(/usr/local/Cellar/python/2.7.2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/MySQL_python-1.2.3-py2.7-macosx-10.4-x86_64.egg/_mysql.so, 2): Library not loaded: /usr/local/lib/libmysqlclient.18.dylib
Referenced from: /usr/local/Cellar/python/2.7.2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/MySQL_python-1.2.3-py2.7-macosx-10.4-x86_64.egg/_mysql.so
Reason: image not found

そっか。runserverの前にまずsyncdbしないといけないんだった。

$ python manage.py syncdb;
Traceback (most recent call last):
File “manage.py”, line 14, in
execute_manager(settings)
File “/usr/local/Cellar/python/2.7.2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py”, line 438, in execute_manager
utility.execute()
File “/usr/local/Cellar/python/2.7.2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py”, line 379, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File “/usr/local/Cellar/python/2.7.2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py”, line 261, in fetch_command
klass = load_command_class(app_name, subcommand)
File “/usr/local/Cellar/python/2.7.2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py”, line 67, in load_command_class
module = import_module(‘%s.management.commands.%s’ % (app_name, name))
File “/usr/local/Cellar/python/2.7.2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/importlib.py”, line 35, in import_module
__import__(name)
File “/usr/local/Cellar/python/2.7.2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/commands/syncdb.py”, line 7, in
from django.core.management.sql import custom_sql_for_model, emit_post_sync_signal
File “/usr/local/Cellar/python/2.7.2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/sql.py”, line 6, in
from django.db import models
File “/usr/local/Cellar/python/2.7.2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/__init__.py”, line 78, in
connection = connections[DEFAULT_DB_ALIAS]
File “/usr/local/Cellar/python/2.7.2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/utils.py”, line 93, in __getitem__
backend = load_backend(db[‘ENGINE’])
File “/usr/local/Cellar/python/2.7.2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/utils.py”, line 33, in load_backend
return import_module(‘.base’, backend_name)
File “/usr/local/Cellar/python/2.7.2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/importlib.py”, line 35, in import_module
__import__(name)
File “/usr/local/Cellar/python/2.7.2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/backends/mysql/base.py”, line 14, in
raise ImproperlyConfigured(“Error loading MySQLdb module: %s” % e)
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: dlopen(/usr/local/Cellar/python/2.7.2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/MySQL_python-1.2.3-py2.7-macosx-10.4-x86_64.egg/_mysql.so, 2): Library not loaded: /usr/local/lib/libmysqlclient.18.dylib
Referenced from: /usr/local/Cellar/python/2.7.2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/MySQL_python-1.2.3-py2.7-macosx-10.4-x86_64.egg/_mysql.so
Reason: image not found

うわあああああ\(^o^)/
syncdb関係なかったです。

DYLD_LIBRARY_PATHを指定しよう

どうやらDYLD_LIBRARY_PATHというのを指定しないといけないようです。

myMachine:sample kyo$ python manage.py syncdb
Traceback (most recent call last):

*** 中略 ***

django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: dlopen(/Library/Python/2.7/site-packages/_mysql.so, 2): Library not loaded: libmysqlclient.18.dylib
  Referenced from: /Library/Python/2.7/site-packages/_mysql.so
  Reason: image not found

# DYLD_LIBRARY_PATH に mysql/lib の場所を設定
myMachine:sample kyo$ export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:/usr/local/mysql/lib/

参考:
Pythonでアプリを作ることになったので勉強 3日目 (Django)
http://blog.youria.jp/kyo/python/python_study_3day_with_django/

ということでボクの環境では以下を実行したらsyncdbできるようになり、runserverも動きました。

export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:/usr/local/mysql-5.5.25-osx10.6-x86_64/lib/

やったね。よかったよかった。

毎回exportしたくない

Terminalを毎回起動するたびにexportコマンド実行しないとrunserverできなくてめんどくさくなりました。
なので~/.bashrcに書いてみました。

vim ~/.bashrc
export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:/usr/local/mysql-5.5.25-osx10.6-x86_64/lib/

これでも新しく開いたTerminalではrunserverが失敗してしまいました。設定はあってる気がするのですが・・
もーいっかいググると下記サイトにあるように、.bash_profileに書けばTerminal起動時に.bashrcを読み込んでくれるみたいです。

if [ -f ~/.bashrc ] ; then
. ~/.bashrc
fi

参考:
ターミナル起動時に.bashrcを読み込むようにする
http://d.hatena.ne.jp/ruedap/20100913/mac_bash_bashrc

.bash_profileに書いたところTerminalを起動するたびにrunserverが実行できるようになりました。

よかったー

開発のプロが教える標準Django完全解説―Webアプリケーションフレームワーク (デベロッパー・ツール・シリーズ)
開発のプロが教える標準Django完全解説―Webアプリケーションフレームワーク (デベロッパー・ツール・シリーズ) 増田 泰 中居 良介 露木 誠 松原 豊

アスキー・メディアワークス 2008-06-19
売り上げランキング : 85028

Amazonで詳しく見る by G-Tools