Hey! If you love Python and building Python apps as much as I do, let's connect on Twitter or LinkedIn. I talk about this stuff all the time!

Uninstalling PyTorch

Learn how to properly uninstall PyTorch and its dependencies, ensuring a clean Python environment. …


Updated May 13, 2023

Learn how to properly uninstall PyTorch and its dependencies, ensuring a clean Python environment.

How to Uninstall PyTorch

Definition of the Concept

PyTorch is an open-source machine learning library developed by Facebook. It provides a dynamic computation graph and automatic differentiation, making it a popular choice for researchers and developers. When you install PyTorch, it adds dependencies to your system, including NumPy, SciPy, and other libraries.

Uninstalling PyTorch means removing these dependencies from your system, leaving behind a clean Python environment.

Step-by-Step Explanation

1. Check if PyTorch is Installed

Before uninstalling, verify that PyTorch is indeed installed on your system using the following command:

pip show torch

If PyTorch is not found, you can skip to step 3.

2. Uninstall Using pip

To remove PyTorch and its dependencies using pip, run the following commands:

pip uninstall torch
pip uninstall torchvision # if you installed torchvision separately

This will remove PyTorch and any dependent libraries that were installed via pip.

3. Remove Package Files (Optional)

If you want to delete the package files (.whl) for PyTorch and its dependencies, use the following command:

pip uninstall --force-reinstall torch torchvision # this will also remove package files

Note: This step is optional, as it’s not necessary to remove package files unless you’re sure they won’t be needed again.

4. Remove Library Files (Optional)

To delete the library files (.egg-info and .pyc) for PyTorch and its dependencies, use the following command:

rm -rf /usr/lib/python3.x/site-packages/torch*
rm -rf /usr/lib/python3.x/site-packages/torchvision*

Replace x with your Python version (e.g., 3.9).

Note: This step is also optional, as it’s not necessary to remove library files unless you’re sure they won’t be needed again.

5. Verify Uninstallation

After completing these steps, verify that PyTorch has been successfully uninstalled by checking for the presence of the torch and torchvision packages using pip:

pip show torch

If PyTorch is not found, it has been properly uninstalled.

Conclusion

Uninstalling PyTorch involves removing its dependencies from your system. By following these steps, you can ensure a clean Python environment for future projects.

Stay up to date on the latest in Python, AI, and Data Science

Intuit Mailchimp