Python pip 与 `npm install package --save-dev` 的等价物是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19135867/
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
What is pip's equivalent of `npm install package --save-dev`?
提问by hllau
In nodejs, I can do npm install package --save-dev
to save the installed package into the package.
在 nodejs 中,我可以npm install package --save-dev
将安装的包保存到包中。
How do I achieve the same thing in Python package manager pip
? I would like to save the package name and its version into, say, requirements.pip
just after installing the package using something like pip install package --save-dev requirements.pip
.
如何在 Python 包管理器中实现相同的功能pip
?我想requirements.pip
在使用类似pip install package --save-dev requirements.pip
.
回答by Ewan
There isn't an equivalent with pip
.
没有与pip
.
Best way is to pip install package && pip freeze > requirements.txt
最好的方法是 pip install package && pip freeze > requirements.txt
You can see all the available options on their documentation page.
您可以在他们的文档页面上看到所有可用的选项。
If it really bothers you, it wouldn't be too difficult to write a custom bash script (pips
) that takes a -s
argument and freezes to your requirements.txt
file automatically.
如果它真的困扰您,编写一个自定义 bash 脚本 ( pips
)并不会太困难,该脚本接受一个-s
参数并requirements.txt
自动冻结到您的文件。
Edit 1
编辑 1
Since writing this there has been no change in providing an auto --save-dev
option similar to NPM however Kenneth Reitz (author of requests
and many more) has released some more info about a better pip workflowto better handle pip
updates.
自从写这篇文章以来,提供--save-dev
类似于 NPM的自动选项没有任何变化,但是 Kenneth Reitz(requests
以及更多作者)发布了更多关于更好的 pip 工作流程以更好地处理pip
更新的信息。
Edit 2
编辑 2
Linked from the "better pip workflow" article above it is now recommended to use pipenv
to manage requirements and virtual environments. Having used this a lot recently I would like to summarise how simple the transition is:
从上面的“更好的 pip 工作流”文章链接,现在建议使用它pipenv
来管理需求和虚拟环境。最近使用了很多,我想总结一下过渡是多么简单:
Install pipenv
(on Mac)
安装pipenv
(在 Mac 上)
brew install pipenv
pipenv
creates and manages it's own virtual environments so in a project with an existing requirements.txt
, installing all requirements (I use Python3.7 but you can remove the --three
if you do not) is as simple as:
pipenv
创建并与现有的管理它自己的虚拟环境所以在一个项目requirements.txt
,安装的所有要求(我用Python3.7,但你可以删除--three
,如果你不这样做)很简单,只要:
pipenv --three install
Activating the virtualenv to run commands is also easy
激活 virtualenv 来运行命令也很容易
pipenv shell
Installing requirements will automatically update the Pipfile
and Pipfile.lock
安装需求将自动更新Pipfile
和Pipfile.lock
pipenv install <package>
It's also possible to update out-of-date packages
也可以更新过时的包
pipenv update
I highly recommend checking it outespecially if coming from a npm
background as it has a similar feel to package.json
and package-lock.json
我强烈建议检查一下,特别是如果来自npm
背景,因为它与package.json
和package-lock.json
回答by abhiomkar
I made a quick hack on pip
to add --save
option to install/uninstall commands.
我快速pip
添加--save
了安装/卸载命令的选项。
Please have a look at my blog for more information about this hack: http://blog.abhiomkar.in/2015/11/12/pip-save-npm-like-behaviour-to-pip/
请查看我的博客以获取有关此 hack 的更多信息:http: //blog.abhiomkar.in/2015/11/12/pip-save-npm-like-behaviour-to-pip/
Installation (GitHub): https://github.com/abhiomkar/pip-save
安装(GitHub):https: //github.com/abhiomkar/pip-save
Hope this helps.
希望这可以帮助。
回答by Evan Lévesque
you can manuallysave it in a Makefile (or a text file and then imported in your Makefile):
您可以手动将其保存在 Makefile(或文本文件,然后导入您的 Makefile)中:
PYTHON=.venv/bin/python # path to pyphon
PIP=.venv/bin/pip # path to pip
SOURCE_VENV=. .venv/bin/activate
install:
virtualenv .venv
$(SOURCE_VENV) && $(PIP) install -e PACKAGE
$(SOURCE_VENV) && $(PIP) install -r requirements.txt # other required packages
and then just run make install
然后就跑 make install
回答by Anyany Pan
How about make a shell function to do this ?
Add below code to your ~/.profile
or ~/.bashrc
如何制作一个shell函数来做到这一点?将以下代码添加到您的~/.profile
或~/.bashrc
pips() {
local pkg=
if [ -z "" ]; then
echo "usage: pips <pkg name>"
return 1
fi
local _ins="pip install $pkg"
eval $_ins
pip freeze | grep $pkg -i >> requirements.txt
}
then run source ~/.profile
or source ~/.bashrc
to import it to your current terminal
然后运行source ~/.profile
或source ~/.bashrc
将其导入当前终端
when you want to install && save a package, just run, for example pips requests
.
after package was installed, its version will be save into requirements.txt
in your current directory.
当你想安装 && 保存一个包时,只需运行,例如pips requests
. 安装包后,它的版本将保存到requirements.txt
您的当前目录中。
回答by Noortheen Raja
I've created python package that wraps around the actual pip
called pipm. All pip
commands will work as it is, plus they will be reflected in the requirements file. Unlike pip-save
(similar tool I found and wasn't able to use) it can handle many files and environments(test, dev, production, etc. ). It also has command to upgrade all/any of your dependencies.
我创建了围绕实际pip
调用的python 包pipm。所有pip
命令都将按原样工作,而且它们将反映在需求文件中。与pip-save
(我发现但无法使用的类似工具)不同,它可以处理许多文件和环境(测试、开发、生产等)。它还具有升级所有/任何依赖项的命令。
installation
安装
pipm install pkg-name
pipm install pkg-name
installation as development dependency
安装为开发依赖
pipm install pkg-name --dev
pipm install pkg-name --dev
installation as testing dependency
安装作为测试依赖
pipm install pkg-name --test
pipm install pkg-name --test
removal
移动
pipm uninstall pkg-name
pipm uninstall pkg-name
update all your dependencies
更新所有依赖项
pipm update
pipm update
install all your dependencies from the requirements file
从需求文件安装所有依赖项
pipm install
pipm install
including development dependencies
包括开发依赖
pipm install --dev
pipm install --dev
回答by Karim N Gorjux
This simple line is a starting point. You can easily built a bash command to reuse the PACKAGE in the line.
这条简单的线是一个起点。您可以轻松构建一个 bash 命令来重用行中的 PACKAGE。
pip install PACKAGE && pip freeze | grep PACKAGE >> requirements.txt
Thanks to @devsnd for the simple bash function example:
感谢@devsnd 提供了简单的 bash 函数示例:
function pip-install-save {
pip install && pip freeze | grep >> requirements.txt
}
To use it, just run:
要使用它,只需运行:
pip-install-save some-package
回答by Anthony Pierotti
I am using this small command line to install a package and save its version in requirements.txt
:
pkg=package && pip install $pkg && echo $(pip freeze | grep -i $pkg) >> requirements.txt
我正在使用这个小命令行来安装一个包并将其版本保存在requirements.txt
:
pkg=package && pip install $pkg && echo $(pip freeze | grep -i $pkg) >> requirements.txt
回答by dskrvk
Update:apparently, pipenv is not officially endorsed by Python maintainers, and the previously-linked pageis owned by a different organization. The tool has its pros and cons, but the below solution still achieves the result that the OP is seeking.
更新:显然,pipenv 没有得到 Python 维护者的正式认可,之前链接的页面归另一个组织所有。该工具有利有弊,但以下解决方案仍然达到了 OP 所寻求的结果。
pipenvis a dependency management tool that wraps pip
and, among other things, provides what you're asking:
pipenv是一个依赖管理工具,它包装pip
并提供您所要求的内容:
https://pipenv.kennethreitz.org/en/latest/#example-pipenv-workflow
https://pipenv.kennethreitz.org/en/latest/#example-pipenv-workflow
$ pipenv install <package>
This will create a Pipfile if one doesn't exist. If one does exist, it will automatically be edited with the new package your provided.
$ pipenv install <package>
如果不存在,这将创建一个 Pipfile。如果确实存在,它将自动使用您提供的新包进行编辑。
A Pipfile
is a direct equivalent of package.json
, while Pipfile.lock
corresponds to package-lock.json
.
APipfile
是 的直接等价物package.json
,而Pipfile.lock
对应于package-lock.json
。