Skip to content Skip to sidebar Skip to footer

Pytest Cannot Find Module On Import But Code Runs Just Fine

The goal is to use the pytest unit test framework for a Python3 project that uses Cython. This is not a plug-and-play thing, because pytest by default is not able to import the Cyt

Solution 1:

I have no idea why this is happening, but for me the easiest solution was to use the pytest-cython approach and change one or multiple things listed below in the package's setup.py file:

  • when defining your Extension for the ext_modules to include the Cython .pyx files, do not use distutils.extension.Extension but rather use setuptools.Extension

The reason why I manually create an Extension instead of using the Cython.Build.cythonize function, is not important here. But please note that for the pytest-runner approach:

  • do not use the cythonize function, but create the Extension manually

After writing this post I cannot even seem to reproduce the problem using pytest-cython anymore, which suggests that maybe something else is the cause of the problem. An additional thing you could try is to make sure that:

  • when manually creating an Extension for your .pyx module, make sure the name of the Extension is identical to the name of the module (so name it 'calculateScore' and not for instance 'package.calculateScore').

  • delete the compiled .so file corresponding to your .pyx file and then re-run.

Post a Comment for "Pytest Cannot Find Module On Import But Code Runs Just Fine"