Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pycharm does not recognize Cython modules located in path

I have these two lines of code:

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

from optv.tracking_framebuf cimport TargetArray

The first one is not highlighted by PyCharm (2016.2.3 professional on Ubuntu 14.04) as unresolved refference but the second line is highlighted in red underline as unresolved refference.

My TargetArray class is located in tracking_framebuf.pxd file which is located in /usr/local/lib/python2.7/dist-packages/optv/ along with .c, .pyx, .so files with the same name.

I inserted /usr/local/lib/python2.7/dist-packages/optv/ and /usr/local/lib/python2.7/dist-packages/ paths to be associated with python interpreter, but the error messages are still apearing in the editor.

Despite the error messages the file (along with others) is cythonized successfully using this setup.py script:

# -*- 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']}
)

Am I missing something on my way to get rid of these error messages and being able to view the contents of the .pxd files i place in the path?

like image 900
Max Segal Avatar asked Oct 23 '16 13:10

Max Segal


2 Answers

The problem was solved by adding /usr/local/lib/python2.7/dist-packages/ to PYTHONPATH by:

File --> Settings --> Project --> Project Structure --> Add Content Root .

like image 58
Max Segal Avatar answered Oct 21 '22 18:10

Max Segal


By default, PyCharm will ignore Cython imports unless they are part of a search path. If the module folder is white, this is a smoking gun:

enter image description here

If the folder is white, then add it to the search path:

enter image description here

Update 2017-09-18

For some reason sometimes PyCharm does not actually add directories marked as "Sources Root" to the Python path. Fix this by switching this on.

Notice the "Starting Script" in the image below. I assume that manually adding these lines to your Python script would also achieve the same result.

enter image description here

like image 26
Contango Avatar answered Oct 21 '22 20:10

Contango