How To Uninstall Python Packages
The process of uninstalling Python packages and their associated dependencies can be efficiently accomplished using the pip package manager. Pip, as an inherent tool for Python, offers built-in capabilities to manage package installation, upgrading, and uninstallation, streamlining the package management workflow for developers and ensuring a clean and organized Python environment.
On Windows:
On Linux:
On macOS:
Uninstall Python Packages
Following is a step-by-step explanation of how to uninstall a package in Python using pip:
- Open a terminal/command prompt
- Check if pip is installed
- List installed packages
- Uninstall the package
- Verify that the package is uninstalled
Open a terminal/command prompt
Open a terminal/command prompt on your system.
Check if pip is installed
Check if pip is installed on your system by running the following command:
If pip is installed, you will see its version number in the output. If not, you will see an error message.
List installed packages
List all the packages that are currently installed on your system using the following command:
Above command command will display a list of all the packages installed on your system along with their version numbers.
Uninstall the package
To uninstall a package, use the following command:
Replace package_name with the name of the package you want to uninstall.
If the package has any dependencies that are no longer required, pip will also prompt you to uninstall them. You can choose whether or not to uninstall them by typing y or n.
Verify that the package is uninstalled
After the package is uninstalled, verify that it is no longer installed on your system by running the pip list command again.
If the package is no longer listed, then it has been successfully uninstalled.
Now you have successfully uninstalled a package in Python using pip.
Uninstalling python packages and dependencies
If the package has dependencies that are no longer required, pip will prompt you to uninstall them as well. You can choose to uninstall the dependencies by typing y or to keep them by typing n.
Verify the package is uninstalled
To verify that the package has been successfully uninstalled, run the pip list command again:
If the package is no longer listed, then it has been successfully uninstalled.
Uninstall dependencies
If you have uninstalled a package and want to remove its dependencies, you can use the pip autoremove command:
Above command will remove any packages that are no longer needed by your system.
It's important to note that, be careful when using the pip autoremove command as it may remove packages that are still being used by other packages. It's always a good idea to verify which packages will be removed before running this command.
Now you have successfully uninstalled a Python package and its dependencies using pip.
Uninstall packages in a Python virtual environment
Uninstalling packages in a Python virtual environment is similar to uninstalling packages in a standard Python environment. However, it is important to activate the virtual environment before running the uninstallation command.
Following are the steps to uninstall packages in a Python virtual environment:
Activate the virtual environment
Navigate to the directory where the virtual environment is installed and activate it. The activation command depends on the operating system and the shell you are using.
WindowsOn Windows using Command Prompt, you can activate the virtual environment by running the following command:
On macOS or Linux using bash shell, you can activate the virtual environment by running the following command:
Uninstall the package
Once the virtual environment is activated, you can use the pip uninstall command to uninstall the package. Replace package_name with the name of the package you want to uninstall:
Verify the package is uninstalled
To verify that the package has been successfully uninstalled, run the following command:
Above command will display a list of all installed packages in the virtual environment. If the package you just uninstalled is not listed, then it has been successfully uninstalled.
Deactivate the virtual environment
After you have uninstalled the package, you can deactivate the virtual environment by running the following command:
Above command will return your command prompt to the standard shell environment.
Now you have successfully uninstalled a package in a Python virtual environment.
Uninstall Python Package Dependencies With Pipenv
Pipenv is a tool for managing Python environments and packages. It provides a way to install packages in a virtual environment, along with their dependencies. To uninstall a package and its dependencies using Pipenv, follow these steps:
Activate the Pipenv environment
Open your command line interface and navigate to the directory where your Pipenv environment is located. Activate the environment by running the following command:
Above command will activate the virtual environment and set the environment variables needed to use Pipenv.
Uninstall the package and its dependencies
To uninstall a package and its dependencies, use the following command:
Replace package_name with the name of the package you want to uninstall.
For example, if you want to uninstall the numpy package and its dependencies, you can type:
Above command will remove the numpy package and its dependencies from your virtual environment.
Verify the package and its dependencies are uninstalled
To verify that the package and its dependencies have been successfully uninstalled, use the pipenv graph command to list all the packages installed in the virtual environment:
Above command will display a list of all the packages installed in the virtual environment, along with their dependencies. If the package and its dependencies are no longer listed, then they have been successfully uninstalled.
Deactivate the Pipenv environment
After you have uninstalled the package and its dependencies, you can deactivate the virtual environment by running the following command:
Above command will return your command prompt to the standard shell environment.
Now you have successfully uninstalled a package and its dependencies using Pipenv.
Uninstall A python Package Installed With Setuptools
Uninstalling a Python package installed with Setuptools is similar to uninstalling a package installed with pip.
Conclusion
To uninstall Python packages, you can use the pip package manager with the following command: pip uninstall package_name. This command will remove the specified package and its dependencies from your Python environment, allowing you to efficiently manage your package installations and maintain a clean development environment.
- Keywords in Python
- Python Operator - Types of Operators in Python
- Python Data Types
- Python Shallow and deep copy operations
- Python Datatype conversion
- Python Mathematical Function
- Basic String Operations in Python
- Python Substring examples
- How to check if Python string contains another string
- Check if multiple strings exist in another string : Python
- Memory Management in Python
- Python Identity Operators
- What is a None value in Python?
- How to Install a Package in Python using PIP
- How to update/upgrade a package using pip?
- How to call a system command from Python
- How to use f-string in Python
- Python Decorators (With Simple Examples)
- Python Timestamp Examples