使用 pip 命令从 requirements.txt 升级 python 包

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24764549/
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 05:09:14  来源:igfitidea点击:

Upgrade python packages from requirements.txt using pip command

pythonpippackagesrequirements.txt

提问by abhiomkar

How do I upgrade all my python packages from requirements.txt file using pip command?

如何使用 pip 命令从 requirements.txt 文件升级所有 python 包?

tried with below command

尝试使用以下命令

$ pip install --upgrade -r requirements.txt

Since, the python packages are suffixed with the version number (Django==1.5.1) they don't seem to upgrade. Is there any better approach than manually editing requirements.txt file?

因为,python 包以版本号 ( Django==1.5.1)为后缀,它们似乎没有升级。有没有比手动编辑 requirements.txt 文件更好的方法?

EDIT

编辑

As Andy mentioned in his answer packages are pinned to a specific version, hence it is not possible to upgrade packages through pip command.

正如安迪在他的回答中提到的那样,软件包被固定到特定版本,因此无法通过 pip 命令升级软件包。

But, we can achieve this with pip-toolsusing the following command.

但是,我们可以pip-tools使用以下命令来实现这一点。

$ pip-review --auto

this will automatically upgrade all packages from requirements.txt (make sure to install pip-toolsusing pip install command).

这将自动从 requirements.txt 升级所有包(确保pip-tools使用 pip install 命令安装)。

采纳答案by Andy

No. Your requirements file has been pinnedto specific versions. If your requirements are set to that version, you should not be trying to upgrade beyond those versions. If you needto upgrade, then you need to switch to unpinned versions in your requirements file.

否。您的需求文件已固定到特定版本。如果您的要求设置为该版本,则不应尝试升级到这些版本之外。如果需要升级,则需要在需求文件中切换到未固定版本。

Example:

例子:

lxml>=2.2.0

This would upgrade lxml to any version newer than 2.2.0

这会将 lxml 升级到任何比 2.2.0 新的版本

lxml>=2.2.0,<2.3.0

This would upgrade lxml to the most recent version between 2.2.0 and 2.3.0.

这会将 lxml 升级到 2.2.0 和 2.3.0 之间的最新版本。

回答by Freelancer

you can try:

你可以试试:

pip install --upgrade --force-reinstall -r requirements.txt

You can also ignore installed package and install the new one :

您还可以忽略已安装的软件包并安装新软件包:

pip install --ignore-installed -r requirements.txt

回答by Montaro

Since I couldn't do that using bash, I wrote a python module to create a new requirements file with no versions and use it:

由于我无法使用 bash 做到这一点,因此我编写了一个 python 模块来创建一个没有版本的新需求文件并使用它:

data = open('requirements-prod.pip', 'r')
data2 = open('requirements-prod-no-version.pip', 'w')
for line in data.readlines():
    new_line = line[:line.index('==')]
    data2.write(new_line + '\n')
data2.flush()

Then install the libs from the new file pip install -U -r requirements-prod-no-version.pip

然后从新文件安装库 pip install -U -r requirements-prod-no-version.pip

Finally freeze the versions to the original file pip freeze > requirements-prod.pip

最后将版本冻结到原始文件 pip freeze > requirements-prod.pip

回答by Simion Agavriloaei

I already answered this question here. Here's my solution:

我已经在这里回答了这个问题。这是我的解决方案:

Because there was no easy way for upgrading package by package, and updating the requirements.txt file, I wrote this pip-upgraderwhich also updates the versions in your requirements.txtfilefor the packages chosen (or all packages).

因为没有简单的方法可以逐个包升级和更新 requirements.txt 文件,所以我编写了这个pip-upgrader,它还会更新requirements.txt文件中所选包(或所有包)的版本。

Installation

安装

pip install pip-upgrader

Usage

用法

Activate your virtualenv (important, because it will also install the new versions of upgraded packages in current virtualenv).

激活您的 virtualenv(很重要,因为它还会在当前的 virtualenv 中安装新版本的升级包)。

cdinto your project directory, then run:

cd进入你的项目目录,然后运行:

pip-upgrade

Advanced usage

高级用法

If the requirements are placed in a non-standard location, send them as arguments:

如果需求放置在非标准位置,请将它们作为参数发送:

pip-upgrade path/to/requirements.txt

If you already know what package you want to upgrade, simply send them as arguments:

如果您已经知道要升级的软件包,只需将它们作为参数发送:

pip-upgrade -p django -p celery -p dateutil

If you need to upgrade to pre-release / post-release version, add --prereleaseargument to your command.

如果您需要升级到预发布/发布后版本,--prerelease请在命令中添加参数。

Full disclosure: I wrote this package.

完全披露:我写了这个包。

回答by Santhosh Yedidi

I edit the requirements.txt as below and run $sh ./requirements.txt

我编辑如下 requirements.txt 并运行 $sh ./requirements.txt

pip install -U amqp;
pip install -U appdirs;
pip install -U arrow;
pip install -U Babel;
pip install -U billiard;
pip install -U celery;
pip install -U Django;
pip install -U django-cors-headers;
pip install -U django-crispy-forms;
pip install -U django-filter;
pip install -U django-markdown-deux;
pip install -U django-pagedown;
pip install -U django-timezone-field;
pip install -U djangorestframework;
pip install -U fcm-django;
pip install -U flower;
pip install -U gunicorn;
pip install -U kombu;
pip install -U Markdown;
pip install -U markdown2;
pip install -U packaging;

回答by Gaurav Mandhotra

If you install anything in your django project and after installation you want to update your requirement file this command can update you requirement.txt file pip freeze > requirements.txt

如果你在你的 django 项目中安装了任何东西,并且在安装后你想更新你的需求文件,这个命令可以更新你的requirement.txt 文件 pip freeze > requirements.txt

if your requirement file not exist in you project you can use this command for make new requirement.txt file pip freeze > requirements.txt

如果您的项目中不存在您的需求文件,您可以使用此命令创建新的requirement.txt 文件 pip freeze > requirements.txt

回答by dmdip

Another solution is to use the upgrade-requirementspackage

另一种解决方案是使用upgrade-requirements

pip install upgrade-requirements

And then run :

然后运行:

upgrade-requirements

It will upgrade all the packages that are not at their latest versions, and also create an updated requirements.txt at the end.

它将升级所有不是最新版本的包,并在最后创建一个更新的 requirements.txt。

回答by Austin Atherton

  • 1) To upgrade pip installed files from reqs.txtadd the >=in replacement of ==this will tell pip to install libgreater than or equal to the version you are requesting, here by installing the most to-date version of requested library

    1.a) **My answer for thread ** By adding py -m pip install -rreqs.txtto a daily restart... or something of the nature you can update your installed libs. Summed up by Andy Perfectly

    -My reason For entering this thread was to find information on how to update virtual env base pip (usually 10.0.03 for me??)

  • 1)从升级PIP安装的文件reqs.txt添加> =替代的==这会告诉PIP安装LIB大于或等于版本您正在请求,在这里通过安装最先进的最新版本要求库

    1.a) **我对线程的回答 ** 通过将py -m pip install -rreqs.txt添加到每日重新启动...或某些性质,您可以更新已安装的库。安迪完美总结

    -我进入这个线程的原因是找到有关如何更新虚拟环境基础点的信息(对我来说通常是 10.0.03??)

in-hopes of satisfying an issue of which have I was able to derive one of two solutions

希望能够解决我能够得出两种解决方案之一的问题

A. creation of venv|| B. Installation of Required libs

A. 创建venv|| B. 所需库的安装

Thanks to Andy I have satisfied need B

感谢安迪,我满足了需求 B

By adding pip >=requested versionin reqs.txt

通过在reqs.txt 中添加 pip >=请求的版本

upon instantiation of new virtual-Environment || re-instantiation of previous Venv

在新虚拟环境实例化时 || 重新实例化之前的 Venv

  1. py -m venv devenv
  1. py -m venv devenv

to setup new dev env

设置新的开发环境

  1. devenv\scripts\activate.bat
  1. devenv\scripts\activate.bat

to activate dev env

激活开发环境

  1. python -m pip install -r requirenments.txt
  1. python -m pip install -r requirenments.txt

to install base libs

安装基础库

yeilds output

产量输出

Collecting pip >=20.0.2 (from -r requirenments.txt (line 1)) Using cached >https://files.pythonhosted.org/packages/54/0c/d01aa759fdc501a58f431eb594a17495f15b88da142ce14b5845662c13f3/pip-20.0.2-py2.py3-none-any.whl

Found existing installation: pip 10.0.1

Uninstalling pip-10.0.1:

 Successfully uninstalled pip-10.0.1
 Successfully installed pip-20.0.2

收集 pip >=20.0.2(来自 -r requirednments.txt(第 1 行))使用缓存 > https://files.pythonhosted.org/packages/54/0c/d01aa759fdc501a58f431eb594a17495f15b88da142ce56320252182ce5632032031312013121111111111111111111111111111111111111111111111111110000000无任何.whl

找到现有安装:pip 10.0.1

卸载 pip-10.0.1:

 Successfully uninstalled pip-10.0.1
 Successfully installed pip-20.0.2

Sorry for the Brain Dump, Hopes this helps someone :)

抱歉脑转储,希望这有助于某人:)

Austin ?

奥斯汀?

回答by MatthewJohn

I've just had to do the same... used this small one-liner to do the job:

我只需要做同样的事情......使用这个小的单线来完成这项工作:

packages=$(cat requirements.txt | sed 's/==.*//g'); echo $packages | xargs pip3 install -U; freeze=$(pip3 freeze); for p in $(echo $packages); do echo $freeze | grep -E "^${p}==" >> requirements.new; done

which:

哪一个:

  • packages=$(cat requirements.txt | sed 's/==.*//g')creates a list of the current packages names in requirements.txt (removing the version).
  • echo $packages | xargs pip3 install -Uthen passes all of the packages as arguments to pip3 to upgrade.
  • freeze=$(pip3 freeze);Gets all of the current package versions in the format required for requirements.txt
  • for p in $(echo $packages)then iterates through the package names
    • echo $freeze | grep -E "^${p}==" >> requirements.newgets the package version line from the pip freeze output which matches the package and writes to new requirements.txt
  • packages=$(cat requirements.txt | sed 's/==.*//g')在 requirements.txt 中创建当前包名称的列表(删除版本)。
  • echo $packages | xargs pip3 install -U然后将所有包作为参数传递给 pip3 进行升级。
  • freeze=$(pip3 freeze);以requirements.txt 所需的格式获取所有当前包版本
  • for p in $(echo $packages)然后遍历包名
    • echo $freeze | grep -E "^${p}==" >> requirements.new从 pip freeze 输出中获取与包匹配的包版本行并写入新的 requirements.txt

This has the added benefit of preserving the ordering of the original requirements.txt. :)

这具有保留原始requirements.txt 的顺序的额外好处。:)

Hope this helps!

希望这可以帮助!

回答by Hermes

I suggest freezingall of your dependencies in order to have predictable builds.

我建议冻结您的所有依赖项,以便进行可预测的构建

When doing that, you can update all dependenciesat once like this:

这样做时,您可以像这样一次更新所有依赖项

sed -i '' 's/==/>=/g' requirements.txt
pip install -U -r requirements.txt
pip freeze > requirements.txt

Having done the above, testyour project with the new set of packages and eventually committhe requirements.txtfile to the repository.

完成上述操作后,使用新的软件包集测试您的项目,并最终将文件提交requirements.txt到存储库。