Do you want to uninstall the Python PIP package you installed sometime back but don’t know how? Sometimes, you may want to remove a package and its dependencies, because you no longer need it or troubleshoot a compatibility issue.
Nevertheless, properly uninstalling Python packages can save you valuable time and effort. In this article, we will share some expert tips on how to uninstall PIP packages and dependencies.
How to uninstall the Python PIP package and dependencies
PIP or Preferred Installer Program is the standard package manager for Python that manages its packages and dependencies. It’s written in Python programming language and is executed as a command line to install, uninstall, or reinstall Python packages and their modules. It comes pre-installed in the majority of the Python versions such as Python 3.4 and later & Python 2.7.9 and later.
So, if someday, you want to uninstall the Python package and its dependencies using PIP, we have it covered for you.
- Preparatory steps
- Uninstall individual Python package
- Uninstall Python packages and dependencies
- Uninstall packages in the Python virtual environment
1] Preparatory steps
Before you proceed to uninstall the Python package using PIP, you must follow the preparatory stages as shown below:
Check if PIP is installed
Firstly, you must check if PIP is already installed. For this, open the Command Prompt with admin rights, run the below command, and hit Enter:
pip --version
If PIP is installed, it will display the version number.
If PIP is not installed, it could be because you missed checking the PIP box when installing Python. In that case, you can follow the guidelines mentioned in our detailed article here.
Add Python to the Windows path
Once you are done checking the PIP version, you must check if you have added Python to the Windows path. If not, here’s how to do it:
Launch the Run console (Win + R) > sysdm.cpl > Enter. This will open the System Properties dialog box.
Here, select the Advanced tab, and click on Environment Variables.
Next, under User variables, click on New.
Now, add the Variable name and Variable value.
In the Variable Value field, you should add the Python application path and Python Scripts path. Follow the below instructions to find the paths:
- Look for the Python app on Windows search, right-click on it, and select Open file location.
- Now, right-click on the Python shortcut and again click on Open file location.
- The app path should appear as C:\Users\cy\AppData\Local\Programs\Python\Python100
- The scripts path should appear as C:\Users\cy\AppData\Local\Programs\Python\Python100\Scripts
- Make sure to replace the Python version with yours.
- Copy these two paths and paste them into the Variable Value field, separating them with a semi-colon.
Next, proceed to uninstall the Python package.
Read: How to open and view Python PY files on Windows
2] Uninstall individual Python package
To uninstall PIP packages one by one, go to Windows search, type in Command Prompt right-click on it, and select Run as administrator. Now, type cd∖ , and hit Enter.
Now, run the command in the below format and hit Enter:
cd C:\Users\cy\AppData\Local\Programs\Python\Python100\Scripts
Make sure to replace the Python package version (in bold) with yours.
Now, run the below command and press Enter:
pip uninstall package_name
Replace the package name with the one you have installed, for example, NumPy, Pandas, Seaborn, etc.
Then type Y to proceed with the uninstallation.
Read: Fix Command python setup.py egg_info failed with error code 1
3] Uninstall Python packages and dependencies
To remove all Python packages that you installed using PIP, you can follow the below process. By running the below command (PIP freeze) you can uninstall all of them without any need for confirmation.
The complete command for this would be in the below format:
pip uninstall -y -r <(pip freeze)
You can also save the installed packages in the requirements.txt file and directly uninstall all the PIP packages from the file. For this, run the below commands one after another and hit Enter after each one:
pip freeze > requirements.txt
pip uninstall -r requirements.txt (To uninstall packages one by one)
pip uninstall -r requirements.txt -y (To delete all the packages at once)
Moreover, you can also use xargs to uninstall all the PIP packages. Type in the below command and hit Enter:
pip freeze | xargs pip uninstall -y
But, if you have installed packages using VCS for example, GitLab, Github, etc., make sure to remove them.
You can then uninstall Python packages with PIP using the below command:
pip freeze | grep -v “^-e” | xargs pip uninstall -y
To check if the package has been uninstalled successfully, run the below command and hit Enter:
pip list
If you don’t see the package listed in the results, it means it has been uninstalled successfully.
Now, to uninstall the dependencies for the package that you removed, run the below command and hit Enter:
pip autoremove
This command will uninstall all packages that are no more needed by the system.
At the same time, while using the PIP autoremove command, re-check what packages are to be removed, else, it may uninstall even those packages that are currently in use.
Read: How to install Python in Windows 11
4] Uninstall packages in the Python virtual environment
In this case, you need to first activate the virtual environment before you run the command to uninstall the same.
To activate the virtual environment, go to the directory where it’s installed. Now, to activate it, type in the below command and press the Enter key:
venv\Scripts\activate.bat
Once you have activated the Python virtual environment, run the below command to uninstall the same and hit Enter:
pip uninstall package_name
Now that you have uninstalled the package, run the below command to deactivate the virtual environment and hit Enter:
deactivate
That’s it! You have successfully removed the Python packages using PIP.
How do I delete unused pip packages?
To remove pip packages that are not in use, run the command pip uninstall package_name. Make sure to replace package_name with the actual name of the package you wish to remove, such as Pandas. Type in the command in your terminal or command prompt, and confirm the uninstallation when prompted.
How to uninstall Python packages without pip?
To uninstall Python packages without PIP, you can use the setup.py file. Then go to the directory where the package is installed, run the python setup.py uninstall command if available, or remove the package directory manually from the site-packages folder within your Python environment. But make sure that it doesn’t affect system stability.