I'm trying to build an example cython script, but when I run the python setup.py build_ext --inplace
command, I get an error saying that I need MS Visual C++ version 14.0 or greater. I've tried a lot of the things on related SO threads and other forums but to no avail in resolving the issue.
The specific cython script:
test.pyx
:
cpdef int test(int n):
cdef int sum_ = 0, i = 0
while i < n:
sum_ += i
i += 1
return sum_
setup.py
:
# from setuptools import setup
from distutils.core import setup
from Cython.Build import cythonize
setup(
name = "test",
ext_modules = cythonize('test.pyx'), # accepts a glob pattern
)
I'm on python 3.10.0
and cython 0.29.30
and am using Windows 10
And here is the error that I get:
C:\Users\LENOVO PC\PycharmProjects\MyProject\cython_src>py setup.py build_ext --inplace
Compiling test.pyx because it changed.
[1/1] Cythonizing test.pyx
C:\Users\LENOVO PC\AppData\Local\Programs\Python\Python310\lib\site-packages\Cython\Compiler\Main.py:369: FutureWarning: Cython directive 'language_level' not set, using 2 for now (Py2). This will change in a later release! File: C:\Users\LENOVO PC\PycharmProjects\MyProject\cython_src\test.pyx
tree = Parsing.p_module(s, pxd, full_module_name)
running build_ext
building 'test' extension
error: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/
C:\Users\LENOVO PC\PycharmProjects\MyProject\cython_src>
I've tried numerous different things:
But none of these have worked for me unfortunately, and I keep getting the same error.
I personally think the cause might be related to missing registry keys, or missing path variables, because the MSVC tools are definitely installed on my machine, but the setup script is unable to find them, but I do not know how to find out for sure.
Some additional info that might be relevant(?):
I've used Cython on the same machine before, and it used to work just fine, I had Visual Studio 2019 at this time. At some point though, I uninstalled it and upgraded to Visual Studio 2022 because I was learning C++ and wanted to use a newer C++ standard. Oddly enough, when I did this, the IDE that I use for C++ (CLion) stopped detecting the MSVC toolchain as well, and I never got it to correctly detect it again (I've been using WSL toolchain on CLion since)
Recently when I tried to use Cython again and got this error, and did a lot of digging, I realised that the two incidents might be related, so I thought that it may be worth mentioning here.
Both the main python issue and the secondary CLion thing that I mentioned were resolved with this one solution (the issues were connected after all!)
Clear the registry key that is mentioned in this SO thread: https://stackoverflow.com/a/64389979/15379178
This error had nothing to do with python (sort of) or msvc, in short, an anaconda installation had left an invalid path in my cmd's auto run regkey and it was causing a "The system cannot find the path specified" error and despite it being unrelated to python or msvc it was causing the build to fail. I am so glad I finally figured this out!