70

I'm using pip and virtualenv for my python application. I would like to upgrade to a new version of the application without touching the dependencies. When I use pip install -U, it tries to upgrade all the packages, and even uninstalls and re-installs the same version of a dependency package when there isn't a new version available.

I also tried pip install -U --no-deps but that seems equivalent to a regular install instead of an upgrade. Is there a combination of flags that will do what I want?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Amy G
  • 741
  • 1
  • 5
  • 5
  • good question. I cross-linked http://stackoverflow.com/questions/2875232/list-python-package-dependencies-without-loading-them -- if you can't even list dependencies ahead of time, seems you're stuck. – denis May 20 '10 at 16:17

3 Answers3

75

Overview:

  • Install new packages without upgrading installed ones: pip install (without -U)
  • Upgrade only packages that are outdated according to requirements: pip install --upgrade --upgrade-strategy only-if-needed (default in new versions)
  • Upgrade package and all dependencies to latest version: pip install --upgrade --upgrade-strategy eager (default in old versions)
  • Install or upgrade listed packages without touching dependencies: --no-deps

UPDATE (thanks to @Jether's comment): If you're using the latest version of pip, then updating dependencies only when necessary is now the default behavior, and you don't need to do anything special! The answer below outlines the steps for older versions of pip (which also works for newer versions if you want to be portable).

If you really want to not touch dependencies, then indeed the way to go is

pip install -U --no-deps mypackage

But I think what you'll usually want is to not upgrade dependencies unless it's required. In that case you can use:

pip install --upgrade --upgrade-strategy only-if-needed mypackage

This only updates requirements if the package requires a newer version than is installed.

Mark
  • 18,730
  • 7
  • 107
  • 130
  • This is now the new default strategy in pip: https://github.com/pypa/pip/pull/4500 – Jether Oct 22 '17 at 14:30
  • 11
    LoL, I just blindly copied the command from the answer and it indeed installed some [`mypackage`](https://pypi.python.org/pypi/Mypackage/0.1) into my python. – ImportanceOfBeingErnest Feb 02 '18 at 15:35
  • 2
    I believe eager is the default. only-if-needed will be the default in pip 10.0: https://github.com/pypa/pip/pull/4500/files, https://pypi.python.org/pypi/pip. – Jérôme Mar 15 '18 at 10:11
  • 2
    Is there any option for "only if inexistent"? I don't want PIP to touch any of the existing packages, but let it install the missing ones.... – Daniel Möller May 07 '18 at 12:41
  • @DanielMöller Isn't that what `pip install` does without `--upgrade`? – Mark May 07 '18 at 18:57
  • 1
    How to install the latest package as possible that did not require updating dependency? – Muhammad Yasirroni Dec 16 '22 at 03:54
  • What happens if you *also* specify an explicit package version which contradicts the `--upgade` option, who wins? E.g. mypackage 2.0 is installed, and 4.0 is the latest. You run `pip install -U mypackage==1.0` ? What about `pip install -U mypackage==3.0`. I assume the explicit version wins for the package itself, and the -U is redundant? But is the behavior for dependencies any different? – Amit Naidu Feb 14 '23 at 00:28
61

I just tried on my virtualenv project and pip install -U --no-deps mypackage seems to work just fine. It just download mypackage and nothing else. What's your set up like?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Y.H Wong
  • 7,151
  • 3
  • 33
  • 35
  • My confusion came from the difference in behavior when you give pip a specific tarball versus a package name. Carl Meyer had a helpful explanation: http://groups.google.com/group/python-virtualenv/msg/d702c251a6dc40a5 – Amy G Jun 10 '10 at 16:28
  • 3
    I highly recommend running `pip check` afterwards to check if there any issues with missing dependencies of dependencies. – Flimm Feb 14 '17 at 14:19
4

You're right. I thought that when I added --no-deps it had neglected to uninstall the existing version. But I tried it again and see there's no issue:

$ pip install -U --no-deps myproj
Downloading/unpacking myproj
  Downloading myproj-1.0-trunk.31072.tar.gz (43Kb): 43Kb downloaded
  Running setup.py egg_info for package myproj
Installing collected packages: myproj
  Found existing installation: myproj 1.0-trunk.31053
    Uninstalling myproj:
      Successfully uninstalled myproj
  Running setup.py install for myproj
Successfully installed myproj
Cleaning up...
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Amy G
  • 741
  • 1
  • 5
  • 5