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
.
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
This simple line is a starting point. You can easily built a bash command to reuse the PACKAGE in the line.
Thanks to @devsnd for the simple bash function example:
To use it, just run:
How about make a shell function to do this ? Add below code to your
~/.profile
or~/.bashrc
then run
source ~/.profile
orsource ~/.bashrc
to import it to your current terminalwhen you want to install && save a package, just run, for example
pips requests
. after package was installed, its version will be save intorequirements.txt
in your current directory.you can manually save it in a Makefile (or a text file and then imported in your Makefile):
and then just run
make install
I've created python package that wraps around the actual
pip
called pipm. Allpip
commands will work as it is, plus they will be reflected in the requirements file. Unlikepip-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
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 yourrequirements.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 ofrequests
and many more) has released some more info about a better pip workflow to better handlepip
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)pipenv
creates and manages it's own virtual environments so in a project with an existingrequirements.txt
, installing all requirements (I use Python3.7 but you can remove the--three
if you do not) is as simple as:Activating the virtualenv to run commands is also easy
Installing requirements will automatically update the
Pipfile
andPipfile.lock
It's also possible to update out-of-date packages
I highly recommend checking it out especially if coming from a
npm
background as it has a similar feel topackage.json
andpackage-lock.json