我终于解决了无处不在的MySQL-python / OSX(10.7 64位)崩溃问题,使用32位MySQL并使用'arch -i386'调用python,使Python和MySQL工作正常。 Django之外的Py脚本正在查询MySQL等。但是,当我尝试启动Django并调用开发服务器时,我收到了以前使用64位Python时出现的错误:
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: dlopen(/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/_mysql.so, 2): no suitable image found. Did find:
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/_mysql.so: mach-o, but wrong architecture
在我的.bash_profile中
alias python ='arch -i386 python2.7'
导出VERSIONER_PYTHON_PREFER_64_BIT =否
export VERSIONER_PYTHON_PREFER_32_BIT = yes
所以我不明白Django是如何调用python的,以及为什么它不尊重我在.bash_profile中的内容。我需要在Django中进行哪些更改以使其调用32位Python?当我执行'python manage.py runserver'时的完整回溯是:
Traceback (most recent call last):
File "manage.py", line 14, in <module>
execute_manager(settings)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 438, in execute_manager
utility.execute()
File "/Library/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 "/Library/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 "/Library/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 "/Library/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 "/Library/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 "/Library/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 "/Library/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 "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/contrib/admin/__init__.py", line 3, in <module>
from django.contrib.admin.helpers import ACTION_CHECKBOX_NAME
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/contrib/admin/helpers.py", line 3, in <module>
from django.contrib.admin.util import (flatten_fieldsets, lookup_field,
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/contrib/admin/util.py", line 1, in <module>
from django.db import models
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/__init__.py", line 78, in <module>
connection = connections[DEFAULT_DB_ALIAS]
File "/Library/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 "/Library/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 "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/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: dlopen(/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/_mysql.so, 2): no suitable image found. Did find:
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/_mysql.so: mach-o, but wrong architecture
答案 0 :(得分:1)
VERSIONER_PYTHON_PREFER_32_BIT
环境变量是Apple提供的功能。它仅适用于Mac OS X 10.6和10.7中Apple提供的Pythons系统。从显示的路径中,您使用的是非系统Python(安装在/Library/Frameworks
中,可能来自python.org安装程序)。 VERSIONER_PYTHON_PREFER_32_BIT
对此不会有任何影响。您是否尝试使用以下内容启动开发服务器:
arch -i386 /usr/local/bin/python2.7 django-admin.py ...
如果Django在子进程中启动Python解释器(默认为64位),你仍然会遇到麻烦。你应该让所有东西都能在64位模式下运行,或者坚持使用完整的32位Python和MySQLdb链。通过安装MacPorts或Homebrew等第三方分销商提供的完整解决方案,您可以省去很多麻烦。
答案 1 :(得分:0)
我有一个similar issue with Snow Leopard,但我的所有工作都在使用虚拟环境。如果你也使用了一个(这是使用Django的推荐方法),那么你可以强制你的虚拟环境使用的python版本。当您创建新的虚拟环境时,只需说出:
virtualenv --python=/Library/Frameworks/Python.framework/Versions/2.7/bin/python new-environment