ModuleNotFoundError when installing Cython module through pip on Windows, but not on Linux

330 views Asked by At

I'm trying to build a python module in the folder mymodule containing some cython code. However, on Windows, installing it via pip, it cannot find Cython even though it is installed. On Linux, no such problem occurs.

As a minimal example, let's say I have a cython file calculations.pyx and a setup.py.

mymodule
|
|-- calculations.pyx
+-- setup.py

The setup.py looks like this:

from setuptools import setup, Extension
from Cython.Build import cythonize

setup(
    name='mymodule',
    version='0.1',
    
    install_requires=['Cython'],
    ext_modules=cythonize(Extension(
        'calculations',
        sources=['calculations.pyx'],
    )),
)

When I'm installing this module (pip install -e mymodule) on Linux, the module installs correctly and cython builds the binaries. When I'm trying to install the module on Windows, i get a ModuleNotFoundError: No module named 'Cython' error. However, if I just build the extension myself using python setup.py build_ext --inplace, it works correctly.

I have verified that Cython is already installed. Even if it wasn't installed, it should be installed after running the pip install -e mymodule command, since it is listed in the install_requires list. I have verified this on Linux, and there Cython is correctly installed as a depenency if it wasn't present before running the setup.py.

What could be the issue here? Also, I have only one Python version installed, not multiple.

1

There are 1 answers

0
sunnytown On

For anyone with the same issue: I found a solution that made it work, even though I don't understand why. By putting a pyproject.toml file in the root directory mymodule, which tells pip the packages required to even run setup.py. Source: https://cython.readthedocs.io/en/latest/src/userguide/source_files_and_compilation.html

The toml file:

[build-system]
requires = ["setuptools", "wheel", "Cython"]

Now it works and compiles. However, I still do not understand why it worked on Linux and also why it didn't work on Windows even though I had cython alredy installed.