Question: I'm trying to fix up one of my virtualenvs - I'd like to reset all of the installed libraries back to the ones that match production.

Is there a quick and easy way to do this with pip?

Answer: 
I use the --user option to uninstall all the packages installed in the user site.

```
pip3 freeze --user | xargs pip3 uninstall -y

```
Pip has no way of knowing what packages were installed by it and what packages were installed by your system's package manager. For this you would need to do something like this

for rpm-based distros (replace python2.7 with your python version you installed pip with):

```
find /usr/lib/python2.7/ |while read f; do
  if ! rpm -qf "$f" &> /dev/null; then
    echo "$f"
  fi
done |xargs rm -fr

```

for a deb-based distribution:

```
find /usr/lib/python2.7/ |while read f; do
  if ! dpkg-query -S "$f" &> /dev/null; then
    echo "$f"
  fi
done |xargs rm -fr

```

then to clean up empty directories left over:

```
find /usr/lib/python2.7 -type d -empty |xargs rm -fr

```

I found the top answer very misleading since it will remove all (most?) python packages from your distribution and probably leave you with a broken system.