how to update\upgrade a package using pip? [closed

2019-01-31 17:22发布

What is the way to update a package using pip? those do not work

pip update
pip upgrade

I know this is a simple question but as it is not so easy to find (pip documentation doesn't pop up and the questions from stack overflow are relevant but are not exactly about that)

标签: python pip
3条回答
ら.Afraid
2楼-- · 2019-01-31 17:33

The way is

sudo pip install [package_name] --upgrade

or in short

sudo pip install [package_name] -U

sudo will ask to enter your root password to confirm the action.

If you do not have a root password (if you are not the admin) you should probably work with virtualenv and then you should drop the sudo:

pip install [package_name] --upgrade
查看更多
聊天终结者
3楼-- · 2019-01-31 17:33

If you only want to upgrade one package, refer to @borgr's answer. I often find it necessary, or at least pleasing, to upgrade all my packages at once. Currently, pip doesn't natively support that action, but with sh scripting it is simple enough. You use pip list, awk (or cut and tail), and command substitution. My normal one-liner is:

for i in $(pip list -o | awk 'NR > 2 {print $1}'); do sudo pip install -U $i; done

This will ask for the root password. If you do not have access to that, the --user option of pip or virtualenv may be something to look into.

查看更多
smile是对你的礼貌
4楼-- · 2019-01-31 17:36
import subprocess as sbp
import pip
pkgs = eval(str(sbp.run("pip3 list -o --format=json", shell=True,
                         stdout=sbp.PIPE).stdout, encoding='utf-8'))
for pkg in pkgs:
    sbp.run("pip3 install --upgrade " + pkg['name'], shell=True)

Save as xx.py
Then run Python3 xx.py
Environment: python3.5+ pip10.0+

查看更多
登录 后发表回答