强制Django使用32位Python

3 投票
2 回答
1633 浏览
提问于 2025-04-17 02:19

我终于解决了在MySQL-python和OSX(10.7 64位)之间的常见问题,现在Python和MySQL可以正常工作了。我使用了32位的MySQL,并通过'arch -i386'来调用python。除了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'

export VERSIONER_PYTHON_PREFER_64_BIT=no

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

2 个回答

0

我之前在使用Snow Leopard时遇到过类似的问题,不过那时我在做所有工作时都使用了虚拟环境。如果你也在使用虚拟环境(这是使用Django的推荐方式),那么你可以强制指定虚拟环境使用的Python版本。当你创建一个新的虚拟环境时,只需输入类似下面的命令:

virtualenv --python=/Library/Frameworks/Python.framework/Versions/2.7/bin/python new-environment
1

VERSIONER_PYTHON_PREFER_32_BIT 是苹果提供的一个功能。它只适用于 Mac OS X 10.6 和 10.7 中苹果自带的 Python。根据你提供的路径来看,你使用的是一个非系统自带的 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)安装一个完整的解决方案。

撰写回答