Skip to content

BUG: ModuleNotFoundError: No module named 'distutils.msvccompiler' on Linux #27405

@PinkPandaKatie

Description

@PinkPandaKatie

Describe the issue:

The module numpy/distutils/command/config.py unconditionally imports numpy.distutils.mingw32ccompiler, which unconditionally imports distutilis.msvccompiler. This module is not present on Linux, for obvious reasons.

The only function that config.py imports from mingw32ccompiler is get_manifest, which it only calls if self.compiler.compiler_type == 'mingw32', which is plausible if cross compiling, I guess.

The only function that mingw32ccompiler imports from distutils.msvccompiler is get_build_version as get_build_msvc_version. mingw32ccompiler.generate_manifest,calls this function, and is perfectly capable of handling if it returnsNone`, indicating that MSVC is not installed.

The solution is as simple as catching ImportError when attempting to import distutils.msvccompiler and providing a stub for get_build_msvc_version which returns None:

try:
    from distutils.msvccompiler import get_build_version as get_build_msvc_version
except ImportError:
    def get_build_msvc_version():
        return None

I'll have a PR for this in just a few minutes.

Reproduce the code example:

import numpy.distutils.core

Error message:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3/dist-packages/numpy/distutils/core.py", line 24, in <module>
    from numpy.distutils.command import config, config_compiler, \
  File "/usr/lib/python3/dist-packages/numpy/distutils/command/config.py", line 19, in <module>
    from numpy.distutils.mingw32ccompiler import generate_manifest
  File "/usr/lib/python3/dist-packages/numpy/distutils/mingw32ccompiler.py", line 30, in <module>
    from distutils.msvccompiler import get_build_version as get_build_msvc_version
ModuleNotFoundError: No module named 'distutils.msvccompiler'

Python and NumPy Versions:

1.21.5
3.10.12 (main, Jul 29 2024, 16:56:48) [GCC 11.4.0]

Runtime Environment:

No response

Context for the issue:

I found this while creating a setup.py which uses Cython.Build.cythonize to auto-build a Cython module. The module itself doesn't even use numpy, but since it is installed, there is some hook which Cython picks up on which causes it to pull in numpy.distutils.core, which leads to the given error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions