Skip to main content
 首页 » 编程设计

python之有什么方法可以设置用 ucs2 编译的 cython

2024年10月01日8开发

Cython 将 python 代码转换为共享对象时遇到问题。

安装文件在这里:

from distutils.core import setup 
from Cython.Build import cythonize 
setup( 
    ext_modules = cythonize("hello.py") 
) 

所以在我的 Ubuntu 桌面 util 转移到 CentOS 上一切正常。

出现错误:

undefined symbol :PyUnicodeUCS4_DecodeUTF8

我用谷歌搜索,发现有很多关于此的问题,但是,几乎所有问题都说根本原因是 python 与 UCS2 或 UCS4,我理解这一点,但没有找到解决此问题的方法。

IMO,解决方法:

  1. 通过“--enable-unicode=ucs4/ucs2”重建 python 以获得正确的版本

但是我需要重新安装所有的包

  1. 从另一个具有正确 UCS 的 python 的桌面编译代码

现在,我想知道是否有办法将 Cython 设置为使用指定的 UCS 模式进行编译。

非常感谢任何建议。

谢谢。

请您参考如下方法:

首先,回答您的实际问题:

I wanna if there is way to set Cython to compile with specified UCS mode.

您可以 build a separate python installation from source并将 Cython 链接到它的标题。要查找 header ,您可以使用 python-config 工具(或用于 Python 3 的 python3-config)。它通常位于 bin 目录中,其中 python 可执行文件是:

$ # system python on my machine (macos): 
$ which python-config 
/usr/bin/python-config 
 
$ # python 3 installation 
$ which python3-config  
/Library/Frameworks/Python.framework/Versions/3.6/bin/python3-config 
 
$ python-config --cflags 
-I/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -I/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -fno-strict-aliasing -fno-common -dynamic -arch x86_64 -arch i386 -g -Os -pipe -fno-common -fno-strict-aliasing -fwrapv -DENABLE_DTRACE -DMACOSX -DNDEBUG -Wall -Wstrict-prototypes -Wshorten-64-to-32 -DNDEBUG -g -fwrapv -Os -Wall -Wstrict-prototypes -DENABLE_DTRACE 
$ python-config --ldflags 
-L/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/config -lpython2.7 -ldl -framework CoreFoundation 

将输出复制到 setup.py:

from setuptools import setup 
from setuptools.extension import Extension 
from Cython.Build import cythonize 
 
 
cflags_ucs4 = [ 
    '-I/Library/Frameworks/Python.framework/Versions/3.6/include/python3.6m', 
    '-I/Library/Frameworks/Python.framework/Versions/3.6/include/python3.6m', 
    ... 
] 
ldflags_ucs4 = [ 
    '-L/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/config-3.6m-darwin',  
    '-lpython3.6m',  
    ... 
] 
 
cflags_ucs4 = [...] 
ldflags_ucs2 = [...] 
 
 
should_build_ucs2 = False  # i.e. could be passed via sys.argv 
 
if should_build_ucs2: 
    cflags = cflags_ucs2 
    ldflags = ldflags_ucs2 
else: 
    cflags = cflags_ucs4 
    ldflags = ldflags_ucs4 
 
extensions = [ 
    Extension('hello.py', extra_compile_args=cflags, extra_link_args=ldflags), 
] 
 
setup( 
    ext_modules = cythonize(extensions) 
) 

但是,我不建议这样做,因为这样做不会赢得任何东西 - 您仍然需要构建和分发两个单独的包(一个用于 UCS2,另一个用于 UCS4),维护起来很麻烦。

相反,如果您正在构建一个应该可以安装在广泛的 Linux 发行版上的轮子(这很可能是您的实际目标),我建议您的构建符合 PEP 513 (manylinux1 packages)。我建议您通读它,因为当我遇到分发兼容 Linux 的轮子的问题时,它对我很有帮助。

现在,获得兼容 manylinux1 的 wheel 的一种方法是在您的机器上构建 wheel,然后运行 ​​auditwheel 来检查特定于平台的问题并尝试解决它们:

$ pip install auditwheel 
$ python setup.py bdist_wheel 
$ # there should be now a mypkg-myver-cp36-cp36m-linux_x86_64.whl file in your dist directory 
$ auditwheel show dist/mypkg-myver-cp36-cp36m-linux_x86_64.whl 
$ # check what warnings auditwheel produced 
$ # if there are warnings, try to repair them: 
$ auditwheel repair dist/mypkg-myver-cp36-cp36m-linux_x86_64.whl 

这应该会在 wheelhouse 目录中生成一个名为 mypkg-myver-cp36-cp36m-manylinux1_x86_64.whl 的 wheel 文件。通过运行 auditwheel show wheelhouse/mypkg-myver-cp36-cp36m-manylinux1_x86_64.whl 再次检查现在一切正常。如果 wheel 现在与 manylinux1 一致,您可以分发它并且它​​应该可以在大多数 Linux 发行版上运行(至少是那些带有 glibc 的发行版;像 Alpine 这样带有 musl 的发行版不会工作,如果你想支持它,你将需要构建一个单独的轮子)。

如果 auditwheel 无法修复您的轮子,您应该怎么办?最好的方法是拉一个由 PyPA 提供的特殊 docker 容器来构建 manylinux1-compliant wheels(这是我自己使用的):

$ docker pull https://quay.io/repository/pypa/manylinux1_x86_64 

在这个容器内构建的 wheel 可以在大多数 Linux 发行版上运行(不包括一些奇特的发行版,如 Alpine)。