问题:使用pip命令从requirements.txt升级python软件包
如何使用pip命令从requirements.txt文件升级所有python软件包?
尝试以下命令
$ pip install --upgrade -r requirements.txt
由于python软件包的后缀是版本号(Django==1.5.1
),因此它们似乎没有升级。有没有比手动编辑requirements.txt文件更好的方法?
编辑
正如Andy在其答案中提到的那样,软件包已固定到特定版本,因此无法通过pip命令升级软件包。
但是,我们可以pip-tools
使用以下命令来实现。
$ pip-review --auto
这将自动从requirements.txt中升级所有软件包(确保pip-tools
使用pip install命令安装)。
How do I upgrade all my python packages from requirements.txt file using pip command?
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?
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.
But, we can achieve this with pip-tools
using the following command.
$ pip-review --auto
this will automatically upgrade all packages from requirements.txt (make sure to install pip-tools
using pip install command).
回答 0
否。您的需求文件已固定到特定版本。如果您的要求设置为该版本,则不应尝试升级到那些版本之外。如果需要升级,则需要在需求文件中切换到未固定的版本。
例:
lxml>=2.2.0
这会将lxml升级到2.2.0以上的任何版本
lxml>=2.2.0,<2.3.0
这会将lxml升级到2.2.0和2.3.0之间的最新版本。
No. Your requirements file has been pinned to specific versions. If your requirements are set to that version, you should not be trying to upgrade beyond those versions. If you need to 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,<2.3.0
This would upgrade lxml to the most recent version between 2.2.0 and 2.3.0.
回答 1
你可以试试:
pip install --upgrade --force-reinstall -r requirements.txt
您也可以忽略已安装的软件包并安装新的软件包:
pip install --ignore-installed -r requirements.txt
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
回答 2
我已经在这里回答了这个问题。这是我的解决方案:
因为没有简便的方法来逐个软件包升级软件包和更新requirements.txt文件,所以我写了这个pip-upgrader,它也requirements.txt
为所选软件包(或所有软件包)更新了文件中的版本。
安装
pip install pip-upgrader
用法
激活您的virtualenv(这很重要,因为它还将在当前virtualenv中安装新版本的升级软件包)。
cd
进入您的项目目录,然后运行:
pip-upgrade
高级用法
如果需求放置在非标准位置,请将其作为参数发送:
pip-upgrade path/to/requirements.txt
如果您已经知道要升级的软件包,只需将它们作为参数发送:
pip-upgrade -p django -p celery -p dateutil
如果您需要升级到发行前/发行后版本,请添加 --prerelease
请在命令中参数。
全面披露:我写了这个包裹。
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-upgrader which also updates the versions in your requirements.txt
file for the packages chosen (or all packages).
Installation
pip install pip-upgrader
Usage
Activate your virtualenv (important, because it will also install the new versions of upgraded packages in current virtualenv).
cd
into your project directory, then run:
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 --prerelease
argument to your command.
Full disclosure: I wrote this package.
回答 3
我建议冻结所有依赖项,以使构建具有可预测性。
这样做时,您可以像这样一次更新所有依赖项:
sed -i '' 's/==/>=/g' requirements.txt
pip install -U -r requirements.txt
pip freeze > requirements.txt
完成上述操作后,请使用新的程序包集测试您的项目,并最终将文件提交requirements.txt
到存储库。
I suggest freezing all of your dependencies in order to have predictable builds.
When doing that, you can update all dependencies at once like this:
sed -i '' 's/==/>=/g' requirements.txt
pip install -U -r requirements.txt
pip freeze > requirements.txt
Having done the above, test your project with the new set of packages and eventually commit the requirements.txt
file to the repository.
回答 4
我只需要做同样的事情…用这个小的一线工作:
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
哪一个:
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 Frozen输出中获取与软件包匹配的软件包版本行,并写入新的requirements.txt
这具有保留原始requirements.txt的顺序的额外好处。:)
希望这可以帮助!
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 -U
then 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.new
gets the package version line from the pip freeze output which matches the package and writes to new requirements.txt
This has the added benefit of preserving the ordering of the original requirements.txt. :)
Hope this helps!
回答 5
由于无法使用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()
然后从新文件安装库 pip install -U -r requirements-prod-no-version.pip
最后将版本冻结到原始文件 pip freeze > requirements-prod.pip
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:
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
Finally freeze the versions to the original file pip freeze > requirements-prod.pip
回答 6
另一个解决方案是使用升级要求包
pip install upgrade-requirements
然后运行:
upgrade-requirements
它将升级所有不是最新版本的软件包,并在最后创建一个更新的requirements.txt。
Another solution is to use the upgrade-requirements package
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.
回答 7
1)要从reqs.txt升级pip安装的文件,请
添加> =代替==,
这将告诉pip安装的lib大于或等于您请求的版本,此处安装的是请求的库的最新版本
1.a)**我对线程的回答**通过将py -m pip install -r
reqs.txt添加到每日重启中…或者类似的性质,您可以更新已安装的库。安迪完美总结
-我进入此线程的原因是查找有关如何更新虚拟环境基本点的信息(通常对我来说是10.0.03 ??)
希望解决一个问题,我能够得出两个解决方案之一
A. venv创建|| B.安装必需的库
多亏了安迪,我满足了需求B
通过在reqs.txt中添加pip > = 请求的版本
在实例化新的虚拟环境后|| 重新说明以前的Venv
py -m venv devenv
设置新的开发环境
- d
evenv\scripts\activate.bat
激活开发环境
python -m pip install -r requirenments.txt
安装基本库
Yield输出
收集pip > = 20.0.2(从-r requirenments.txt(第1行))使用缓存的> https://files.pythonhosted.org/packages/54/0c/d01aa759fdc501a58f431eb594a17495f15b88da142ce14b5845662c13f3/pip-20.0.2-py2.py3-无任何
找到现有的安装:pip 10.0.1
卸载pip-10.0.1:
Successfully uninstalled pip-10.0.1
Successfully installed pip-20.0.2
对不起,我们希望可以帮助某人:)
🤳奥斯汀👨🎤🚀🥊
1) To upgrade pip installed files from reqs.txt
add the >= in replacement of ==
this will tell pip to install lib greater 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 -r
reqs.txt to 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??)
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
Thanks to Andy I have satisfied need B
By adding pip >= requested version in reqs.txt
upon instantiation of new virtual-Environment || re-instantiation of previous Venv
py -m venv devenv
to setup new dev env
- d
evenv\scripts\activate.bat
to activate dev env
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
Sorry for the Brain Dump, Hopes this helps someone :)
🤳 Austin 👨🎤🚀🥊
回答 8
第二个答案是最有用的,但是我想做的是锁定某些程序包,同时使其他程序包处于最新版本(例如youtube-dl)。
一个示例requirements.txt
如下所示(〜表示兼容):
Pillow==6.2.2
requests~=2.22.0
youtube_dl
然后在终端中,使用命令 pip install --upgrade -r requirements.txt
这样可以确保Pillow保持在6.2.2,将请求升级到最新的2.22.x(如果有),如果尚未安装,则将安装最新版本的youtube-dl。
The second answer is the most useful but what I wanted to do is lock some packages while having others at the latest version (e.g. youtube-dl).
An example requirements.txt
would look like this (~ means compatible):
Pillow==6.2.2
requests~=2.22.0
youtube_dl
Then in the terminal, use the command pip install --upgrade -r requirements.txt
This ensures that Pillow will stay at 6.2.2, requests will be upgraded to the latest 2.22.x (if available), and the latest version of youtube-dl will be installed if not already.
回答 9
我猜最简单的解决方案是使用以下命令创建requirements.txt:
pip freeze | sed 's/==/>=/' > requirements.txt
I guess the simplest solution is creating the requirements.txt with:
pip freeze | sed 's/==/>=/' > requirements.txt
回答 10
如果您在django项目中安装了任何内容,并且在安装后想要更新需求文件,则此命令可以更新您required.txt文件pip冻结> requirements.txt
如果您的需求文件不存在于项目中,则可以使用此命令来创建新的需求文件。
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
if your requirement file not exist in you project you can use this command for make new requirement.txt file
pip freeze > requirements.txt
回答 11
我按如下所示编辑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;
I edit the requirements.txt as below and run $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;