Alian713
Alian713

Reputation: 287

Cython: error while building extension: Microsoft Visual C++ 14.0 or greater is required

Short Description:

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.

Longer Description:

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:

  1. Visited the link in the error and downloaded and installed the build tools
  2. Installed multiple versions of Visual Studio (2022, 2019, 2017) CE and Build Tools
  3. Uninstalled all of the above and reinstalling MSVC 2019 CE and Build Tools from scratch
  4. Browsed through a lot of other related SO threads about this error and none of the solutions presented in them have worked for me so far, they have broadly included:
    • Building the script from the developer console
    • Updating setuptools
    • Installing numerous different components in MSVC
    • Installing numerous vc redistributables

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.

Upvotes: 5

Views: 4077

Answers (2)

Rena
Rena

Reputation: 41

I had the same issue as you (Cython: error while building extension).

I installed the latest Visual C++ but it didn't work, and I tried to follow your solution, but there was no registry key that is mentioned from the link in my registry. (I guess it's because I didn't have Visual Studio installed before.) Finally I installed MS C++ Build Tools which is mentioned in the error message, and it worked for me. (The reason I didn't consider this as the first option was because the installation required a large space.)

I know it didn't work for your case, but I'm leaving it for those like me who will search this post with the same error.

Upvotes: 1

Alian713
Alian713

Reputation: 287

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!

Upvotes: 2

Related Questions