Explain Codes LogoExplain Codes Logo

Python setup.py uninstall

python
pip
uninstall
manual-removal
Nikita BarsukovbyNikita Barsukov·Sep 10, 2024
TLDR

To uninstall Python packages that have been installed via setup.py, you use pip. Run the following command:

pip uninstall package-name

Ensure you replace package-name with the exact name of the package. If you're unsure of the package name, use pip list to locate it within your installed packages.

Pip-unfriendly packages: How to manually uninstall

In some cases, packages installed via the python setup.py method might resist uninstallation through the pip uninstall command. In such cases, you'll need to manually remove the installed files.

Step 1: List all installed files

At the time of package installation, generate a record of all installed files with:

python setup.py install --record files.txt

This will be your "treasure map" for package uninstallation; instead of gold, it leads to the package's uninstallation!

Step 2: Uninstall on Unix systems

On Unix-like systems, remove the installed files by running:

cat files.txt | xargs rm -rf

In other words, this command transforms your "treasure map" into your package-eliminating lightsaber! 😄🗡️🔥

Step 3: Remove files on Windows

On Windows systems, use the following Powershell command to remove all installed files:

Get-Content files.txt | ForEach-Object {Remove-Item $_ -Force}

This command starves the package to extinction on Windows! 🦖🚫

Step 4: Triple-check the package extinction

Upon completion of the deletion process, verify that Python no longer recognizes the package. Attempting to import the package:

import package-name

Should result in a ModuleNotFoundError. If it does, then congrats! You've successfully made Python forget your package! 🎉🔥

Step 5: Say goodbye to the residuals

Some package installations can spawn additional items such as environment variables or configuration file changes. Ensure you reverse any changes made during the installation process to keep your environment clean and tidy.

Step 6: Directory removal - The hard reset

If the package continues to haunt your system, you might need to resort to directory deletion. Get the location of the package's directory by running:

pip show package-name

Once located, remove the package's directory. Sayonara, stubborn package! 🎯

Visualization

Visualizing the concept of python setup.py uninstall:

Installing a package is like planting a tree (🌳) in your garden.

When you want to uninstall it:

Just like you can't "unplant" a tree, you can't do:

python setup.py uninstall // Nope! 🚫🌳

You've to manually remove each root (files/folders) linked to the package.

# Using your "gardening tools" for package removal pip uninstall package_name // Uproot! 🧹🌳

Once removed, your garden (Python environment) is tidy again:

No more trees Clean Garden = Clean Python Environment 🌿

Complete! 🎉

Practical tips and cautionary advice

Virtual environments: A friend in need

Consider always installing packages within a virtual environment. This Python sandboxing ensures that uninstalling a package is as easy as deactivating and deleting the virtual environment folder.

Pip-install: A future-proof habit

If you are foreseeing the need for uninstalling packages, consider using pip install . for installation instead of setup.py. This makes the package visibly installed to pip, ensuring it can be smoothly uninstalled later.

Pip resistance: When packages fight back

Sometimes packages resist peaceful removal. In such rare scenes, pip uninstall might fight back. Plan B? Upgrade pip with the command:

python -m ensurepip --upgrade

Subsequently, attempt the uninstallation again. This is like asking a supervisor to "please take care of this"! 😉

Manual cleanup: Just like spring cleaning

If pip fails to remove your package, prepare for a manual cleanup. Ensure you have a list of files to delete (remember files.txt?) and double-check it. It's your equivalent of checking under the bed and behind the fridge during spring cleaning! 🧹🌼