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:

pip uninstall package_name

On Linux:

sudo -H pip uninstall package_name

On macOS:

pip uninstall package_name

Uninstall Python Packages

Following is a step-by-step explanation of how to uninstall a package in Python using pip:

  1. Open a terminal/command prompt
  2. Check if pip is installed
  3. List installed packages
  4. Uninstall the package
  5. 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:

pip --version

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:

pip list

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:

pip uninstall package_name

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


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:

pip list

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:

pip autoremove

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.

Windows

On Windows using Command Prompt, you can activate the virtual environment by running the following command:

venv\Scripts\activate.bat
macOS or Linux

On macOS or Linux using bash shell, you can activate the virtual environment by running the following command:

source venv/bin/activate

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:

pip uninstall package_name

Verify the package is uninstalled

To verify that the package has been successfully uninstalled, run the following command:

pip freeze

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:

deactivate

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:

pipenv shell

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:

pipenv uninstall package_name

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:

pipenv uninstall numpy

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:

pipenv graph

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:

exit

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.