非法指令:4 when 运行 Django

Illegal instruction: 4 when running Django

我现在全新安装了 Django v1.11.10。当我 运行 python manage.py runserver 一切正常。但是当我尝试连接到 Postgres 数据库时,我安装了包 pip install psycopg2,修改了 DATABASES varibale 并且在 运行ning runserver 命令之后它失败并出现 Illegal instruction 错误:

Performing system checks...

System check identified no issues (0 silenced).
Illegal instruction: 4

这是什么?如何获取日志错误?我使用 Mac OS 10.11.6,PostgresApp(在 v9 和 v10 服务器上试过以检查错误源)。 Python 3.6.4(通过 virtualenv)。

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'mydb',
        'USER': 'sirjay',
        'PASSWORD': '',
        'HOST': 'localhost',
        'PORT': '',
    }
}

如果我设置 NAMEUSER 不正确,或者即使我关闭 Postgres.app 服务器,错误也是一样的。就像 Django 看不到 Postgres 一样。但是 phpPgAdmin 我可以连接到 Postgres 服务器。

重新安装 pip install --no-binary psycopg2 psycopg2 解决了问题

psycopg2部分是用C写的,需要编译。当您 pip install 一个包时,通常会有一个预编译的二进制轮可供下载。

出于某种原因,预编译的 psycopg2 模块包含您的 CPU 无法识别的指令(可能是因为您的处理器太旧)。您可以通过自己编译模块来解决此问题,这将确保代码在您的 CPU:

上运行
$ pip install --no-binary psycopg2 psycopg2

--no-binary psycopg2 是一个单独的选项,因此您必须指定包名称两次。您也可以在 requirements.txt 中加入:

psycopg2==a.b.c    --no-binary psycopg2

and 指出了问题,但是当 运行 建议的命令时,我收到以下错误:

 Error: pg_config executable not found.

    pg_config is required to build psycopg2 from source.  Please add the directory
    containing pg_config to the $PATH or specify the full executable path with the
    option:

        python setup.py build_ext --pg-config /path/to/pg_config build ...

    or with the pg_config option in 'setup.cfg'.

由于我对设置、配置等一无所知,因此花了一段时间才弄清楚如何操作。这是为了防止其他人遇到同样的问题:

pip uninstall psycopg2

then

export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/latest/bin/

然后

pip install --no-binary :all: psycopg2

这解决了我的问题。