Cython problem (need Microsoft Visual C++ 14?)

I tried to run the code from github GitHub - tcompa/Laughlin-Metropolis at v1.0.2

It seems that I have to install something for cython?

PS C:\Users\jmzhang\Downloads\Laughlin-Metropolis-1.0.2>  python setup_cython.py build_ext --inplace
Compiling lib_laughlin_metropolis.pyx because it changed.
[1/1] Cythonizing lib_laughlin_metropolis.pyx
D:\Anaconda\Lib\site-packages\Cython\Compiler\Main.py:381: FutureWarning: Cython directive 'language_level' not set, using '3str' for now (Py3). This has changed from earlier releases! File: C:\Users\jmzhang\Downloads\Laughlin-Metropolis-1.0.2\lib_laughlin_metropolis.pyx
  tree = Parsing.p_module(s, pxd, full_module_name)
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/
PS C:\Users\jmzhang\Downloads\Laughlin-Metropolis-1.0.2> 

Yes. Cython generates C code so you need a C compiler to turn that C code into an extension module.

On Windows, the only C compiler that’s really supported is MSVC so you’re strongly recommended to use that one. That limitation is imposed by Python rather than Cython.

1 Like

I have installed visual studio 2022 to my D disk. But I still cannot let it work

PS C:\Users\jmzhang\Downloads\Laughlin-Metropolis-1.0.2> python setup_cython.py build_ext --inplace
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/

It seems that I need to configure it somehow.

The easiest solution is to run that command from a “native tools” command prompt. That makes sure that it can see Visual Studio.

Alternatively, if you edit the setup_cython.py file and replace distutils with setuptools then setuptools will set it up for you.

This is what in setup_cython.py.

from distutils.extension import Extension
from distutils.core import setup
from Cython.Distutils import build_ext


lib = 'lib_laughlin_metropolis.pyx'
basename = lib[:-4]
ext_modules = [Extension(basename, [basename + '.pyx'])]
setup(cmdclass={'build_ext': build_ext}, ext_modules=ext_modules)

You mean replacing every ‘distutils’ by ‘setuptools’?

By ‘native tools’ you mean something directly related to Visual Studio?

I am using VS code.

In the start menu there will be something like x64 native tools for VS2022.

Yes. (Although possibly you need to make some other small changes)