How to Uninstall or Delete a Python Package

How to Convert PNG to TIFF in Python?

Are you a developer scouring the web to find out how to uninstall or delete a package on Python? Then this is the perfect guide for you!

A package manager is a tool that enables developers to control the dependencies of their project. A dependency is a portion of code that is needed in order for your program to work as intended and avoid any errors. Dependencies more often than not are also known as packages. Additionally, packages can also have their own dependencies.

Luckily for us, Python has its own repository called the Python Package Index which is the largest Python repository. So, this Python Package Index consists of many packages that are actively developed and maintained by the Python community all around the world. 

Additionally, Python comes with a package manager called pip. Pip is essentially a tool that allows us to install and manage libraries and dependencies. Also, pip has the ability to install packages from a variety of different sources, including the Python Package Index.

In the event that we need to uninstall a package, we can use pip. Therefore, in this guide, we’ll go over how to uninstall a Python package whether you’re on Windows, macOS, and Linux.

 

 

Global vs Local Python Packages

A locally installed python package is a package that is installed to be used for a specific project. Hence, it’s located in your projects directory. Locally installed python packages are also accessible to the user that installed it. 

On the other hand, a globally installed python package is one that is installed under /usr/local/bin for Unix systems and \Program Files\ for Windows systems and is accessible to all users. When using the pip command to install packages, it installs it globally by default. 

 

Uninstalling Packages Locally

In order to uninstall or remove a package that was installed locally, we can use the native command line. Simply enter the following command:

pip uninstall <packagename>

 

Uninstalling Packages Globally

Sometimes, a package may be installed both locally and globally. Suppose you want to uninstall a package globally or if you want to make sure that a package is completely removed from your system after uninstalling it locally. In that case, the following are steps to uninstall a Python package globally whether on a Windows or Linux device:

 

For Windows

  1. First, run the Command Prompt as an administrator.

run Command Prompt as an administrator on Windows

 

  1. Then, type the following command in the command prompt window:
pip uninstall <packagename>

 

For Linux

  1. First, open the Terminal

open terminal on macOS or Linux

 

  1. Then, type in the following command and click the Enter key:
sudo su pip uninstall <packagename>

 

Uninstalling Packages in a Python Virtual Environment

A virtual environment is used to create a separate environment for python projects. Here, each project can have its own dependencies that are separate from the dependencies of other projects. Virtual environments are easy to create and anyone can have any number of environments since they’re essentially just directories with a couple of scripts.

Therefore, the following are 2 ways to uninstall a local package from a virtual environment, either using Pip or Pipenv. These steps apply to most of the major OS you might be using, such as Windows, macOS, or Linux:

 

Method 1: Using Pip

  1. To start, open a Command Prompt window (Windows) or a Terminal window (Linux, macOS). 
  2. Next, navigate to the directory where the project is located. If you’re unsure of how to change directories through your Command Prompt/Terminal, you can check out the following video:

 

  1. Lastly, enter the following command:
pip uninstall <packagename>

 

Method 2: Using Pipenv

  1. Firstly, open a Command Prompt window (Windows) or a Terminal window (Linux). 
  2. After that, navigate to the directory where the project is located. For this, you can watch the video in the previous section.
  3. Lastly, enter the following command:
pipenv uninstall <packagename>

 

Uninstalling the Dependencies of a Package with Pip

When we use pip to install a package, not only does it install the package itself, but it also installs all of its dependencies. However, when we use pip to uninstall a package, it doesn’t uninstall the dependencies that come with it. 

Therefore, if you would like to uninstall a package’s dependencies, here’s what you can do:

  1. In the event that your project contains a requirements.txt file and you installed a package using that file, the following command can be used to uninstall all the packages in requirements.txt:
pip uninstall requirements.txt

Note: the requirements.txt file is a file used to list out the packages that are required to run a project. The requirements.txt file can usually be found in the root directory of a project.

  1. Another method that can be used to uninstall the dependencies of a package is by listing out the requirements of a package and then proceeding to uninstall those packages. This can be done by following the steps below:
    1. First, use the following command to display the requirements of a package. For this example, we’ll use the requests package:

python requests package

 

    1. Next, from the second last line of the screenshot above, we can see that the requests package requires the urllib3, idna, certifi, and charset-normalizer packages. Hence, we can proceed to uninstall these dependencies by using the following command:
pip uninstall <packagename>

 

Uninstalling a Package Installed with Setuptools Using Pip

Setuptools is a tool that allows developers to make and distribute packages in an easier way. There is no specific uninstall command for packages that were installed using setuptools. However, we can use the aforementioned pip command:

pip uninstall <packagename>

 

Conclusion

The management of packages is of utmost importance for a developer when it comes to coding projects. This can be proven by the fact that pip is the fourth most used python package with a whopping 627 million downloads. This shows how powerful of a tool pip is and why so many developers use it to manage their Python packages. 

There are many reasons why a developer would have to uninstall a package, ranging from a wrongly installed package to simply not needing to use one anymore, and etc.

In this article, we looked at many different ways for a developer to uninstall python packages. These include uninstalling packages locally, globally, in a virtual environment, and also uninstalling the dependencies of a package using pip. Additionally, we also covered the differences between local and global python packages. 

That being said, hopefully, you’ve found this guide helpful to uninstall Python packages in your development space.

Feel free to share this post with your fellow coders to guide them to uninstall their Python package!

Total
0
Shares
1 comment
Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts
Total
0
Share