python - 强制 Django 使用 32 位 Python

标签 python mysql django macos osx-lion

我终于解决了无处不在的 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'

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

最佳答案

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 在默认为 64 位的子进程中启动 Python 解释器,您仍然可能会遇到麻烦。您应该让所有东西都在 64 位模式下工作,或者坚持使用完整的 32 位 Python 和 MySQLdb 链。通过安装第三方分销商(如 MacPorts 或 Homebrew)提供的完整解决方案,您可以省去很多麻烦。

关于python - 强制 Django 使用 32 位 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7423846/

相关文章:

python - 使用正则表达式查找括号之间的特定字符串(包括括号)

python - 在主要单元之前执行 jupyter notebook 中的特定单元

python - 为什么安装 python 包会破坏 setuptools 并导致找不到 pkg_resources?

python - 如何在类的实例上调用 list() 时让类返回列表

php - 我想批量更新表中的数据

Mysql按组处理数据

MySQL 查询,其中数据显示在行而不是列中

python - djangorest框架无法获取字段名称

python - 赋值前引用的局部变量 'form1'

python - 在django测试框架中全局设置?