【问题标题】:How to compile pyqt5 and python code in cython如何在 cython 中编译 pyqt5 和 python 代码
【发布时间】:2019-06-14 10:42:16
【问题描述】:

是否可以用 python3 和 pyqt5 和 cython 编译我的项目?

我创建了一个包含以下内容的 compyle.py 文件:

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
ext_modules = [
    Extension("Main",  ["Main.py"]),
    Extension("MainWindow",  ["MainWindow.py"]),
    Extension("CopyDialog",  ["CopyDialog.py"]),
    Extension("CopyDebugThread",  ["CopyDebugThread.py"])

]
setup(
    name = 'My Program Name',
    cmdclass = {'build_ext': build_ext},
    ext_modules = ext_modules
)

我根据this 运行带有此评论的文件:

python3 compile.py build_ext --inplace

但我得到了这个错误:

$ python3 compile.py build_ext --inplace
running build_ext
cythoning Main.py to Main.c
/home/groot/.local/lib/python3.6/site-packages/Cython/Compiler/Main.py:367: FutureWarning: Cython directive 'language_level' not set, using 2 for now (Py2). This will change in a later release! File: /home/groot/PythonProject/ConfigServer/Main.py
  tree = Parsing.p_module(s, pxd, full_module_name)

Error compiling Cython file:
------------------------------------------------------------
...
from PyQt5.QtWidgets import QApplication
from MainWindow import MainWindowApp

app = QApplication(sys.argv)
mainWindow = MainWindowApp()
sys.exit(app.exec())            ^
------------------------------------------------------------

Main.py:7:13: Expected an identifier
building 'Main' extension
creating build
creating build/temp.linux-x86_64-3.6
x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -I/usr/include/python3.6m -c Main.c -o build/temp.linux-x86_64-3.6/Main.o
Main.c:1:2: error: #error Do not use this file, it is the result of a failed Cython compilation.
 #error Do not use this file, it is the result of a failed Cython compilation.
  ^~~~~
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1

我认为 cython 可以编译 PyQt5 模块?我怎么能这样做?

【问题讨论】:

    标签: python python-3.x pyqt pyqt5 cython


    【解决方案1】:

    虽然python容忍使用app.exec(),但是cython更严格,解决方法是使用app.exec_()

    from PyQt5.QtWidgets import QApplication
    from MainWindow import MainWindowApp
    
    app = QApplication(sys.argv)
    mainWindow = MainWindowApp()
    sys.exit(app.exec_()) # <---
    

    【讨论】:

    • 我认为这是因为 Cython 仍然默认将代码解释为 Python 2。我猜将语言级别设置为 3 也可以(因为 exec 将不再是一个声明)跨度>
    • @DavidW 它可能是但正式的函数是 app.exec_(),尽管如前所述 app.exec() 也是可以容忍的。恕我直言,使用 exec_() 可以避免兼容性问题
    • @eyllanesc dude 编译后如何运行这些文件? paste.ubuntu.com/p/y38TZvkSfh
    • @sayreskabir Cython 不会生成一个可以自己执行的应用程序,而是一个必须导入的库。我建议您调查 Cython 是什么、它的作用以及它的用途,以便您了解执行应用程序必须做什么
    猜你喜欢
    • 2021-10-30
    • 1970-01-01
    • 1970-01-01
    • 2017-01-16
    • 1970-01-01
    • 2016-11-09
    • 2014-08-22
    • 2011-10-22
    相关资源
    最近更新 更多