问题:pip等同于`npm install package –save-dev`吗?

在nodejs中,我可以npm install package --save-dev将已安装的软件包保存到软件包中。

如何在Python包管理器中实现同一目的pip?我想将软件包名称及其版本保存到,例如,requirements.pip使用来安装软件包之后pip install package --save-dev requirements.pip

In nodejs, I can do npm install package --save-dev to save the installed package into the package.

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.


回答 0

没有与的等效项pip

最好的方法是 pip install package && pip freeze > requirements.txt

您可以在其文档页面上看到所有可用的选项。

如果确实让您感到困扰,那么编写一个自定义bash脚本(pips)会很困难,该脚本带有一个-s参数并requirements.txt自动冻结到您的文件中。

编辑1

自编写此书以来,提供--save-dev类似于NPM 的自动选项没有任何变化,但是Kenneth Reitz(requests及更多作者)发布了一些有关更好的点子工作流程以更好地处理pip更新的信息。

编辑2

从上面的“更好的点子工作流程”文章链接到现在,建议将其用于pipenv管理需求和虚拟环境。最近使用了很多,我想总结一下转换是多么简单:

安装pipenv(在Mac上)

brew install pipenv

pipenv创建并管理它自己的虚拟环境,因此在具有现有的项目中requirements.txt,安装所有要求(我使用Python3.7,但--three如果不这样做,则可以删除)就很简单:

pipenv --three install

激活virtualenv以运行命令也很容易

pipenv shell

安装要求将自动更新PipfilePipfile.lock

pipenv install <package>

还可以更新过期的软件包

pipenv update

我强烈建议您进行检查,尤其是来自npm背景时,因为它的感觉package.jsonpackage-lock.json

There isn’t an equivalent with pip.

Best way is to 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.

Edit 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 workflow to better handle pip updates.

Edit 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:

Install pipenv (on 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 --three install

Activating the virtualenv to run commands is also easy

pipenv shell

Installing requirements will automatically update the Pipfile and Pipfile.lock

pipenv install <package>

It’s also possible to update out-of-date packages

pipenv update

I highly recommend checking it out especially if coming from a npm background as it has a similar feel to package.json and package-lock.json


回答 1

这条简单的线是一个起点。您可以轻松构建一个bash命令来重用该行中的PACKAGE。

pip install PACKAGE && pip freeze | grep PACKAGE >> requirements.txt

感谢@devsnd提供了简单的bash函数示例:

function pip-install-save { 
    pip install $1 && pip freeze | grep $1 >> requirements.txt
}

要使用它,只需运行:

pip-install-save some-package

This simple line is a starting point. You can easily built a bash command to reuse the PACKAGE in the line.

pip install PACKAGE && pip freeze | grep PACKAGE >> requirements.txt

Thanks to @devsnd for the simple bash function example:

function pip-install-save { 
    pip install $1 && pip freeze | grep $1 >> requirements.txt
}

To use it, just run:

pip-install-save some-package

回答 2

我创建Python包,各地的实际包装pip称为PIPM。所有pip命令将按原样运行,并且它们将反映在需求文件中。与pip-save(我发现但无法使用的类似工具)不同,它可以处理许多文件和环境(测试,开发,生产等)。它还具有用于升级所有/任何依赖项的命令。

安装

pipm install pkg-name

安装为开发依赖项

pipm install pkg-name --dev

安装为测试依赖项

pipm install pkg-name --test

清除

pipm uninstall pkg-name

更新所有依赖

pipm update

从需求文件安装所有依赖项

pipm install

包括开发依赖

pipm install --dev

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.

installation

pipm install pkg-name

installation as development dependency

pipm install pkg-name --dev

installation as testing dependency

pipm install pkg-name --test

removal

pipm uninstall pkg-name

update all your dependencies

pipm update

install all your dependencies from the requirements file

pipm install

including development dependencies

pipm install --dev


回答 3

更新:显然,pipenv没有得到Python维护者的正式认可,并且以前链接的页面是由另一个组织拥有的。该工具各有利弊,但以下解决方案仍然可以达到OP所寻求的结果。

pipenv是一个依赖项管理工具,它包装pip并提供您所要求的内容:

https://pipenv.kennethreitz.org/en/latest/#example-pipenv-workflow

$ pipenv install <package>

如果不存在,这将创建一个Pipfile。如果存在,它将使用您提供的新软件包自动进行编辑。

A Pipfile直接等于package.json,而Pipfile.lock对应于package-lock.json

Update: apparently, pipenv is not officially endorsed by Python maintainers, and the previously-linked page is 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 is a dependency management tool that wraps pip and, among other things, provides what you’re asking:

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.

A Pipfile is a direct equivalent of package.json, while Pipfile.lock corresponds to package-lock.json.


回答 4

我快速pip添加--save了安装/卸载命令选项。

请查看我的博客以获取有关此黑客的更多信息:http : //blog.abhiomkar.in/2015/11/12/pip-save-npm-like-behaviour-to-pip/

安装(GitHub):https : //github.com/abhiomkar/pip-save

希望这可以帮助。

I made a quick hack on pip to add --save option to install/uninstall commands.

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/

Installation (GitHub): https://github.com/abhiomkar/pip-save

Hope this helps.


回答 5

您可以将其手动保存在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

然后运行 make install

you can manually save it in a Makefile (or a text file and then imported in your 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


回答 6

我正在使用此小命令行来安装软件包并将其版本保存在requirements.txtpkg=package && pip install $pkg && echo $(pip freeze | grep -i $pkg) >> requirements.txt

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


回答 7

使shell函数执行此操作怎么样?将以下代码添加到您的~/.profile~/.bashrc

pips() {
    local pkg=$1

    if [ -z "$1" ]; then
        echo "usage: pips <pkg name>"
        return 1
    fi

    local _ins="pip install $pkg"
    eval $_ins
    pip freeze | grep $pkg -i >> requirements.txt
}

然后运行source ~/.profilesource ~/.bashrc将其导入到当前终端

例如,当您要安装&&保存软件包时,只需运行即可pips requests。安装软件包后,其版本将保存到requirements.txt您的当前目录中。

How about make a shell function to do this ? Add below code to your ~/.profile or ~/.bashrc

pips() {
    local pkg=$1

    if [ -z "$1" ]; 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

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.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。