How to make pip “dry-run”?

2019-03-22 12:36发布

For developing a script that runs pip install it would be usefull to have a --dry-run function.

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

标签: python pip
4条回答
男人必须洒脱
2楼-- · 2019-03-22 13:09

With pip version 9 there's a new option --format freeze leading to an elegant one line solution for the pip install -r use case:

pip list --format freeze | diff - requirements.txt
查看更多
看我几分像从前
3楼-- · 2019-03-22 13:23

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

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

Some progress in that direction can be found here:

查看更多
倾城 Initia
4楼-- · 2019-03-22 13:24

It appears you are right, it has been deprecated (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.

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 $1
}

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 /tmp/venv; /tmp/venv/bin/pip install flask; rm -rf /tmp/venv
查看更多
我想做一个坏孩纸
5楼-- · 2019-03-22 13:34

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

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

查看更多
登录 后发表回答