update user installed packages with pip
Question: 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?


Answer: 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 pip
    import subprocess
    
    homedir = os.getenv('HOME')
    homepkg = []
    
    for dist in pip.get_installed_distributions():
      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)
    

