MVSC doesn't seem to work with Cython (in my case, at least)

33 views
Skip to first unread message

Nenner KN

unread,
Feb 18, 2025, 10:23:00 AMFeb 18
to cython-users
I tried to compile some Python code using Cython and I've wasted whole day installing and setting the stuff that was supposed the make a compilation process work. However, I've had to face certain types of errors no matter the utilities I install even by the link given in the error message

(.venv2) C:\Users\Nikita_WBGR\PycharmProjects\LLD_test_2>python setup.py build_ext --inplace
Compiling count_to_billion.pyx because it changed.
[1/1] Cytonization of count_to_billion.pyx
C:\Users\Nikita_WBGR\PycharmProjects\LLD_test_2\.venv2\Lib\site-packages\Cython\Compiler\Main.py:381: FutureWarning: Cython 'language_level' directive is not set, '3str' (Py3) is used for now. This has changed from previous releases! File: C:\Users\Nikita_WBGR\PycharmProjects\LLD_test_2\count_to_billion.pyx
  tree = Parsing.p_module(s, pxd, full_module_name)
start build_ext
creating the 'count_to_billion' extension
error: Requires Microsoft Visual C++ 14.0 or later. Get it using “Microsoft C++ Build Tools”: https://visualstudio.microsoft.com/visual-cpp-build-tools/

(.venv2) C:\Users\Nikita_WBGR\PycharmProjects\LLD_test_2>cl
Optimizing Microsoft (R) C/C++ compiler version 19.43.34808 for x64
(C) Microsoft Corporation.  All rights reserved.

Usage: cl [ parameter... ] file_name... [ /link parameter_component...].

Translated with DeepL.com (free version)

(.venv2) C:\Users\Nikita_WBGR\PycharmProjects\LLD_test_2>cl --version
Optimizing Microsoft (R) C/C++ compiler version 19.43.34808 for x64
(C) Microsoft Corporation.  All rights reserved.

cl: command line warning D9002: omission of unknown parameter “--version”
cl: command line error D8003: missing original filename

David Woods

unread,
Feb 19, 2025, 2:26:09 PMFeb 19
to cython...@googlegroups.com
There's a lot of details you've omitted here so it's very hard to know what you're actually doing, but finding the compiler is largely up to the build system, not Cython.

Generally setuptools is better at finding it than distutils, so make sure you've imported setuptools.

This tool claims to help too <https://github.com/kdschlosser/python_msvc>

Johannes Fischer

unread,
Feb 20, 2025, 8:07:59 AMFeb 20
to cython-users
Try to initialize your build env first, something like:

"C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvarsall.bat" x64

I created a setup script that always works, just name your file uiev.pyx, and save this code as uiev_compile.py :


from Cython.Compiler import Options
from setuptools import Extension, setup
from Cython.Build import cythonize
import sys
import platform
import numpy as np
import os

numpyincludefolder = np.get_include() # if you need numpy

iswindows = "win" in platform.platform().lower()
name = "uiev" # name your cython file uiev.pyx

Options.docstrings = False
Options.embed_pos_in_docstring = False
Options.generate_cleanup_code = False
Options.clear_to_none = True
Options.annotate = True
Options.fast_fail = False
Options.warning_errors = False
Options.error_on_unknown_names = True
Options.error_on_uninitialized = True
Options.convert_range = True
Options.cache_builtins = True
Options.gcc_branch_hints = True
Options.lookup_module_cpdef = False
Options.embed = False
Options.cimport_from_pyx = True
Options.buffer_max_dims = 8
Options.closure_freelist_size = 8


configdict = {
    "py_limited_api": False,
    "name": name,
    "sources": [
        name + ".pyx",
    ],
    "include_dirs": [
        numpyincludefolder, # comment that out if you don't need numpy
    ],
    "define_macros": [
        ("NPY_NO_DEPRECATED_API", 1),
        # ("NPY_1_7_API_VERSION", 0),
        # ("CYTHON_USE_DICT_VERSIONS", 0),
        # ("CYTHON_FAST_GIL", 1),
        # ("CYTHON_USE_PYLIST_INTERNALS", 1),
        # ("CYTHON_USE_UNICODE_INTERNALS", 1),
        # ("CYTHON_ASSUME_SAFE_MACROS", 1),
        # ("CYTHON_USE_TYPE_SLOTS", 1),
        # ("CYTHON_USE_PYTYPE_LOOKUP", 1),
        # ("CYTHON_USE_ASYNC_SLOTS", 1),
        # ("CYTHON_USE_PYLONG_INTERNALS", 1),
        # ("CYTHON_USE_UNICODE_WRITER", 1),
        # ("CYTHON_UNPACK_METHODS", 1),
        # ("CYTHON_USE_EXC_INFO_STACK", 1),
        # ("CYTHON_ATOMICS", 1),
    ],
    "undef_macros": [],
    "library_dirs": [],
    "libraries": [],
    "runtime_library_dirs": [],
    "extra_objects": [],
    "extra_compile_args": [
        # "/std:c++20",
    ]
    if iswindows
    else [
        "-march=native",
        "-mtune=native",
        "-std=c++2a",
        "-pthread",
    ],
    "extra_link_args": [],
    "export_symbols": [],
    "swig_opts": [],
    "depends": [],
    "language": "c++",
    "optional": None,
}
compiler_directives = {
    "binding": False,
    "boundscheck": False,
    "wraparound": False,
    "initializedcheck": False,
    "nonecheck": False,
    "overflowcheck": False,
    "overflowcheck.fold": True,
    "embedsignature": True,
    "embedsignature.format": "c",  # (c / python / clinic)
    "cdivision": True,
    "cdivision_warnings": True,
    "cpow": True,
    "always_allow_keywords": True,
    "c_api_binop_methods": False,
    "profile": False,
    "linetrace": False,
    "infer_types": True,
    "language_level": 3,  # (2/3/3str)
    "c_string_type": "bytes",  # (bytes / str / unicode)
    "c_string_encoding": "ascii",  # (ascii, default, utf-8, etc.)
    "type_version_tag": False,
    "unraisable_tracebacks": True,
    "iterable_coroutine": True,
    "annotation_typing": True,
    "emit_code_comments": True,
    "cpp_locals": False,
    "legacy_implicit_noexcept": False,
    "optimize.use_switch": True,
    "optimize.unpack_method_calls": True,
    "warn.undeclared": True,  # (default False)
    "warn.unreachable": True,  # (default True)
    "warn.maybe_uninitialized": True,  # (default False)
    "warn.unused": True,  # (default False)
    "warn.unused_arg": True,  # (default False)
    "warn.unused_result": True,  # (default False)
    "warn.multiple_declarators": True,  # (default True)
    "show_performance_hints": True,  # (default True)
}
compdi = configdict
clidict = compiler_directives

ext_modules = Extension(**configdict)

setup(
    name=name,
    ext_modules=cythonize(ext_modules, compiler_directives=compiler_directives),
)
sys.exit(0)


and run the build command: python uiev_compile.py build_ext --inplace

Reply all
Reply to author
Forward
0 new messages