Error while installing scikit-learn with pip : Preparing metadata (pyproject.toml) did not run successfully

I try to install scikit-learn==1.2.2 for the django project and I got this error.

 Collecting scikit-learn==1.2.2
  Using cached scikit-learn-1.2.2.tar.gz (7.3 MB)
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Preparing metadata (pyproject.toml) ... error
  error: subprocess-exited-with-error

  × Preparing metadata (pyproject.toml) did not run successfully.
  │ exit code: 1
  ╰─> [36 lines of output]
      Partial import of sklearn during the build process.
      Traceback (most recent call last):
        File "D:\Group Project Backend\Prediction\env\Lib\site-packages\pip\_vendor\pyproject_hooks\_in_process\", line 353, in <module>
        File "D:\Group Project Backend\Prediction\env\Lib\site-packages\pip\_vendor\pyproject_hooks\_in_process\", line 335, in main
          json_out['return_val'] = hook(**hook_input['kwargs'])
        File "D:\Group Project Backend\Prediction\env\Lib\site-packages\pip\_vendor\pyproject_hooks\_in_process\", line 149, in prepare_metadata_for_build_wheel
          return hook(metadata_directory, config_settings)
        File "C:\Users\ASUS\AppData\Local\Temp\pip-build-env-_ej3exwe\overlay\Lib\site-packages\setuptools\", line 366, in prepare_metadata_for_build_wheel
        File "C:\Users\ASUS\AppData\Local\Temp\pip-build-env-_ej3exwe\overlay\Lib\site-packages\setuptools\", line 487, in run_setup
        File "C:\Users\ASUS\AppData\Local\Temp\pip-build-env-_ej3exwe\overlay\Lib\site-packages\setuptools\", line 311, in run_setup
          exec(code, locals())
        File "<string>", line 669, in <module>
        File "<string>", line 663, in setup_package
        File "<string>", line 597, in configure_extension_modules
        File "C:\Users\ASUS\AppData\Local\Temp\pip-install-xv622fd3\scikit-learn_6df30b226b524d1caf236a6b94005795\sklearn\_build_utils\", line 47, in cythonize_extensions
        File "C:\Users\ASUS\AppData\Local\Temp\pip-install-xv622fd3\scikit-learn_6df30b226b524d1caf236a6b94005795\sklearn\_build_utils\", line 82, in basic_check_build
        File "C:\Users\ASUS\AppData\Local\Temp\pip-install-xv622fd3\scikit-learn_6df30b226b524d1caf236a6b94005795\sklearn\_build_utils\", line 38, in compile_test_program
        File "C:\Users\ASUS\AppData\Local\Temp\pip-build-env-_ej3exwe\overlay\Lib\site-packages\setuptools\_distutils\", line 343, in compile
        File "C:\Users\ASUS\AppData\Local\Temp\pip-build-env-_ej3exwe\overlay\Lib\site-packages\setuptools\_distutils\", line 253, in initialize
          vc_env = _get_vc_env(plat_spec)
        File "C:\Users\ASUS\AppData\Local\Temp\pip-build-env-_ej3exwe\overlay\Lib\site-packages\setuptools\", line 230, in msvc14_get_vc_env
          return _msvc14_get_vc_env(plat_spec)
        File "C:\Users\ASUS\AppData\Local\Temp\pip-build-env-_ej3exwe\overlay\Lib\site-packages\setuptools\", line 187, in _msvc14_get_vc_env
          raise distutils.errors.DistutilsPlatformError("Unable to find vcvarsall.bat")
      distutils.errors.DistutilsPlatformError: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools":
      [end of output]

  note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed

× Encountered error while generating package metadata.
╰─> See above for output.

note: This is an issue with the package mentioned above, not pip.
hint: See above for details.


I am using pyhton 3.12.2 in my windows machine. I am building career prediction website using React and django.I use vscode as my IDE. I train a model using google collab and it is used version 1.2.2 of the scikit-learn. I can install other packages like Django,djangorestframework but can't install scikit-learn. I'm installing everything in virtual environment.

The reason for the issue you encountered is that building the scikit-learn package from source requires Microsoft Visual C++ 14.0 or higher. Installing the required build tools is the first step towards fixing this problem. Here's how to do it:

  1. Install Microsoft C++ Build Tools : link

  2. Update pip and setuptool.

    pip install --upgrade pip setuptools

  3. Install scikit-learn

    pip install scikit-learn==1.2.2

The first version that provides wheels for Python 3.12 is scikit-learn 1.4.0. See . So do not use old version, use a newer one. Either pip install scikit-learn==1.4.2 or simply pip install scikit-learn for the latest one (currently 1.5).

Version 1.2.2 provides wheels up to Python 3.11. If you want to use 1.2.2 use older Python. Downgrade to Python 3.11.

Scikit learn requires Microsoft Visual C++ Compiler as it includes C/C++ Extensions that needs to be compiled.

Scikit-learn 1.2.2 requires Microsft Visual C++ 14.0 or Greater

Online Installer of vsc++/16 : Visual C++ Online Installer 16

Offline Installer: Offline Installer Link

Run the command pip install scikit-learn==1.2.2 again to reinstall scikit-learn.

Back to Top