python - High Sierra + Python + Postgresql 错误 : Illegal instruction: 4

标签 python python-3.x macos postgresql psycopg2

我可以通过命令行连接到 Postgres 没问题,但是如果我尝试使用 psycopg2 模块通过 Python 连接,我会收到以下错误。有趣的是,我刚刚尝试连接 PSeqal.app,它因同样的错误而崩溃。

环境:

  • 苹果操作系统:10.13.3
  • Xcode 9.2
  • python :3.6.4
    • 连接模块:psycopg2
  • PostgreSQL:10.3

脚本:

这是我尝试通过 Python 连接到 Postgresql 的简单连接脚本:

def connect(self, params):
    """ Connect to the PostgreSQL database server """

    conn = None
    try:
        # connect to the PostgreSQL server
        logging.info('Connecting to the PostgreSQL database...')
        conn = psycopg2.connect(**params)

        # create a cursor
        self.cursor = conn.cursor()

        except (Exception, psycopg2.DatabaseError) as error:
            print(error)
        finally:
            if conn is not None:
                conn.close()
                logging.warning('Database connection closed.')

错误:

脚本输出:

$ python3 testdb.py
Illegal instruction: 4

Apple 错误(部分):

Process:               Python [79534]
Path:                  /usr/local/Cellar/python3/3.6.4_2/Frameworks/Python.framework/Versions/3.6/Resources/Python.app/Contents/MacOS/Python
Identifier:            Python
Version:               3.6.4 (3.6.4)
Code Type:             X86-64 (Native)
Parent Process:        bash [657]
Responsible:           Python [79534]
User ID:               501

Date/Time:             2018-03-02 15:30:27.642 +1300
OS Version:            Mac OS X 10.13.3 (17D102)
Report Version:        12
Anonymous UUID:        xx


Time Awake Since Boot: 21000 seconds

System Integrity Protection: disabled

Crashed Thread:        0  Dispatch queue: com.apple.main-thread

Exception Type:        EXC_BAD_INSTRUCTION (SIGILL)
Exception Codes:       0x0000000000000001, 0x0000000000000000
Exception Note:        EXC_CORPSE_NOTIFY

Termination Signal:    Illegal instruction: 4
Termination Reason:    Namespace SIGNAL, Code 0x4
Terminating Process:   exc handler [0]

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread 
0   libpq.5.10.dylib                0x00000001118e8d27 initPQExpBuffer + 32
1   libpq.5.10.dylib                0x00000001118dba93 PQconninfoParse + 43
2   _psycopg.cpython-36m-darwin.so  0x0000000111897cf1 psyco_parse_dsn + 113
3   org.python.python               0x000000010fd4681a _PyCFunction_FastCallDict + 463
4   org.python.python               0x000000010fdaad8e call_function + 489
5   org.python.python               0x000000010fda3c43 _PyEval_EvalFrameDefault + 4811
6   org.python.python               0x000000010fdab4f0 _PyEval_EvalCodeWithName + 1719
7   org.python.python               0x000000010fda293d PyEval_EvalCodeEx + 57
8   org.python.python               0x000000010fd2ec76 function_call + 339
9   org.python.python               0x000000010fd0e9f2 PyObject_Call + 101
10  org.python.python               0x000000010fda3eac _PyEval_EvalFrameDefault + 5428
11  org.python.python               0x000000010fdab4f0 _PyEval_EvalCodeWithName + 1719
12  org.python.python               0x000000010fda293d PyEval_EvalCodeEx + 57
13  org.python.python               0x000000010fd2ec76 function_call + 339
14  org.python.python               0x000000010fd0e9f2 PyObject_Call + 101
15  org.python.python               0x000000010fda3eac _PyEval_EvalFrameDefault + 5428
16  org.python.python               0x000000010fdabe93 _PyFunction_FastCall + 121
17  org.python.python               0x000000010fdaad65 call_function + 448
18  org.python.python               0x000000010fda3c43 _PyEval_EvalFrameDefault + 4811
19  org.python.python               0x000000010fdabe93 _PyFunction_FastCall + 121
20  org.python.python               0x000000010fd0eb71 _PyObject_FastCallDict + 196
21  org.python.python               0x000000010fd0ec94 _PyObject_Call_Prepend + 156
22  org.python.python               0x000000010fd0e9f2 PyObject_Call + 101
23  org.python.python               0x000000010fd598ae slot_tp_init + 57
24  org.python.python               0x000000010fd5683c type_call + 184
25  org.python.python               0x000000010fd0eb3c _PyObject_FastCallDict + 143
26  org.python.python               0x000000010fdaad5e call_function + 441
27  org.python.python               0x000000010fda3c43 _PyEval_EvalFrameDefault + 4811
28  org.python.python               0x000000010fdabe93 _PyFunction_FastCall + 121
29  org.python.python               0x000000010fd0eb71 _PyObject_FastCallDict + 196
30  org.python.python               0x000000010fd0ec94 _PyObject_Call_Prepend + 156
31  org.python.python               0x000000010fd0e9f2 PyObject_Call + 101
32  org.python.python               0x000000010fd598ae slot_tp_init + 57
33  org.python.python               0x000000010fd5683c type_call + 184
34  org.python.python               0x000000010fd0eb3c _PyObject_FastCallDict + 143
35  org.python.python               0x000000010fdaad5e call_function + 441
36  org.python.python               0x000000010fda3c43 _PyEval_EvalFrameDefault + 4811
37  org.python.python               0x000000010fdab4f0 _PyEval_EvalCodeWithName + 1719
38  org.python.python               0x000000010fda28fe PyEval_EvalCode + 42
39  org.python.python               0x000000010fdcb24e run_mod + 54
40  org.python.python               0x000000010fdca26f PyRun_FileExFlags + 160
41  org.python.python               0x000000010fdc994c PyRun_SimpleFileExFlags + 285
42  org.python.python               0x000000010fddd770 Py_Main + 3484
43  org.python.python               0x000000010fd01e1d 0x10fd00000 + 7709
44  libdyld.dylib                   0x00007fff6c260115 start + 1

Thread 0 crashed with X86 Thread State (64-bit):
rax: 0x00007f86c3db9200  rbx: 0x00007ffedfefe3a0  rcx: 0x0000000000000100  rdx: 0x0000000000010000
rdi: 0x0000000000000b93  rsi: 0x00000000ffff0001  rbp: 0x00007ffedfefe390  rsp: 0x00007ffedfefe380
r8: 0x000000006c3db930   r9: 0x000000000000000f  r10: 0x00000000ffff0000  r11: 0x00007f86c3d00000
r12: 0x0000000110665738  r13: 0x0000000111c68510  r14: 0x0000000111cb80b0  r15: 0x00007ffedfefe3a0
rip: 0x00000001118e8d27  rfl: 0x0000000000010206  cr2: 0x00000001118ef90e

Logical CPU:     2
Error Code:      0x00000000
Trap Number:     6

到目前为止我尝试了什么:

  • 将 Python 从 3.5.1 升级到 3.6.4
    • 重建所有依赖库,包括 psycopg2 以确保它们与 High Sierra 兼容(我希望如此)
  • 从头开始重新安装 Postgres
  • 升级的 Xcode + 工具

所以我对接下来要尝试什么感到有点茫然?还有其他人遇到过这个吗?任何指向哪里看的指针?任何能帮助我完成这项工作的东西都将不胜感激,谢谢!

最佳答案

所以看起来 psycopg2 库是罪魁祸首,这为我修复了它:

$pip3 uninstall psycopg2
...
Successfully uninstalled psycopg2-binary-2.7.4

$pip3 install psycopg2-binary --no-binary :all:
Collecting psycopg2-binary Downloading psycopg2-binary-2.7.4.tar.gz (426kB) 100% |---| 430kB 649kB/s Skipping bdist_wheel for psycopg2-binary, due to binaries being disabled for it. Installing collected packages: psycopg2-binary Running setup.py install for psycopg2-binary ... done
Successfully installed psycopg2-binary-2.7.4

$python3 test.py
Connecting to the PostgreSQL database...
All good.
Database connection closed.

我认为原因与我试图在 2010 时代的 MBP 上构建 psycopg2 的事实有关,它与硬件存在兼容性问题 - 可能是这台机器拥有的 2.66 GHz Intel Core i7 处理器。

感谢@Laurenz Albe 和@joop 的快速回复、指导和见解,你们帮助我缩小了这个答案的范围,我希望这个答案在这份工作期间保持稳定 ;) 干杯!

关于python - High Sierra + Python + Postgresql 错误 : Illegal instruction: 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49062045/

相关文章:

python - 将属性动态设置为Python类中的函数

python-3.x - 我无法在hackerrank python中提交此代码?

通过R中的符号链接(symbolic link)读取数据

python - 如何检查要完成下载 Python3 的文件?

python - 使用 PyMSSQL 执行引用链接服务器的存储过程时出错

python - 要在 Google App Engine 上托管静态 (HTML) 网站,app.yaml 文件中应该包含什么?

python - Python 中的连续 'is' 运算符

python - 从 np.datetime64 转换为浮点年份

xcode - GFortran错误: ld: library not found for -lSystem when trying to compile

macos - 运行私有(private) docker 镜像 : standard_init_linux. go:207 时出错:执行用户进程导致 "exec format error"