简体   繁体   English

Pip freeze for only 项目需求

[英]Pip freeze for only project requirements

When I run pip freeze > requirements.txt it seems to include all installed packages.当我运行pip freeze > requirements.txt时,它似乎包含所有已安装的包。 This appears to be the documented behavior.似乎是记录在案的行为。

I have, however, done something wrong as this now includes things like Django in projects that have no business with Django.但是,我做错了一些事情,因为现在在与 Django 没有业务的项目中包括 Django 之类的东西。

How do I get requirements for just this project?我如何获得这个项目的需求? or in the future how do I install a package with pip to be used for this project.或者将来如何安装 package 和pip以用于此项目。 I think I missed something about a virtualenv .我想我错过了一些关于virtualenv的东西。

pipreqs can save the day for a specific project. pipreqs 可以为特定项目节省时间。 Just只是

pip install pipreqs
#then
pipreqs path/to/project

Github Page Github 页面

我已经尝试了pipreqspigar并发现pigar更好,因为它还能生成关于它在哪里使用的信息,它还有更多的选择。

I use this command我用这个命令

EDIT: Thanks Addisson Klinke for suggestion编辑:感谢 Addisson Klinke 的建议

pip freeze -r requirements.txt | pip freeze -r requirements.txt | grep -B100 "pip freeze" | grep -B100 "pip 冻结" | grep -v "pip freeze" grep -v "pip 冻结"

pip freeze -r requirements.txt | sed '/freeze/,$ d'

When I ran pip freeze -r requirements.txt the output is something like当我运行pip freeze -r requirements.txt ,输出类似于

APScheduler==3.2.0
Eve==0.6.4
Eve-Elastic==0.3.8
## The following requirements were added by pip freeze:
arrow==0.8.0
Cerberus==0.9.2

I have a requirements file like this我有一个这样的需求文件

APScheduler
Eve
Eve-Elastic

So I get this output and sed to remove the dependencies that I don`t want.所以我得到这个输出和sed来删除我不想要的依赖项。

First output this to a file首先将其输出到文件

pip freeze -q -r requirements.txt | sed '/freeze/,$ d' > requirements-froze.txt 

That will output just the libs with version这将只输出带有版本的库

APScheduler==3.2.0
Eve==0.6.4
Eve-Elastic==0.3.8

Then replace requirements file然后替换需求文件

mv requirements-froze.txt requirements.txt 

I still suggest using the official pip freeze > requirements.txt ( documentation ) compared to the two alternative features using pigar and pipreqs mentioned in the other answers because pip freeze lists the effective package names .我仍然建议使用官方pip freeze > requirements.txt文档)与其他答案中提到的使用pigarpipreqs的两个替代功能相比,因为pip freeze列出了有效的 package 名称

Incomplete comparison among the three as per February 2022截至 2022 年 2 月,三者之间的不完全比较

Criteria \ Tool标准\工具 pip freeze > requirements.txt pip 冻结 > requirements.txt pigar猪油 pipreqs pipreqs
Name mismatch (1)姓名不匹配 (1) Package my-package==1.0.0 Package my-package==1.0.0 Module my_package == 1.0.0模块my_package == 1.0.0 Module my_package==1.0.0模块my_package==1.0.0
Module overloading (2)模块重载(二) All packages my-package1==1.0.0 , my-package2==2.0.0所有包my-package1==1.0.0 , my-package2==2.0.0 None没有任何 Top-level module (version shows 0.0.0) my==0.0.0顶层模块(版本显示0.0.0) my==0.0.0
Showing only directly used packages仅显示直接使用的包 No Yes是的 Yes是的
Minimal contents最少的内容 Yes是的 No Yes是的

(1) There can be a mismatch between module and package name such as my-package (package name) vs my_package (module name). (1) 模块和 package 名称之间可能存在不匹配,例如my-package (包名称)与my_package (模块名称)。

(2) There can be several packages using the same top level folder such as my-package1 and my-package2 (package names) which are installed under my/package1 and my/package2 , which are imported by Python's command import my.package1 and import my.package2 . (2) 可以有多个包使用相同的顶级文件夹,例如my-package1my-package2 (包名称),它们安装在my/package1my/package2 ,它们由 Python 的命令import my.package1import my.package2 Note that pipreqs notes version 0.0.0 for the not existing package my .请注意,pipreqs 为不存在的 package my注释版本 0.0.0。

I know these are very particular cases but I hope giving people this overview can help understanding limitations and avoid possible mistakes.我知道这些是非常特殊的情况,但我希望向人们提供这个概述可以帮助理解限制并避免可能的错误。

Here is a simplified version based on the previous comment: https://stackoverflow.com/a/40026169/4052041这是基于之前评论的简化版本: https : //stackoverflow.com/a/40026169/4052041

mv requirements.txt requirements.txt.bak
pip freeze -q -r requirements.txt.bak | awk '/.*pip freeze.*/  {exit} {print}' > requirements.txt

With your virtualenv activated, do python -E -m pip freeze .激活virtualenv后,执行python -E -m pip freeze The -E flag means, according to python --help , ignore PYTHON* environment variables (such as PYTHONPATH) , so pip it's not going to access to global site-packages . -E标志表示,根据python --help忽略 PYTHON* 环境变量(例如 PYTHONPATH) ,因此 pip 它不会访问全局site-packages

如果您使用的是 linux,那么请使用sed

pip freeze | sed 's/==.*$/''/' > requirements.txt

I had the same issue with pip freeze .我对pip freeze有同样的问题。 In my case the problem was that I ran pip freeze without activating my projects virtual environment.在我的情况下,问题是我在没有激活我的项目虚拟环境的情况下运行了pip freeze I activated the virtual environment and pip freeze > requirements.txt worked fine.我激活了虚拟环境并且pip freeze > requirements.txt工作正常。

So do make sure you activate your projects virtual environment by running <virtualenv folder name>\\Scipts\\activate on Windows or source <virtualenv folder name>\\bin\\activate on Linux.因此,请确保通过在 Windows 上运行<virtualenv folder name>\\Scipts\\activate或在 Linux 上运行source <virtualenv folder name>\\bin\\activate来激活您的项目虚拟环境。

If the virtualenv has global access you should run pip freeze with the -l or --local option, pip freeze -l which according to thepip freeze docs如果virtualenv具有全局访问权限,您应该使用-l--local选项运行pip freezepip freeze -l根据pip freeze 文档

-l, --local -l, --local
If in a virtualenv that has global access, do not output globally-installed packages.如果在具有全局访问权限的 virtualenv 中,请不要输出全局安装的包。

I just had the same issue, here's what I've found to solve the problem.我刚刚遇到了同样的问题,这是我找到的解决问题的方法。

First create the venv in the directory of your project, then activate it.首先在你的项目目录下创建venv,然后激活它。

For Linux/MacOS : python3 -m venv ./venv source myvenv/bin/activate对于 Linux/MacOS: python3 -m venv ./venv source myvenv/bin/activate

For Windows : python3 -m venv .\\venv env\\Scripts\\activate.bat对于 Windows: python3 -m venv .\\venv env\\Scripts\\activate.bat

Now pip freeze > requirements.txt should only takes the library used in the project.现在pip freeze > requirements.txt应该只接受项目中使用的库。

NB: If you have already begin your project you will have to reinstall all the library to have them in pip freeze.注意:如果您已经开始了您的项目,则必须重新安装所有库才能将它们冻结在 pip 中。

pip install pipreqs, 
pipreqs>requirements.txt

That works easily这很容易工作

I do came across same situation.我确实遇到过同样的情况。 After activating the virtual enviroment and doing激活虚拟环境并执行后

pip3 freeze > requirements.txt

still its collect all package install on my WSL ubuntu 22.04仍然收集所有 package 安装在我的 WSL ubuntu 22.04

After install "pipreqs" and typing pipreqs>requirments.txt My Wsl got stuck.安装“pipreqs”并输入pipreqs> requirements.txt 后,我的 Wsl 卡住了。

The best solution is pip3 freeze -l > requirments.txt its only collected my local package used for my development.最好的解决方案是pip3 freeze -l > requirments.txt它只收集了我的本地 package 用于我的开发。

There is a package that works for me in windows and Lunix Pipdeptree After the installation you can use Pipdeptree to generate requirements.txt file with just top-level packages.在 windows 和 Lunix Pipdeptree中有一个 package 对我有用。安装后,您可以使用 Pipdeptree 生成仅包含顶级包的 requirements.txt 文件。

Windows: Windows:

pipdeptree -f --warn silence | findstr  /r  "^[a-zA-Z0-9\-]" > requirements.txt

Lunix:卢尼克斯:

pipdeptree -f --warn silence | grep -E '^[a-zA-Z0-9\-]+' > requirements.txt 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM