Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Uninstalling Packages

00:00 What do you do if you’ve installed the Python package, but you want to uninstall it and completely remove it from your system? For example, if you’ve installed a package just to try it out how do you undo that, how do you uninstall the package? Let’s talk about that now.

00:15 You can uninstall packages and remove them from your system using the pip uninstall command. This command is really similar to the pip install command, you just go pip uninstall and the name of the package and pip will remove the package from your system.

00:30 Here is a quick demo; you can see here that I’ve got the Requests package installed. Let’s remove it. Once I triggered the uninstall command, pip is going to ask me if I really want to remove this package, so at this point I can still cancel the process, and no change will be made to my system, so I am going to go yes here, and hit return, alright, and that’s it, you successfully uninstalled Requests.

00:55 Let’s make sure it’s actually gone. As you can see, Requests no longer shows up in this list of installed packages. You just saw how uninstalling a package through pip removed the package itself.

01:11 However, the question remains whether that also removes dependent packages, or in other words, will uninstalling a package also remove the secondary dependencies of this package?

01:24 And the answer is no, so pip uninstall will not uninstall secondary dependencies. Therefore, it isn’t that easy to keep your Python environment nice and clean, this is why I recommend that you use virtual environments as much as possible, there is a whole section in this course dedicated to virtual environments, but in short, there are way for you to create new Python environments that are completely separate from your global environment, so you can install packages without cluttering up your global Python environment.

01:54 And if you’re using virtual environments then removing packages and cleaning up all of the unneeded secondary dependencies is really easy, you just delete the virtual environment and recreate it from scratch.

02:06 And this will get rid of all the unneeded secondary dependencies.

Avatar image for FooledByCode

FooledByCode on June 2, 2022

Is there a more reasonable way of removing secondary dependencies? In real world we do use virtual environments which has lot more dependencies/packages installed so to delete the virtual environment is something I would not do. Is there a way to figure out secondary dependencies and then uninstall them one by one?

Avatar image for Bartosz Zaczyński

Bartosz Zaczyński RP Team on June 2, 2022

@FooledByCode You can use pipdeptree, which is a third-party package that shows the tree of your dependencies:

$ python -m pipdeptree
  - cycler [required: >=0.10, installed: 0.11.0]
  - fonttools [required: >=4.22.0, installed: 4.33.3]
  - kiwisolver [required: >=1.0.1, installed: 1.4.2]
  - numpy [required: >=1.17, installed: 1.22.4]
  - packaging [required: >=20.0, installed: 21.3]
    - pyparsing [required: >=2.0.2,!=3.0.5, installed: 3.0.9]
  - pillow [required: >=6.2.0, installed: 9.1.1]
  - pyparsing [required: >=2.2.1, installed: 3.0.9]
  - python-dateutil [required: >=2.7, installed: 2.8.2]
    - six [required: >=1.5, installed: 1.16.0]
  - numpy [required: >=1.21.0, installed: 1.22.4]
  - python-dateutil [required: >=2.8.1, installed: 2.8.2]
    - six [required: >=1.5, installed: 1.16.0]
  - pytz [required: >=2020.1, installed: 2022.1]
  - pip [required: >=6.0.0, installed: 22.1.2]
Avatar image for FooledByCode

FooledByCode on June 2, 2022

Thank you very much @Bartosz that will do.

Become a Member to join the conversation.