PyCharm:导入使用动态库本身的cython模块 - ImportError&代码完成

时间:2017-06-14 10:12:34

标签: python cython importerror

我有一个cython模块,它是一个与数据库通信的c ++库的包装器。该库名为libpersistence.so。该库本身与libhiredis.so动态链接。 我在libpersistence.so周围编写了一个cython包装器来创建一个py_persistence.cpython-35m-x86_64-linux-gnu.so文件,作为我的python脚本中的模块导入,以使用cpp持久层。

但是,在导入py_persistence模块时,PyCharm中存在两个问题:

  1. PyCharm无法识别从模块导入Persistence类。这也意味着没有代码完成
  2. 运行我的测试会导致ImportError,无法打开libpersistence.so文件共享对象文件。
  3. 我使用以下目录结构。

    top_level/
        indexing_service/
            bin/
                libhiredis.so.0.13
                libpersistence.so
            kCore/
                include/
                    hiredis/
                    persistence/
    
        query_service/
            __init__
            runpython.sh
            py_persistence/
                __init__
                py_persistence.cpython-35m-x86_64-linux-gnu.so
                py_persistence.pxd
                py_persistence.pyx
                setup.py
                setup.sh
                test/
                    test_persistence.py
    

    setup.sh文件只指定python3 setup.py build_ext --inplace setup.py文件如下:

    from distutils.core import setup
    from Cython.Distutils import build_ext, Extension
    
    setup(
            cmdclass = { 'build_ext' : build_ext },
            ext_modules = [
                Extension(
                    'py_persistence',
                    sources=['py_persistence.pyx'],
                    libraries=['persistence'],
                    language='c++',
                    extra_link_args=['-L../../indexing_service/bin/'],
                    runtime_library_dirs=['../../indexing_service/bin/'],
                    extra_compile_args=[
                        '-std=c++14',
                        '-I../../indexing_service/kCore/include/persistence',
                        '-I../../indexing_service/kCore/include/hiredis'
                    ],
                    cython_directives={
                        'c_string_type':'unicode',
                        'c_string_encoding':'utf8',
                    },
                )],
    )
    

    因此,在构建cython py_persistence库时,它会链接到libpersistence.solibhiredis.so.0.13文件以进行动态链接。这很有效,这可以编译并正确构建py_persistence.cpython-35m-x86_64-linux-gnu.so文件。

    当我使用runpython.py帮助程序脚本运行我的测试时,我写的一切都很好。 runpython.py明确地将所需目录添加到运行时库路径:

    export PYTHONPATH=$PYTHONPATH:.
    export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:../indexing_service/bin/:.
    python3 ${@:1}
    

    运行测试: ./runpython.sh -m unittest discover py_persistence/test -v

    这完美无缺。 然而,让这个设置工作,以便我可以从PyCharm运行它给我带来很多麻烦。我似乎无法正确地规范路径。在test_persistence.py文件中,我在导入py_persistence库时得到ImportError

    导入代码:

    from py_persistence.py_persistence import Persistence
    

    错误:

    Error
    Traceback (most recent call last):
      File "/usr/lib/python3.5/unittest/case.py", line 58, in testPartExecutor
        yield
      File "/usr/lib/python3.5/unittest/case.py", line 600, in run
        testMethod()
      File "/usr/lib/python3.5/unittest/loader.py", line 34, in testFailure
        raise self._exception
    ImportError: Failed to import test module: test_persistence
    Traceback (most recent call last):
      File "/usr/lib/python3.5/unittest/loader.py", line 428, in _find_test_path
        module = self._get_module_from_name(name)
      File "/usr/lib/python3.5/unittest/loader.py", line 369, in _get_module_from_name
        __import__(name)
      File "/home/tom/work/dev/alexandria.works/query_service/py_persistence/test/test_persistence.py", line 3, in <module>
        from py_persistence.py_persistence import Persistence
    ImportError: libpersistence.so: cannot open shared object file: No such file or directory
    

    我已经尝试将indexing_service/bin/文件夹添加到PyCharm中针对虚拟环境的路径:

    Interpreter Paths in PyCharm

    用于运行测试的运行配置如下:

    Run Configuration

    我遇到的问题是ImportError以及代码完成无效的事实。 我对此处的其他内容感到茫然...在sys.path文件中打印test_persistence.py告诉我indexing_service/bin/文件夹位于路径上。

    ['/home/tom/Applications/pycharm-2017.1.1/helpers/pycharm', 
    '/home/tom/work/dev/alexandria.works/query_service/py_persistence/test', 
    '/home/tom/work/dev/alexandria.works/indexing_service/bin', 
    '/home/tom/work/dev/alexandria.works/query_service', 
    '/home/tom/Applications/pycharm-2017.1.1/helpers/pycharm', 
    '/home/tom/work/dev/alexandria.works/query_service/dev-env/lib/python35.zip', 
    '/home/tom/work/dev/alexandria.works/query_service/dev-env/lib/python3.5', 
    '/home/tom/work/dev/alexandria.works/query_service/dev-env/lib/python3.5/plat-x86_64-linux-gnu', 
    '/home/tom/work/dev/alexandria.works/query_service/dev-env/lib/python3.5/lib-dynload', '/usr/lib/python3.5', 
    '/usr/lib/python3.5/plat-x86_64-linux-gnu', 
    '/home/tom/work/dev/alexandria.works/query_service/dev-env/lib/python3.5/site-packages']
    

    有人知道我错过了什么吗?

0 个答案:

没有答案