I would start by listing the Python packages managed by apt-get
(the following one-liner takes a few seconds to run, be patient):
$ for pyfile in `ls /usr/lib/python2.7/dist-packages`; do dpkg -S "/usr/lib/python2.7/dist-packages/${pyfile}" | sed 's/:.*//g'; done | sort -u
Note: if necessary, replace 2.7 with whatever python version you're interested in.
Next, uninstall the packages you installed manually. If you are unsure which ones you installed, you can get the list of all packages installed manually using the following command (see this question):
$ comm -23 <(apt-mark showmanual | sort -u) <(gzip -dc /var/log/installer/initial-status.gz | sed -n 's/^Package: //p' | sort -u)
Then uninstall the Python packages you installed manually, for example:
$ sudo apt-get remove python-tk python-scipy
Next, you want to know which packages you installed using pip:
$ ls -ltd /usr/local/lib/python2.7/dist-packages/*
This command lists the contents of pip's install directory sorted by last modification date, making it easier to find the packages you installed yourself. For example, I installed Google's gcloud
tool which installs a bunch of pip packages that I don't want to uninstall, but looking through the list it's easy to see that they were all installed at the same exact time, so I know which ones I should leave, and which ones I can uninstall. Before you uninstall any pip package, you should take a snapshot of the list of packages installed:
$ sudo pip freeze > $HOME/pip_freeze_snapshot.txt
Then just uninstall the packages you want, for example:
$ sudo pip uninstall py pytest
Finally, don't forget that some pip packages may have been installed in your user directory. Perhaps you're fine with that, but if not, you can list them:
$ ls -ltd $HOME/.local/lib/python2.7/site-packages/*
If you want to get rid of all these packages you can just delete this directory. Otherwise, just uninstall the individual packages, for example:
$ pip uninstall tensorflow
Don't forget to check for other python versions (e.g., 3.5).