安装Python版darkflow时遭遇Cython编译错误,求解决方案
解决darkflow安装时的Cython编译错误
执行pip install .安装darkflow项目时,出现Cython编译失败,报错找不到nms.pxd文件、NMS无法识别为合法标识符,最终导致metadata生成失败。
错误详情
C:\Users\navin\Desktop\smt4\Smart-traffic-control-system-with-python\darkflow>pip install . Processing c:\users\navin\desktop\smt4\smart-traffic-control-system-with-python\darkflow Preparing metadata (setup.py) ... error error: subprocess-exited-with-error × python setup.py egg_info did not run successfully. │ exit code: 1 ╰─> [60 lines of output] C:\Users\navin\Desktop\smt4\Smart-traffic-control-system-with-python\darkflow\setup.py:6: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses import imp c:\users\navin\appdata\local\programs\python\python39\lib\site-packages\Cython\Compiler\Main.py:381: FutureWarning: Cython directive 'language_level' not set, using '3str' for now (Py3). This has changed from earlier releases! File: C:\Users\navin\Desktop\smt4\Smart-traffic-control-system-with-python\darkflow\darkflow\cython_utils\cy_yolo2_findboxes.pyx tree = Parsing.p_module(s, pxd, full_module_name) Error compiling Cython file: ------------------------------------------------------------ ... cimport numpy as np cimport cython ctypedef np.float_t DTYPE_t from libc.math cimport exp from ..utils.box import BoundBox from nms cimport NMS ^ ------------------------------------------------------------ darkflow\cython_utils\cy_yolo2_findboxes.pyx:7:0: 'nms.pxd' not found Error compiling Cython file: ------------------------------------------------------------ ... cimport numpy as np cimport cython ctypedef np.float_t DTYPE_t from libc.math cimport exp from ..utils.box import BoundBox from nms cimport NMS ^ ------------------------------------------------------------ darkflow\cython_utils\cy_yolo2_findboxes.pyx:7:0: 'nms\NMS.pxd' not found Error compiling Cython file: ------------------------------------------------------------ ... if(tempc > threshold): probs[row, col, box_loop, class_loop] = tempc #NMS return NMS(np.ascontiguousarray(probs).reshape(H*W*B,C), np.ascontiguousarray(Bbox_pred).reshape(H*B*W,5)) ^ ------------------------------------------------------------ darkflow\cython_utils\cy_yolo2_findboxes.pyx:97:11: 'NMS' is not a constant, variable or function identifier Traceback (most recent call last): File "<string>", line 2, in <module> File "<pip-setuptools-caller>", line 34, in <module> File "C:\Users\navin\Desktop\smt4\Smart-traffic-control-system-with-python\darkflow\setup.py", line 73, in <module> ext_modules = cythonize(ext_modules) File "c:\users\navin\appdata\local\programs\python\python39\lib\site-packages\Cython\Build\Dependencies.py", line 1154, in cythonize cythonize_one(*args) File "c:\users\navin\appdata\local\programs\python\python39\lib\site-packages\Cython\Build\Dependencies.py", line 1321, in cythonize_one raise CompileError(None, pyx_file) Cython.Compiler.Errors.CompileError: darkflow/cython_utils/cy_yolo2_findboxes.pyx Compiling darkflow/cython_utils/nms.pyx because it changed. Compiling darkflow/cython_utils/cy_yolo2_findboxes.pyx because it changed. Compiling darkflow/cython_utils/cy_yolo_findboxes.pyx because it changed. [1/3] Cythonizing darkflow/cython_utils/cy_yolo2_findboxes.pyx [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. error: metadata-generation-failed × Encountered error while generating package metadata. ╰─> See above for output. note: This is an issue with the package mentioned above, not pip. hint: See above for details. C:\Users\navin\Desktop\smt4\Smart-traffic-control-system-with-python\darkflow>
可行解决方案
方案1:修复Cython文件的相对导入路径
报错核心原因是cy_yolo2_findboxes.pyx中对nms的导入路径不正确,按以下步骤修改:
- 打开
darkflow/cython_utils/cy_yolo2_findboxes.pyx文件 - 将第7行的
from nms cimport NMS修改为from .nms cimport NMS(添加相对路径前缀.) - 保存文件后重新执行
pip install .
方案2:指定Cython语言版本
针对报错中提到的language_level未设置问题,修改项目根目录的setup.py:
- 找到
ext_modules = cythonize(ext_modules)这一行 - 修改为
ext_modules = cythonize(ext_modules, language_level="3") - 保存后重新执行安装命令
方案3:使用兼容的Python和Cython版本
darkflow对新版本Python、Cython兼容性较差,建议降级到以下版本:
- Python 3.7或3.8(避免使用3.9及以上版本)
- Cython 0.29.x版本(避免使用3.0及以上版本)
先执行降级命令:
pip install cython==0.29.36
之后再执行pip install .
方案4:手动预编译Cython文件
先手动编译所有Cython文件,再执行安装:
cd darkflow/cython_utils cython nms.pyx cython cy_yolo2_findboxes.pyx cython cy_yolo_findboxes.pyx cd ../.. pip install .
内容的提问来源于stack exchange,提问作者navinisha mohan
相关产品推荐
相关产品推荐

