Home > Back-end >  Import conflict for Cython library when running unittests
Import conflict for Cython library when running unittests

Time:06-04

I'm working on a Python library written on C, Python and Cython. I wrote tests before for the same library when it was written only on python, but now the library needs to be compiled before it can be imported.

When I try to import the library in the unittest file after building it using python3 setup.py install I encounter ImportError which states that the Cython file cannot be imported.

The reason is because the file it tries to import, is the file that was not compiled yet. The library was compiled, but the import system prefers the files under the same project directory and not from site-packages.

What can I do in such situations? I want to be able to run my unittests both locally and on a CI.

Here's my project structure and where there errors occur:

lib-name:
    -lib-name/
        module.pyx
        main.py (imports module.pyx)
    -tests/
        test_lib_name.py (imports lib-name, raises ImportError because main.py can't import module.pyx)

Thanks!

CodePudding user response:

Before importing do this(so that the lib doesn't get loaded from the current directory.):

sys.path.remove('')

After importing do this(back to default):

sys.path.append('')

  • Related