自定义包安装导致 subprocess.CalledProcessError -> No module named pip

问题描述 投票:0回答:1

重要(?)细节

  • Windows 10,版本 10.0.19045
  • Python 3.12.2
  • PyCharm 2024.1(专业版)
  • GitBash 2.45.1.windows.1

设置

我正在尝试在虚拟环境中使用我的包安装

pygraphviz

我编写了一个自定义

install
子类,供
setuptools
在软件包安装过程中使用。

"""Contents of custom_install.py"""

from os import getenv
from platform import system
import subprocess
import sys

from setuptools.command.install import install


class InstallPygraphviz(install):
    """Custom command to install pygraphviz package.

    Reference:
    https://pygraphviz.github.io/documentation/stable/install.html#windows
    """

    def run(self):
        python_exe = sys.executable
        subprocess.check_call([python_exe, "-m", "ensurepip"])
        pip_command = [python_exe, "-m", "pip", "install"]
        if system() == "Windows":
            graphviz_path = getenv("GRAPHVIZ_PATH")
            if graphviz_path is None:
                raise ValueError("GRAPHVIZ_PATH is not set")

            include_path = f"{graphviz_path}/include"
            lib_path = f"{graphviz_path}/lib"
            pip_command.extend(
                [
                    "--config-settings='--global-option=build_ext'",
                    f"--config-settings='--global-option=-I{include_path}'",
                    f"--config-settings='--global-option=-L{lib_path}'",
                ]
            )

        pip_command.append("pygraphviz")
        subprocess.check_call(pip_command)
        install.run(self)

根据这个 github 问题,我了解到我可以使用 pyproject.toml 来设置我的配置设置,而不是 setup.py。

[build-system]
requires = ["setuptools>=70.0.0", "setuptools-scm>=8.1.0"]
build-backend = "setuptools.build_meta"

[project]
name = "awesome"
requires-python = ">=3.12"
dynamic = ["version"]

[tool.setuptools.dynamic]
version = {attr = "awesome.__version__"}

[tool.setuptools.packages.find]
where = ["src"]

[tool.setuptools.cmdclass]
install = "awesome.custom_install.InstallPygraphviz"

这是我的包装布局:

pyproject.toml     
+---src
    +---awesome
    |   |   custom_install.py
    |   |   __init__.py

重现问题

  1. 我创建并激活虚拟环境:

    py -3.12 -m venv env
    
    source env/Scripts/activate
    
  2. cd
    进入项目目录并运行以下命令:

    pip install .
    
  3. 错误

    Processing c:\users\user\ado\test
      Installing build dependencies ... done
      Getting requirements to build wheel ... done
      Installing backend dependencies ... done
      Preparing metadata (pyproject.toml) ... done
    Building wheels for collected packages: awesome
      Building wheel for awesome (pyproject.toml) ... error
      error: subprocess-exited-with-error
    
      × Building wheel for awesome (pyproject.toml) did not run successfully.
      │ exit code: 1
      ╰─> [66 lines of output]
          running bdist_wheel
          running build
          running build_py
          creating build
          creating build\lib
          creating build\lib\awesome
          copying src\awesome\custom_install.py -> build\lib\awesome
          copying src\awesome\__init__.py -> build\lib\awesome
          running egg_info
          writing src\awesome.egg-info\PKG-INFO
          writing dependency_links to src\awesome.egg-info\dependency_links.txt
          writing top-level names to src\awesome.egg-info\top_level.txt
          ERROR setuptools_scm._file_finders.git listing git files failed - pretending there aren't any
          reading manifest file 'src\awesome.egg-info\SOURCES.txt'
          writing manifest file 'src\awesome.egg-info\SOURCES.txt'
          installing to build\bdist.win-amd64\wheel
          running install
          Looking in links: c:\Users\user\AppData\Local\Temp\tmpyagl6bqz
          Processing c:\users\user\appdata\local\temp\tmpyagl6bqz\pip-24.0-py3-none-any.whl
          Installing collected packages: pip
          Successfully installed pip-24.0
          C:\Users\user\ADO\test\env\Scripts\python.exe: No module named pip
          Traceback (most recent call last):
            File "C:\Users\user\ADO\test\env\Lib\site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py", line 353, in <module>
              main()
            File "C:\Users\user\ADO\test\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 "C:\Users\user\ADO\test\env\Lib\site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py", line 251, in build_wheel
              return _build_backend().build_wheel(wheel_directory, config_settings,
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
            File "C:\Users\user\AppData\Local\Temp\pip-build-env-pbo1oi67\overlay\Lib\site-packages\setuptools\build_meta.py", line 410, in build_wheel        
              return self._build_with_temp_dir(
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^
            File "C:\Users\user\AppData\Local\Temp\pip-build-env-pbo1oi67\overlay\Lib\site-packages\setuptools\build_meta.py", line 395, in _build_with_temp_dir
              self.run_setup()
            File "C:\Users\user\AppData\Local\Temp\pip-build-env-pbo1oi67\overlay\Lib\site-packages\setuptools\build_meta.py", line 311, in run_setup
              exec(code, locals())
            File "<string>", line 1, in <module>
            File "C:\Users\user\AppData\Local\Temp\pip-build-env-pbo1oi67\overlay\Lib\site-packages\setuptools\__init__.py", line 103, in setup
              return distutils.core.setup(**attrs)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
            File "C:\Users\user\AppData\Local\Temp\pip-build-env-pbo1oi67\overlay\Lib\site-packages\setuptools\_distutils\core.py", line 184, in setup
              return run_commands(dist)
                     ^^^^^^^^^^^^^^^^^^
            File "C:\Users\user\AppData\Local\Temp\pip-build-env-pbo1oi67\overlay\Lib\site-packages\setuptools\_distutils\core.py", line 200, in run_commands  
              dist.run_commands()
            File "C:\Users\user\AppData\Local\Temp\pip-build-env-pbo1oi67\overlay\Lib\site-packages\setuptools\_distutils\dist.py", line 969, in run_commands  
              self.run_command(cmd)
            File "C:\Users\user\AppData\Local\Temp\pip-build-env-pbo1oi67\overlay\Lib\site-packages\setuptools\dist.py", line 968, in run_command
              super().run_command(command)
            File "C:\Users\user\AppData\Local\Temp\pip-build-env-pbo1oi67\overlay\Lib\site-packages\setuptools\_distutils\dist.py", line 988, in run_command   
              cmd_obj.run()
            File "C:\Users\user\AppData\Local\Temp\pip-build-env-pbo1oi67\normal\Lib\site-packages\wheel\bdist_wheel.py", line 403, in run
              self.run_command("install")
            File "C:\Users\user\AppData\Local\Temp\pip-build-env-pbo1oi67\overlay\Lib\site-packages\setuptools\_distutils\cmd.py", line 316, in run_command    
              self.distribution.run_command(command)
            File "C:\Users\user\AppData\Local\Temp\pip-build-env-pbo1oi67\overlay\Lib\site-packages\setuptools\dist.py", line 968, in run_command
              super().run_command(command)
            File "C:\Users\user\AppData\Local\Temp\pip-build-env-pbo1oi67\overlay\Lib\site-packages\setuptools\_distutils\dist.py", line 988, in run_command   
              cmd_obj.run()
            File "C:\Users\user\ADO\test\src\awesome\custom_install.py", line 38, in run
              subprocess.check_call(pip_command)
            File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\subprocess.py", line 413, in check_call
              raise CalledProcessError(retcode, cmd)
          subprocess.CalledProcessError: Command '['C:\\Users\\user\\ADO\\test\\env\\Scripts\\python.exe', '-m', 'pip', 'install', "--config-settings='--global-option=build_ext'", "--config-settings='--global-option=-IC:\\Users\\user\\AppData\\Local\\Programs\\Graphviz/include'", "--config-settings='--global-option=-LC:\\Users\\user\\AppData\\Local\\Programs\\Graphviz/lib'", 'pygraphviz']' returned non-zero exit status 1.
          [end of output]
    
      note: This error originates from a subprocess, and is likely not a problem with pip.
      ERROR: Failed building wheel for awesome
    Failed to build awesome
    ERROR: Could not build wheels for awesome, which is required to install pyproject.toml-based projects
    

查看回溯我发现了这一点:

C:\Users\user\ADO\test\env\Scripts\python.exe: No module named pip

不确定这怎么可能,因为我相信

pip
是虚拟环境带来的。 但可以肯定的是,我在 :method:
InstallPygraphviz.run
:

的顶部运行以下命令
def run(self):
    python_exe = sys.executable
    subprocess.check_call([python_exe, "-m", "ensurepip"])
    ...

这通过了,如错误之前的回溯中所述:

...
running install
Looking in links: c:\Users\user\AppData\Local\Temp\tmpyagl6bqz
Processing c:\users\user\appdata\local\temp\tmpyagl6bqz\pip-24.0-py3-none-any.whl
Installing collected packages: pip
Successfully installed pip-24.0
C:\Users\user\ADO\test\env\Scripts\python.exe: No module named pip
...

我不知道该怎么办。在解释器中运行 :method:

InstallPygraphviz.run
中的代码效果很好,就像在 git-bash 中运行
pip_command
中的命令一样。

我如何告诉

subprocess
环境中存在
pip
并且它应该使用它?

python pip subprocess setuptools python-venv
1个回答
0
投票

我能够像使用您的代码一样重新创建问题。

(env) PS C:\Users\bkcmk5\Desktop\Projects_for_portfolio\python_test> pip install .   

处理 c:\users kcmk5\desktop\projects_for_portfolio\python_test 安装构建依赖项...完成 获取建造轮子的要求...完成 准备元数据 (pyproject.toml) ...完成 为收集到的包裹建造轮子:太棒了 构建很棒的轮子(pyproject.toml)...错误 错误:子进程退出并出现错误

× Awesome 的构建轮 (pyproject.toml) 未成功运行。 │ 退出代码:1 ╰─>【63行输出】 运行 bdist_wheel 运行构建 运行build_py 复制 src wesom

© www.soinside.com 2019 - 2024. All rights reserved.