Python 如何使pip“试运行”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29531094/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:42:03  来源:igfitidea点击:

How to make pip "dry-run"?

pythonpip

提问by user2546460

For developing a script that runs pip installit would be usefull to have a --dry-runfunction.

为了开发一个运行脚本,pip install拥有一个--dry-run函数会很有用。

I came across the --no-installoption. But this one is deprecated and on call references this. There are hints to unpack a package only, but I can't find a unpackoption in the pip documentation.

我遇到了这个--no-install选项。但是这个已被弃用,并且在通话中引用了this。有仅解压缩包的提示,但我unpack在 pip 文档中找不到选项。

回答by Angelos

It appears you are right, it has been deprecated (ref).

看来你是对的,它已被弃用(ref)。

If by trial run you mean testing it out before actually installing a package in a certain place, presumably before a system wide install, then you can simply run it sandboxed using a virtual environment and then simply discard the environment.

如果通过试运行,您的意思是在某个地方实际安装软件包之前对其进行测试,大概是在系统范围的安装之前,那么您可以简单地使用虚拟环境在沙盒中运行它,然后简单地丢弃该环境。

virtualenv /tmp/venv; /tmp/venv/bin/pip install flask; rm -rf /tmp/venv 

Not as succinct as using a dry-run argument to pip, but it does the job. Also if you want to do a dry run of a series of package installations, omit the deletion at the end.

不像对 pip 使用空运行参数那么简洁,但它可以完成工作。此外,如果您想对一系列软件包安装进行试运行,请省略最后的删除。

In a script you can distil it into a procedure:

在脚本中,您可以将其提炼成一个过程:

#!/bin/bash

TMP_DIR='/tmp/venv'

function dry_run (){
    if [ ! -d "$TMP_DIR" ]; then
            virtualenv /tmp/venv
    fi
    /tmp/venv/bin/pip install 
}

dry_run flask
dry_run uwsgi
rm -rf $TMP_DIR

If you want to do a dry run that tests that the new install(s) play well with system wide deployed, then use virtualenv's system-site-packages option.

如果您想进行试运行以测试新安装是否在系统范围内部署良好,请使用 virtualenv 的 system-site-packages 选项。

virtualenv --system-site-packages /tmp/venv; /tmp/venv/bin/pip install flask; rm -rf /tmp/venv

回答by nealmcb

Yes - pip should have a dry-runoption, to indicate what would happen in a complex situation. It is dangerous when running pip installdowngrades packages without asking you. We need some way to ask what would happen if we run pip install -r requirements.txtwithout laboriously searching thru all the requirements and comparing them to the currently installed ones.

是的 - pip 应该有一个dry-run选项,以指示在复杂情况下会发生什么。在pip install不询问您的情况下运行降级软件包是很危险的。我们需要一些方法来询问如果我们在pip install -r requirements.txt不费力地搜索所有需求并将它们与当前安装的需求进行比较的情况下运行会发生什么。

It looks like setup.py used to have a dry-run. Folks are asking for it elsewhere.

看起来 setup.py 曾经有一个dry-run. 人们在别处要求它。

Some progress in that direction can be found here:

可以在此处找到该方向的一些进展:

回答by kizzx2

With pip version 9 there's a new option --format freezeleading to an elegant one line solution for the pip install -ruse case:

使用 pip 版本 9,有一个新选项--format freeze可以为pip install -r用例提供优雅的单行解决方案:

pip list --format freeze | diff - requirements.txt

回答by MarcH

[Ugly hack disclaimer] on Linux you can try to install in the system location as a user who does nothave permission to install in the /usr/ directory. The command fails with "Permission denied" but only after logging what is missing and what is not.

【Ugly hack 免责声明】在Linux 下,您可以尝试以没有权限安装在/usr/ 目录中的用户身份安装在系统位置。该命令失败并显示“权限被拒绝”,但仅在记录缺少的内容和未记录的内容之后。

(makes you wonder how hard an actual dry-run option would really be to implement)

(让您想知道实际的试运行选项真正实施起来有多难)

回答by silviot

The pip-synccommand from pip-toolscan report what packages would be installed, but will also output the ones that are installed but not in the requirements file. Its dry runoption is -n

pip-sync从命令pip-tools可以报告软件包将被安装什么,但也会输出已安装但未在规定文件的人。它的试运行选项是-n

$ pip install pip-tools
$ pip-sync -n requirements.txt 
Would uninstall:
  pip-tools
Would install:
  requests

Here's the help from pip-sync:

这是来自以下方面的帮助pip-sync

pip-sync --help
Usage: pip-sync [OPTIONS] [SRC_FILES]...

  Synchronize virtual environment with requirements.txt.

Options:
  --version               Show the version and exit.
  -n, --dry-run           Only show what would happen, don't change anything
  --force                 Proceed even if conflicts are found
  -f, --find-links TEXT   Look for archives in this directory or on this HTML
                          page
  -i, --index-url TEXT    Change index URL (defaults to PyPI)
  --extra-index-url TEXT  Add additional index URL to search
  --trusted-host TEXT     Mark this host as trusted, even though it does not
                          have valid or any HTTPS.
  --no-index              Ignore package index (only looking at --find-links
                          URLs instead)
  -q, --quiet             Give less output
  --user                  Restrict attention to user directory
  --cert TEXT             Path to alternate CA bundle.
  --client-cert TEXT      Path to SSL client certificate, a single file
                          containing the private key and the certificate in
                          PEM format.
  --help                  Show this message and exit.