Upgrade fails and pio is borked

On my system (Ubuntu 16.04.1) any attempt to upgrade Platformio either using pio upgrade or pip install -U platformio fails on attempt to remove requests installed with apt:

$ LANG=C pip install -U platformio
Requirement already up-to-date: platformio in /usr/local/lib/python2.7/dist-packages
Requirement already up-to-date: pyserial<4,>=3 in /usr/local/lib/python2.7/dist-packages (from platformio)
Requirement already up-to-date: semantic-version>=2.5.0 in /usr/local/lib/python2.7/dist-packages (from platformio)
Collecting requests<3,>=2.4.0 (from platformio)
  Using cached requests-2.12.3-py2.py3-none-any.whl
Requirement already up-to-date: click<6,>=5 in ./.local/lib/python2.7/site-packages (from platformio)
Requirement already up-to-date: colorama in ./.local/lib/python2.7/site-packages (from platformio)
Requirement already up-to-date: lockfile<0.13,>=0.9.1 in ./.local/lib/python2.7/site-packages (from platformio)
Collecting bottle<0.13 (from platformio)
  Using cached bottle-0.12.10-py2-none-any.whl
Installing collected packages: requests, bottle
  Found existing installation: requests 2.9.1
    Uninstalling requests-2.9.1:
Exception:
Traceback (most recent call last):
  File "/home/jazg/.local/lib/python2.7/site-packages/pip/basecommand.py", line 215, in main
    status = self.run(options, args)
  File "/home/jazg/.local/lib/python2.7/site-packages/pip/commands/install.py", line 317, in run
    prefix=options.prefix_path,
  File "/home/jazg/.local/lib/python2.7/site-packages/pip/req/req_set.py", line 736, in install
    requirement.uninstall(auto_confirm=True)
  File "/home/jazg/.local/lib/python2.7/site-packages/pip/req/req_install.py", line 742, in uninstall
    paths_to_remove.remove(auto_confirm)
  File "/home/jazg/.local/lib/python2.7/site-packages/pip/req/req_uninstall.py", line 115, in remove
    renames(path, new_path)
  File "/home/jazg/.local/lib/python2.7/site-packages/pip/utils/__init__.py", line 267, in renames
    shutil.move(old, new)
  File "/usr/lib/python2.7/shutil.py", line 303, in move
    os.unlink(src)
OSError: [Errno 13] Permission denied: '/usr/local/lib/python2.7/dist-packages/requests-2.9.1.dist-info/DESCRIPTION.rst'

I can install it in virtualenv but this is quite inconvenient…

  1. Try pip uninstall platformio and pip install platformio without -U.
  2. You can install PlatformIO to user folder without sudo
pip install --user platformio

Then need to add bin folder to PATH

Better but still far from good (after uninstalling everything):

$ pio
bash: /usr/local/bin/pio: No such file or directory
$ platformio
bash: /home/jazg/.local/bin/platformio: No such file or directory

pio is still searched elsewhere, I have both ~/.local/bin and /usr/local/bin in $PATH, in this exact order. After installing PlatformIO in my home dir pio is still searched in /usr/local:

$ whereis pio
pio: /home/jazg/.local/bin/pio
$ pio --version
bash: /usr/local/bin/pio: No such file or directory
$ which pio
/home/jazg/.local/bin/pio

Tried removing ~/.local/bin/pio and making symlink to ~/.local/bin/platformio but no joy, bash still wants to run /usr/local/bin/pio. I guess I have something broken in my shell but no idea what exactly.

EDIT:
removed /usr/local/bin from $PATH exported in ~/.profile and suddenly everything started working again. :confounded: