What is pip's equivalent of `npm install package --save-dev`?

Pythonnode.jsPip

Python Problem Overview


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.

Python Solutions


Solution 1 - Python

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

Solution 2 - Python

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

Solution 3 - Python

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 (inactive for sometime), a similar tool I found and wasn't able to use, it can handle many files and environments(test, dev, production, etc. ). It also has a 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

Solution 4 - Python

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.

Solution 5 - Python

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

Solution 6 - Python

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

Solution 7 - Python

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.

Solution 8 - Python

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.

Solution 9 - Python

What about this one:

pip freeze >> requirements.txt

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionhllauView Question on Stackoverflow
Solution 1 - PythonEwanView Answer on Stackoverflow
Solution 2 - PythonKarim N GorjuxView Answer on Stackoverflow
Solution 3 - PythonNoortheen RajaView Answer on Stackoverflow
Solution 4 - PythondskrvkView Answer on Stackoverflow
Solution 5 - PythonEvan LévesqueView Answer on Stackoverflow
Solution 6 - PythonAnthony PierottiView Answer on Stackoverflow
Solution 7 - PythonabhiomkarView Answer on Stackoverflow
Solution 8 - PythonAnyany PanView Answer on Stackoverflow
Solution 9 - PythonAbbas JafariView Answer on Stackoverflow