使用 pip 安装包时出错:“您必须使用 Visual Studio 在 Windows 上构建 python 扩展...您需要安装 Visual Studio for C++”

Kul*_*ami 8 python windows pip dlib

我在 Windows 10 上使用 VS Code。当我尝试使用 pip 安装 Face_recognition 包时,出现以下错误:

 ERROR: Command errored out with exit status 1:
     command: 'c:\users\admin\appdata\local\programs\python\python38\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\Admin\\AppData\\Local\\Temp\\pip-install-fi__6w9v\\dlib\\setup.py'"'"'; __file__='"'"'C:\\Users\\Admin\\AppData\\Local\\Temp\\pip-install-fi__6w9v\\dlib\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\Admin\AppData\Local\Temp\pip-record-f5b3pskr\install-record.txt' --single-version-externally-managed --compile --install-headers 'c:\users\admin\appdata\local\programs\python\python38\Include\dlib'
         cwd: C:\Users\Admin\AppData\Local\Temp\pip-install-fi__6w9v\dlib\
    Complete output (60 lines):
    running install
    running build
    running build_py
    package init file 'tools\python\dlib\__init__.py' not found (or not a regular file)
    running build_ext
    Building extension for Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:57:54) [MSC v.1924 64 bit (AMD64)]
    Invoking CMake setup: 'cmake C:\Users\Admin\AppData\Local\Temp\pip-install-fi__6w9v\dlib\tools\python -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=C:\Users\Admin\AppData\Local\Temp\pip-install-fi__6w9v\dlib\build\lib.win-amd64-3.8 -DPYTHON_EXECUTABLE=c:\users\admin\appdata\local\programs\python\python38\python.exe -DCMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE=C:\Users\Admin\AppData\Local\Temp\pip-install-fi__6w9v\dlib\build\lib.win-amd64-3.8 -A x64'
    -- Building for: NMake Makefiles
    CMake Error at CMakeLists.txt:5 (message):



      !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


      You must use Visual Studio to build a python extension on windows.  If you
      are getting this error it means you have not installed Visual C++.  Note
      that there are many flavors of Visual Studio, like Visual Studio for C#
      development.  You need to install Visual Studio for C++.


      !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!




    -- Configuring incomplete, errors occurred!
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "C:\Users\Admin\AppData\Local\Temp\pip-install-fi__6w9v\dlib\setup.py", line 223, in <module>
        setup(
      File "c:\users\admin\appdata\local\programs\python\python38\lib\site-packages\setuptools\__init__.py", line 144, in setup
        return distutils.core.setup(**attrs)
      File "c:\users\admin\appdata\local\programs\python\python38\lib\distutils\core.py", line 148, in setup
        dist.run_commands()
      File "c:\users\admin\appdata\local\programs\python\python38\lib\distutils\dist.py", line 966, in run_commands
        self.run_command(cmd)
      File "c:\users\admin\appdata\local\programs\python\python38\lib\distutils\dist.py", line 985, in run_command
        cmd_obj.run()
      File "c:\users\admin\appdata\local\programs\python\python38\lib\site-packages\setuptools\command\install.py", line 61, in run
        return orig.install.run(self)
      File "c:\users\admin\appdata\local\programs\python\python38\lib\distutils\command\install.py", line 545, in run
        self.run_command('build')
      File "c:\users\admin\appdata\local\programs\python\python38\lib\distutils\cmd.py", line 313, in run_command
        self.distribution.run_command(command)
      File "c:\users\admin\appdata\local\programs\python\python38\lib\distutils\dist.py", line 985, in run_command
        cmd_obj.run()
      File "c:\users\admin\appdata\local\programs\python\python38\lib\distutils\command\build.py", line 135, in run
        self.run_command(cmd_name)
      File "c:\users\admin\appdata\local\programs\python\python38\lib\distutils\cmd.py", line 313, in run_command
        self.distribution.run_command(command)
      File "c:\users\admin\appdata\local\programs\python\python38\lib\distutils\dist.py", line 985, in run_command
        cmd_obj.run()
      File "C:\Users\Admin\AppData\Local\Temp\pip-install-fi__6w9v\dlib\setup.py", line 135, in run
        self.build_extension(ext)
      File "C:\Users\Admin\AppData\Local\Temp\pip-install-fi__6w9v\dlib\setup.py", line 172, in build_extension
        subprocess.check_call(cmake_setup, cwd=build_folder)
      File "c:\users\admin\appdata\local\programs\python\python38\lib\subprocess.py", line 364, in check_call
        raise CalledProcessError(retcode, cmd)
    subprocess.CalledProcessError: Command '['cmake', 'C:\\Users\\Admin\\AppData\\Local\\Temp\\pip-install-fi__6w9v\\dlib\\tools\\python', '-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=C:\\Users\\Admin\\AppData\\Local\\Temp\\pip-install-fi__6w9v\\dlib\\build\\lib.win-amd64-3.8', '-DPYTHON_EXECUTABLE=c:\\users\\admin\\appdata\\local\\programs\\python\\python38\\python.exe', '-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE=C:\\Users\\Admin\\AppData\\Local\\Temp\\pip-install-fi__6w9v\\dlib\\build\\lib.win-amd64-3.8', '-A', 'x64']' returned non-zero exit status 1.
    ----------------------------------------
ERROR: Command errored out with exit status 1: 'c:\users\admin\appdata\local\programs\python\python38\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\Admin\\AppData\\Local\\Temp\\pip-install-fi__6w9v\\dlib\\setup.py'"'"'; __file__='"'"'C:\\Users\\Admin\\AppData\\Local\\Temp\\pip-install-fi__6w9v\\dlib\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\Admin\AppData\Local\Temp\pip-record-f5b3pskr\install-record.txt' --single-version-externally-managed --compile --install-headers 'c:\users\admin\appdata\local\programs\python\python38\Include\dlib' Check the logs for full command output.
Run Code Online (Sandbox Code Playgroud)

我尝试在安装face_recognition之前安装dlib。

Sid*_*wal 8

答案很简单。你必须按照它说的去做。转到 VS 安装程序并安装适用于 C++ 和 C# 的 VS。原因是这些模块使用 C 代码,而使用 Visual Studio 时,您必须使其能够编译 C++ 代码


rgb*_*x00 5

对于那些不想安装整个 Visual Studio 的人,有一种更简单的方法来获取所需的 CMake 工具:

步骤 1:下载并运行 Visual Studio 2022 的构建工具https://visualstudio.microsoft.com/downloads/#build-tools-for-visual-studio-2019

步骤 2:转到“单个组件”并选择“适用于 Windows 的 C++ CMake 工具”并运行安装。

第 3 步:继续构建 Python 应用程序(例如 dlib)