Ignoring some requirements when installing pip requirements

Ranvijay Jamwal picture Ranvijay Jamwal · Jan 16, 2017 · Viewed 9.3k times · Source

I am using requirements.txt to install requirements for my virtualenv. I use ansible for deployments which installs requirements on remote hosts.

Problem:

  1. Ignoring some requirements

  2. Ignoring already installed requirements (something like pip freeze and if the package shows up, don't install it and don't even upgrade)

Solutions according to me:

  1. I can grep installed packages and make a requirements2.txt having only the ones needed. (Also, remove packages being installed from GIT)

  2. I don't understand what --ignore-installed would do in this case?

  3. Any other solution?

Answer

Sergey Vasilyev picture Sergey Vasilyev · Oct 18, 2017

For selective dependencies installing, the only way is indeed to grep/filter the requirements.txt file according to your criteria. However, there are few ready solutions that might be of use:


If you have a virtualenv and only need to quickly upgrade it to the new requirements or version restrictions, but do not upgrade if the existing packages meet the criteria, you can use

pip install -U --upgrade-strategy=only-if-needed  ...

As the manual says:

--upgrade-strategy <upgrade_strategy> Determines how dependency upgrading should be handled. "eager" - dependencies are upgraded regardless of whether the currently installed version satisfies the requirements of the upgraded package(s). "only-if-needed" - are upgraded only when they do not satisfy the requirements of the upgraded package(s).


For the optional dependencies, the typical solution is the setuptools' extra requirements. For example, I use it for the development & doc-building requirements:

# setup.py
setup(
    ...,
    extras_require={
        'dev': ["pdbpp", "ipython"],
        'doc': ["sphinx"],
    },
)

Then you can install it as follows, both from the PyPI/DevPI repos, and locally (as an editable library):

pip install mylib[dev]
pip install mylib[doc]
pip install -e .[doc,dev]

You can define any names for the "extra modes" with optional dependencies.