从存储库中以编程方式安装pip会导致错误“无此文件或目录”

时间:2019-11-22 15:30:12

标签: python python-3.x windows pip

在Win 10上,我将安装到py 3.6 venv。从终端

pip install --upgrade git+git://github.com/larochew/py_daemoniker.git@cfd8a669975c217f6df7063be8cb8fbc08b5f0df#egg=daemoniker

工作正常:

Connected to pydev debugger (build 192.5728.105)
  Running command git clone -q git://github.com/larochew/py_daemoniker.git 'C:\Users\user\AppData\Local\Temp\pip-install-g9do107m\daemoniker'
Collecting daemoniker
  Cloning git://github.com/larochew/py_daemoniker.git (to revision cfd8a669975c217f6df7063be8cb8fbc08b5f0df) to c:\users\user\appdata\local\temp\pip-install-g9do107m\daemoniker
  Running command git checkout -q cfd8a669975c217f6df7063be8cb8fbc08b5f0df
Installing collected packages: daemoniker
    Running setup.py install for daemoniker: started
    Running setup.py install for daemoniker: finished with status 'done'
Successfully installed daemoniker-0.2.3

但是从代码来看:

try:
    from pip import main as pipmain
except:
    from pip._internal import main as pipmain

pipmain.main(['install', '--upgrade', 'git+git://github.com/larochew/py_daemoniker.git@cfd8a669975c217f6df7063be8cb8fbc08b5f0df#egg=daemoniker'])

结果:

Collecting daemoniker
  Cloning git://github.com/larochew/py_daemoniker.git (to revision cfd8a669975c217f6df7063be8cb8fbc08b5f0df) to c:\users\user\appdata\local\temp\pip-install-64iunozy\daemoniker
  Running command git clone -q git://github.com/larochew/py_daemoniker.git 'C:\Users\user\AppData\Local\Temp\pip-install-64iunozy\daemoniker'
  Running command git checkout -q cfd8a669975c217f6df7063be8cb8fbc08b5f0df
ERROR: Could not install packages due to an EnvironmentError: [Errno 2] No such file or directory: 'C:\\Users\\user\\AppData\\Local\\Temp\\pip-req-tracker-qu0hrtol\\9eeeef850bab53900fe60a86b33217ba700f6f3907aadc10a37ed11f'

尽管正常安装的软件包没有错误。

1 个答案:

答案 0 :(得分:1)

由于我需要从Windows下的代码中静默安装需求,因此以下代码起到了作用:

        subprocess.check_call(
            [executable, '-m', 'pip', 'install', '--upgrade', '-r', config['requirements_file']],
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE
        )

没有stdout=subprocess.PIPE的pip stdout将进入当前进程。同样,这不会在win中创建终端。 Thnx将领导@sinoroc评论