How to delete an installed module in Python?


You can uninstall a Python package on Windows by opening the Windows Command Prompt and entering the following command −

pip uninstall module_name

Uninstalling a package using pip

Python's package manager is called PIP. In other words, it is a tool that enables us to install Python packages and dependencies (the software elements needed by your code to function without throwing any errors) that are not already available to us through the Python Standard Library.

A computer language's tool that makes it simple to install any external dependencies is known as a package manager. Any package can be installed or uninstalled without issue.

Steps involved for uninstalling using pip

Following are the steps to uninstall a package or module using pip command −

  • Open the command prompt.

  • With the help of "PIP uninstall module_name” command, uninstall the module.

  • The flask package will be deleted.

  • In the Python 2.7 version, flask uninstall through pip.

  • This would be "pip3.6 uninstall --user flask" for Python 3.6.

  • Following a list of the files that need to be deleted, the command will request your approval. Enter the Enter key after typing "y" to confirm this action.

Note − Installed packages that are directly on the system cannot be removed.

Example

Following example shows how to uninstall python module using pip command −

C:\Users\Lenovo>pip uninstall scipy

Output

Following is an output of the above code −

Found existing installation: scipy 1.8.1
Uninstalling scipy-1.8.1:
   Would remove:
   c:\users\lenovo\appdata\local\programs\python\python310\lib\site-packages\scipy-1.8.1.dist-info\*
   c:\users\lenovo\appdata\local\programs\python\python310\lib\site-packages\scipy\*
Proceed (Y/n)? y
   Successfully uninstalled scipy-1.8.1

Note − There are a few exceptions, though. Using pip, it is difficult to delete these packages:

  • Pure distutils packages installed with python setup.py install do not leave any metadata of the files they installed.

  • The script wrappers that Python setup.py develop install.

All files must be manually deleted, and any other activities performed during installation must be undone. If you are unsure about the complete list of files, you can reinstall it using the --record option and examine the results. To make a list of the installed file, you can use −

python setup.py install --record files.txt

Now that you have a list of all the files in the files.txt, you may manually delete them.

Uninstalling a package using conda

The main tool for managing package installations is the conda command. It can −

  • Form new environments for conda.
  • The Anaconda package index and current Anaconda installation can be queried and searched.
  • Installing and updating packages in conda environments that already exist.

Steps involved for uninstalling using conda

Following are the steps to uninstall a package or module using conda command −

  • To view a list of all the Anaconda virtual environments, open the Anaconda Navigator window and select the Environments menu item from the window's left side.

  • Select the Open Terminal menu option from the popup menu list after clicking the green triangle at the end of one Anaconda virtual environment.

  • It will enter your chosen anaconda virtual environment and start a dos or terminal window.

  • With the help of "conda uninstall module_name” command, uninstall the module.

  • To confirm the uninstall outcome, execute the conda list package-name command one more.

Example

Following example shows how to uninstall python module using conda command −

(base) C:\Users\Lenovo>conda uninstall numpy

Output

Following is an output of the above code

Collecting package metadata (repodata.json): done
Solving environment: done

## Package Plan ##
   environment location: C:\Users\Lenovo\anaconda3

   removed specs:
      - numpy

The following packages will be REMOVED:

   blas-1.0-mkl
   intel-openmp-2021.4.0-haa95532_3556
   mkl-2021.4.0-haa95532_640
   mkl-service-2.4.0-py39h2bbff1b_0
   mkl_fft-1.3.1-py39h277e83a_0
   mkl_random-1.2.2-py39hf11a4ad_0
   numpy-1.23.1-py39h7a0a035_0
   numpy-base-1.23.1-py39hca35cd5_0

Proceed ([y]/n)? y

Preparing transaction: done
Verifying transaction: done
Executing transaction: done

Updated on: 23-Nov-2022

30K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements