update a virtualenv to match requirements.txt

  • Last Update :
  • Techknowledgy :

I wonder, why simple

$ pip install - r requirements.txt--upgrade

Regarding removing unwanted packages - is that really necessary? If you really insist on it, I would create unwanted-requirements.txt and before updating packages by previous call I would perform:

$ pip uninstall - r unwanted - requirements.txt

If you also need to uninstall packages that are not in requirements.txt, you can use something like that:

pip freeze | grep - v - f requirements.txt - | xargs pip uninstall - y

And only after that:

pip install - r requirements.txt

Suggestion : 2

venv-update is an MIT-Licensed tool to quickly and exactly synchronize a large python project’s virtualenv with its requirements.,A small script designed to keep a virtualenv in sync with a changing list of requirements. The contract of venv-update is this:,This project ships as two separable components: pip-faster and venv-update.,By default, running venv-update will create a virtualenv named venv in the current directory, using requirements.txt in the current directory. This should be the desired default for most projects.

Instead, the script is designed to be vendored (directly checked in) to your project. It has no dependencies other than virtualenv and the standard Python library.

curl - o venv - update https: //raw.githubusercontent.com/Yelp/venv-update/v3.1.1/venv_update.py
   chmod + x venv - update
$ venv - update--help
usage: venv - update[-hV][options]

Update a(possibly non - existent) virtualenv directory using a pip requirements
file.When this script completes, the virtualenv directory should contain the
same packages as
if it were deleted then rebuilt.

venv - update uses "trailing equal"
options(e.g.venv = ) to delimit groups of
   (conventional, dashed) options to pass to wrapped commands(virtualenv and pip).

   venv = parameters are passed to virtualenv
default: venv
install = options to pip - command
default: -r requirements.txt
pip - command = is run after the virtualenv directory is bootstrapped
default: pip - faster install--upgrade--prune
bootstrap - deps = dependencies to install before pip - command = is run
default: venv - update == 3.1 .1

   # install requirements.txt to "venv"
venv - update

# install requirements.txt to "myenv"
venv - update venv = myenv

# install requirements.txt to "myenv"
using Python 3.4
venv - update venv = -ppython3 .4 myenv

# install myreqs.txt to "venv"
venv - update install = -r myreqs.txt

# install requirements.txt to "venv", verbosely
venv - update venv = venv - vvv install = -r requirements.txt - vvv

# install requirements.txt to "venv", without pip - faster--update--prune
venv - update pip - command = pip install

We strongly recommend that you keep the
default value of pip - command = in order
to quickly and reproducibly install your requirements.You can override the
packages installed during bootstrapping, prior to pip - command = , by setting
bootstrap - deps =

   Pip options are also controllable via environment variables.
See https: //pip.readthedocs.org/en/stable/user_guide/#environment-variables
   For example:
   PIP_INDEX_URL = https: //pypi.example.com/simple venv-update

   Please send issues to: https: //github.com/yelp/venv-update
venv: requirements.txt
   . / bin / venv - update

   .PHONY: run - some - script
run - some - script: venv
   . / venv / bin / some - script
  envlist = py27, py34 +
     skipsdist = true

  [testenv] +
  venv_update = +{
  /bin/venv - update\
     venv = {
     }\ +
     install = -r {
  /requirements.txt {toxinidir} -
  deps = -rrequirements.txt
  commands = +{
     [testenv] venv_update
  py.test tests /
     pre - commit run--all - files

Suggestion : 3

This guide discusses how to install packages using pip and a virtual environment manager: either venv for Python 3 or virtualenv for Python 2. These are the lowest-level tools for managing Python packages and are recommended if higher-level tools do not suit your needs.,Installing packages using pip and virtual environments,This is useful for creating Requirements Files that can re-create the exact versions of all packages installed in an environment.,As long as your virtual environment is activated pip will install packages into that specific environment and you’ll be able to import and use packages in your Python application.

python3 - m pip install--user--upgrade pip

python3 - m pip--version
pip 21.1 .3 from $HOME / .local / lib / python3 .9 / site - packages(python 3.9)
py - m pip install--upgrade pip

py - m pip--version
pip 21.1 .3 from c: \python39\ lib\ site - packages(Python 3.9 .4)
python3 - m pip install--user virtualenv
py - m pip install--user virtualenv