3

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?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Ranvijay Jamwal
  • 661
  • 1
  • 6
  • 17
  • 1
    `pip install` will already ignore anything that's already installed. What do you mean *"ignoring some requirements"*, then how will the code work? – jonrsharpe Jan 16 '17 at 14:48
  • @jonrsharpe Not necessarily true for this task; but often some of the requirements are optional and make installation more troublesome. I often remove some solver (ecos) from cvxpy on windows when i know that i don't need this one. But that requires some understanding of the lib though. – sascha Jan 16 '17 at 14:49
  • Have you looked into providing a `setup.py` so you can make it clear which are requirements and which are extras? – jonrsharpe Jan 16 '17 at 14:50
  • @jonrsharpe I mean that I want to waste less time installing and checking requirements. It just goes, fetches the newly added requirement and then installs it. No I haven't tried the setup.py thingy. Will that help in this case? – Ranvijay Jamwal Jan 16 '17 at 15:28

1 Answers1

2

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.

Sergey Vasilyev
  • 3,919
  • 3
  • 26
  • 37