3

I'm using a bunch of python packages for my research that I install in my home directory using the --user option of pip. There are also some packages that were installed by the package manager of my distribution for other things. I would like to have a pip command that only upgrade the packages I installed myself with the --user option.

I tried the recommend version pip freeze --local | grep -v '^\-e' | cut -d = -f 1 | xargs pip install -U but this seems to only work using virtualenvs. pip freeze --local is showing packages that are installed for my user and systemwide.

Is there a way to upgrade only the packages installed locally for my user?

Max Linke
  • 1,705
  • 2
  • 18
  • 24

3 Answers3

4

You can upgrade user-installed packages with this simple command-line one-liner:

pip freeze --user | cut -d'=' -f1 | xargs pip install --user -U
mdeous
  • 17,513
  • 7
  • 56
  • 60
3

I've come across this problem as well and created the following script. The script updates any packages found by pip and which reside within the user's home directory. Usually, this should be the packages that were installed with pip install --user.

#!/usr/bin/env python

# Starting point for this script:
# http://stackoverflow.com/a/5839291

import os
import subprocess
import pkg_resources

HOMEDIR = os.getenv('HOME')
homepkg = []

for dist in list(pkg_resources.working_set):
  if not dist.location.startswith(HOMEDIR):
    continue
  homepkg.append(dist.project_name)

if len(homepkg) == 0:
  print('No locally-installed packages, nothing to update.')
  raise SystemExit

# --no-deps is required because --upgrade by default is recursive and would try
# to update packages that are not from homedir (e.g. from /usr). 

subprocess.call(['pip', 'install', '--user', '--upgrade', '--no-deps'] + homepkg)
Milan
  • 171
  • 2
  • the get_installed_distributions() function no longer exists, moreover pip shouldn't be used as a library, as said here: https://pip.pypa.io/en/latest/user_guide/#using-pip-from-your-program – mdeous Sep 26 '18 at 11:25
0

I would suggest creating virtual environment if it is possible for you.

You would just use sudo apt-get install python-virtualenv to install virtualenv, then enter your folder where you store python projects and type into terminal virtualenv venv. After that, you can activate it like this source venv/bin/activate.

What it does is it creates almost full copy of python (some libraries are just linked to save space) and everything you do after activating only affects that copy, not global environment. Therefore you can install any set of libraries using pip, update them etc. and you won't change anything outside of virtual environment. But don't forget to activate it first before you do anything.

Raven
  • 4,783
  • 8
  • 44
  • 75
  • a virtualenv does not really work for me here because I want to change the global environment for my user. – Max Linke Sep 12 '14 at 14:01
  • Ok this was more of an alternative suggestion, that should be easier to do and work just fine in most cases, but if you really need to update the global environment I don't know of solution. – Raven Sep 12 '14 at 14:59