Search code examples
visual-c++cython

Use Cython / Microsoft Visual C++ Build Tools without admin rights


I was looking for ways to interface C++/Qt projects with Python when I decided I might start learning Cython code. I found this article from Pycharm's website, saying that the free version does have basic Cython support. I followed the guide up to the part above the sentence "Once the build task successfully completes, the .so file is created." as the compiler gives an error:

error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": http://landinghub.visualstudio.com/visual-cpp-build-tools

This problem has been mentioned on multiple stack overflow thread. The problem is that I can only use my work PC and I do not have admin rights. An answer has been giving an answer which I did not get where they got the tools from: https://stackoverflow.com/a/47794180/14729172

Is it possible to get a detailed answer from how to find a work-around (without admin rights) to install the tool (Microsoft Visual C++ Build Tools/Microsoft Visual C++ 14.0), or another tool that allows to run Cython.

I am using latest Pycharm on Python 3.10 on a company Windows 10.

Edit

I tried the possibility of using mingw to use Cython. I use mingw's Python 3.9.12. I used this tutorial (not Pycharm's tutorial anymore). I created the following file example.pyx:

from libc.math cimport pow
cdef double square_and_add (double x):
    return pow(x, 2.0) + x
cpdef print_result (double x):
    print("Test")

In setup.py:

from Cython.Build import cythonize
from distutils.core import setup, Extension
ext = Extension(name="hello", sources=["example.pyx"])
setup(ext_modules=cythonize(ext))

And finally ran:

python setup.py build_ext --inplace --compiler=mingw32

It generated a example.c and a hello.cp39-mingw_x86_64.pyd; which is the expected behavior.


Solution

  • The mentioned tutorial is certainly for Linux since .so files are typically Linux shared libraries. Cython mostly requires MSVC on Windows. Indeed, the documentation states:

    The CPython project recommends building extension modules (including Cython modules) with the same compiler that Python was built with. This is usually a specific version of Microsoft Visual C/C++ (MSVC) - see https://wiki.python.org/moin/WindowsCompilers. MSVC is the only compiler that Cython is currently tested with on Windows. A possible alternative is the open source MinGW (a Windows distribution of gcc). See the appendix for instructions for setting up MinGW manually.

    Put it shortly, you can try with MinGW, but you need to use a MinGW-based CPython to avoid many issues and you could still have some issue due to this configuration not being tested (ie. experimental).