【发布时间】:2014-03-21 18:46:05
【问题描述】:
我正在 Linux 上使用 Cython 构建一个独立的可执行文件。
我有以下代码:
import psycopg2 as pg
conn = pg.connect('dbname=**** user=**** password=****')
cur = conn.cursor()
cur.execute('SELECT version()')
print(cur.fetchone())
问题是当机器没有安装Python包psycopg2时,抛出如下异常:
Traceback (most recent call last):
File "test.py", line 2, in init test (test.c:872)
import psycopg2 as pg
ImportError: No module named 'psycopg2'
我正在使用--embed cython 标志进行构建。
我怎样才能让 Cython 也编译那个特定的包?
【问题讨论】:
-
Cython 主要用于构建扩展,而不是独立的可执行文件。要创建独立的可执行文件,您可以使用 pyinstaller。如果您详细说明您的建筑设置,我可以尝试提供帮助。我并不是说带有 --embed 选项的 cython 不能做到这一点,但是 pyinstaller 有助于将所有依赖项收集到可执行文件中。
-
可以用 Cython 编译外部 Python 模块吗?假设我想创建一个查询 PostgreSQL 数据库的函数,我使用 psycopg2 模块,还有一种方法可以自动构建该模块吗? @drodri
-
你可以找到编译标准库的this experimental script。很有可能你也可以编译
psycopg2。