- echo "pip freeze 2> /dev/null | xargs pip uninstall -y > /dev/null 2> /dev/null" > prerm
- # Cleanup __pycache__ folders
- echo "rm -rf $TERMUX_PREFIX/lib/python${_MAJOR_VERSION}/" >> prerm
+ # Avoid running on update:
+ echo 'if [ $1 != "remove" ]; then exit 0; fi' > prerm
+ # Uninstall everything installed through pip:
+ echo "pip freeze 2> /dev/null | xargs pip uninstall -y > /dev/null 2> /dev/null" >> prerm
+ # Cleanup __pycache__ folders:
+ echo "find $TERMUX_PREFIX/lib/python${_MAJOR_VERSION} -depth -name __pycache__ -exec rm -rf {} \;" >> prerm
+ # Remove contents of site-packages/ folder:
+ echo "rm -Rf $TERMUX_PREFIX/lib/python${_MAJOR_VERSION}/site-packages/*" >> prerm
+ # Remove bin/pip (and bin/pip3* variants) installed by ensurepip in postinst: