我尝试为 django 项目安装 scikit-learn==1.2.2 但出现此错误。
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\_in_process.py", line 353, in <module>
main()
File "D:\Group Project Backend\Prediction\env\Lib\site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py", 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\_in_process.py", 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\build_meta.py", line 366, in prepare_metadata_for_build_wheel
self.run_setup()
File "C:\Users\ASUS\AppData\Local\Temp\pip-build-env-_ej3exwe\overlay\Lib\site-packages\setuptools\build_meta.py", line 487, in run_setup
super().run_setup(setup_script=setup_script)
File "C:\Users\ASUS\AppData\Local\Temp\pip-build-env-_ej3exwe\overlay\Lib\site-packages\setuptools\build_meta.py", 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\__init__.py", line 47, in cythonize_extensions
basic_check_build()
File "C:\Users\ASUS\AppData\Local\Temp\pip-install-xv622fd3\scikit-learn_6df30b226b524d1caf236a6b94005795\sklearn\_build_utils\pre_build_helpers.py", line 82, in basic_check_build
compile_test_program(code)
File "C:\Users\ASUS\AppData\Local\Temp\pip-install-xv622fd3\scikit-learn_6df30b226b524d1caf236a6b94005795\sklearn\_build_utils\pre_build_helpers.py", line 38, in compile_test_program
ccompiler.compile(
File "C:\Users\ASUS\AppData\Local\Temp\pip-build-env-_ej3exwe\overlay\Lib\site-packages\setuptools\_distutils\_msvccompiler.py", line 343, in compile
self.initialize()
File "C:\Users\ASUS\AppData\Local\Temp\pip-build-env-_ej3exwe\overlay\Lib\site-packages\setuptools\_distutils\_msvccompiler.py", 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\msvc.py", 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\msvc.py", 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": https://visualstudio.microsoft.com/visual-cpp-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.
`
我在我的 Windows 机器上使用 pyhton 3.12.2。我正在使用 React 和 django 构建职业预测网站。我使用 vscode 作为我的 IDE。我使用 google collab 训练一个模型,它使用 scikit-learn 的 1.2.2 版本。我可以安装其他软件包,如 Django、djangorestframework,但无法安装 scikit-learn。我正在虚拟环境中安装所有内容。
您遇到的问题的原因是,从源代码构建 scikit-learn 包需要 Microsoft Visual C++ 14.0 或更高版本。安装所需的构建工具是解决此问题的第一步。操作方法如下:
安装 Microsoft C++ 构建工具:链接
更新 pip 和 setuptool。
pip 安装--升级 pip setuptools
安装 scikit-learn
pip 安装 scikit-learn==1.2.2
第一个为 Python 3.12 提供轮子的版本是 scikit-learn 1.4.0。请参阅https://pypi.org/project/scikit-learn/1.4.0/#files。所以不要使用旧版本,使用较新的版本。要么
pip install scikit-learn==1.4.2
或者只是pip install scikit-learn
最新的(当前为 1.5)。版本 1.2.2提供了直至 Python 3.11 的轮子。如果您想使用 1.2.2,请使用较旧的 Python。降级到 Python 3.11。
Scikit learn 需要 Microsoft Visual C++ 编译器,因为它包含需要编译的 C/C++ 扩展。
vsc++/16 在线安装程序:Visual C++ 在线安装程序 16
离线安装程序:离线安装程序链接
再次运行命令
pip install scikit-learn==1.2.2
以重新安装 scikit-learn。