在没有管理员权限的情况下使用 Cython / Microsoft Visual C++ 构建工具

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

当我决定开始学习 Cython 代码时,我一直在寻找将 C++/Qt 项目与 Python 接口的方法。 我从 Pycharm 的网站上找到 this article,说免费版本确实有基本的 Cython 支持。 我按照指南直到“一旦构建任务成功完成,就创建了 .so 文件”这句话上面的部分。因为编译器给出错误:

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

此问题已在多个堆栈溢出线程中提及。 问题是我只能使用我的工作电脑而且我没有管理员权限。 一个答案一直在给出一个答案,但我没有得到他们从哪里获得工具的答案:​​

是否可以从如何找到解决方法(没有管理员权限)来安装工具(Microsoft Visual C++ Build Tools/Microsoft Visual C++ 14.0)或其他允许的工具中获得详细答案到 运行 Cython。

我在公司 Windows 10.Python 3.10 上使用最新的 Pycharm。

编辑

我尝试了使用mingw来使用Cython的可能性。 我使用 mingw 的 Python 3.9.12。 我使用了这个 tutorial(不再是 Pycharm 的教程)。 我创建了以下文件 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")

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))

最后 运行:

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

生成了一个example.c和一个hello.cp39-mingw_x86_64.pyd;这是预期的行为。

提到的教程当然适用于 Linux,因为 .so 文件通常是 Linux 共享库。 Cython 主要需要 Windows 上的 MSVC。事实上,documentation 指出:

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.

简而言之,您可以尝试使用 MinGW,但您需要使用 MinGW-based CPython 来避免许多问题,并且由于此配置未经过测试(即实验性),您仍然可能遇到一些问题.