python - 如何在cython中编译pyqt5和python代码

标签 python python-3.x pyqt pyqt5 cython

是否可以使用 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容忍使用app.exec(),但cython更严格,解决方案是使用app.exec_():

from PyQt5.QtWidgets import QApplication
from MainWindow import MainWindowApp

app = QApplication(sys.argv)
mainWindow = MainWindowApp()
sys.exit(app.exec_()) # <---

关于python - 如何在cython中编译pyqt5和python代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56596517/

相关文章:

python - 了解马尔可夫决策过程的值(value)迭代算法

Python 类型错误 : cannot convert the series to <class 'int' > when trying to do math on dataframe

python - 如何在将其推送到 github 之前在 django 中隐藏 google map api key ?

python - 可能替换的所有版本

Python - 打破和分组用户输入

python - "banana"< "orange"为真,"banana"< "Orange"为假,但为什么呢?

python - PyQt:没有这样的插槽

python - PyQt 启用/禁用 QComboBox 中的元素

python - 从外部函数访问 QLCDNumber 对象

python - 从我的项目中的不同路径导入字典