【问题标题】:Pycharm does not recognize Cython modules located in pathPycharm 无法识别路径中的 Cython 模块
【发布时间】:2017-09-18 16:36:02
【问题描述】:

我有这两行代码:

from libc.stdlib cimport malloc, calloc, realloc, free

from optv.tracking_framebuf cimport TargetArray

PyCharm(Ubuntu 14.04 上的 2016.2.3 专业版)未将第一行突出显示为未解析的引用,但第二行以红色下划线突出显示为未解析的引用。

我的TargetArray 类位于tracking_framebuf.pxd 文件中,该文件位于/usr/local/lib/python2.7/dist-packages/optv/ 以及同名的.c、.pyx、.so 文件中。

我插入了/usr/local/lib/python2.7/dist-packages/optv//usr/local/lib/python2.7/dist-packages/ 路径以与python解释器关联,但错误消息仍然出现在编辑器中。

尽管有错误消息,但使用此 setup.py 脚本成功地对文件(以及其他文件)进行了cythonized:

# -*- coding: utf-8 -*-
from distutils.core import setup
from Cython.Distutils import build_ext
from Cython.Distutils.extension import Extension

import numpy as np
import os
inc_dirs = [np.get_include(), '.']

def mk_ext(name, files):
    return Extension(name, files, libraries=['optv'], include_dirs=inc_dirs,
        pyrex_include_dirs=['.'])

ext_mods = [
    mk_ext("optv.tracking_framebuf", ["optv/tracking_framebuf.pyx"]),
    mk_ext("optv.parameters", ["optv/parameters.pyx"]),
    mk_ext("optv.calibration", ["optv/calibration.pyx"]),
    mk_ext("optv.transforms", ["optv/transforms.pyx"]),
    mk_ext("optv.imgcoord", ["optv/imgcoord.pyx"]),
    mk_ext("optv.image_processing", ["optv/image_processing.pyx"]),
    mk_ext("optv.segmentation", ["optv/segmentation.pyx"]),
    mk_ext("optv.orientation", ["optv/orientation.pyx"])
]

setup(
    name="optv",
    cmdclass = {'build_ext': build_ext},
    packages=['optv'],
    ext_modules = ext_mods,
    package_data = {'optv': ['*.pxd']}
)

我在摆脱这些错误消息并能够查看我放置在路径中的 .pxd 文件的内容的过程中是否遗漏了什么?

【问题讨论】:

  • 您的设置可能有问题...您是如何安装的?如果您想从非默认 Python 包文件夹运行,请尝试更改将编译后的包路径添加到环境变量 PYTHONPATH

标签: python pycharm cython


【解决方案1】:

通过将/usr/local/lib/python2.7/dist-packages/ 添加到 PYTHONPATH 解决了问题:

文件 --> 设置 --> 项目 --> 项目结构 --> 添加内容根目录。

【讨论】:

  • 通过添加我的 venv 的站点包,PyCharm 现在可以识别 Cython 导入。
【解决方案2】:

默认情况下,PyCharm 将忽略 Cython 导入,除非它们是搜索路径的一部分。如果模块文件夹是白色的,这是一个确凿的证据:

如果文件夹是白色的,则将其添加到搜索路径中:

2017-09-18 更新

由于某种原因,有时 PyCharm 并没有真正将标记为“Sources Root”的目录添加到 Python 路径中。通过打开它来解决这个问题。

请注意下图中的“启动脚本”。我假设手动将这些行添加到您的 Python 脚本中也会获得相同的结果。

【讨论】:

    猜你喜欢
    • 2022-08-08
    • 1970-01-01
    • 2020-08-08
    • 2021-05-12
    • 1970-01-01
    • 1970-01-01
    • 2015-09-22
    相关资源
    最近更新 更多