Remove/uninstall all packages installed by pip in Python

avatar
Borislav Hadzhiev

Last updated: Feb 20, 2023
4 min

banner

# Remove/uninstall all packages installed by pip in Python

Use the pip uninstall -y -r <(pip freeze) command to remove all packages installed by pip.

The command uses pip freeze to get a list of the installed packages and uninstalls them without asking for confirmation.

shell
# ๐Ÿ‘‡๏ธ optionally save the packages that are to be removed pip freeze > to_remove.txt # ๐Ÿ‘‡๏ธ remove/uninstall all packages installed by pip pip uninstall -y -r <(pip freeze)

The command uses pip freeze to get a list of the installed packages in requirements format and uninstalls the packages.

pip freeze output

The -y option is short for -yes and means that pip should not ask for confirmation when uninstalling.

The -r option is short for --requirement and uninstalls all the packages in the given requirements file.

If you need to install the packages again, use the pip install -r command.

shell
pip install -r to_remove.txt

If you get an error message that setuptools is not installed, e.g. No module named 'pkg_resources', run the following command.

shell
# ๐Ÿ‘‡๏ธ for Linux or MacOS python -m pip install --upgrade pip setuptools wheel python3 -m pip install --upgrade pip setuptools wheel # ๐Ÿ‘‡๏ธ Windows py -m pip install --upgrade pip setuptools wheel # ๐Ÿ‘‡๏ธ try upgrading pip pip install --upgrade pip setuptools pip3 install --upgrade pip setuptools

You can also store the installed packages in a file and uninstall them directly from the file.

shell
# ๐Ÿ‘‡๏ธ store the installed packages in a file called reqs.txt pip freeze > reqs.txt # ๐Ÿ‘‡๏ธ uninstall packages 1 by 1 pip uninstall -r reqs.txt # ๐Ÿ‘‡๏ธ uninstall all packages without confirmation required pip uninstall -r reqs.txt -y

I used the name reqs.txt because you might not want to override the contents of your existing requirements.txt (if you already have one).

# Remove/uninstall all packages installed by pip using xargs

Alternatively, you can use xargs to remove all packages installed by pip.

shell
# ๐Ÿ‘‡๏ธ optionally save the packages that are to be removed pip freeze > to_remove.txt # ๐Ÿ‘‡๏ธ remove/uninstall all packages installed by pip pip freeze | xargs pip uninstall -y

The command uses pip freeze to get a list of the installed packages and then uses xargs to pass the packages as an argument to the pip uninstall command.

We then used the xargs command to pass the installed packages as an argument to the pip uninstall command.

The -y (yes) option makes it so pip doesn't ask for confirmation when uninstalling packages.

If you need to install the packages again, use the pip install -r command.

shell
pip install -r to_remove.txt

You might have to install pip, setuptools and wheel after running the command.

shell
# ๐Ÿ‘‡๏ธ for Linux or MacOS python -m pip install --upgrade pip setuptools wheel python3 -m pip install --upgrade pip setuptools wheel # ๐Ÿ‘‡๏ธ Windows py -m pip install --upgrade pip setuptools wheel # ๐Ÿ‘‡๏ธ try upgrading pip pip install --upgrade pip setuptools pip3 install --upgrade pip setuptools

# Recreate your virtual environment

Alternatively, you can simply recreate your virtual environment.

  1. Deactivate your virtual environment.
  2. Delete the folder of your virtual environment.
  3. Create a new virtual environment.
  4. Activate the new virtual environment.
shell
# ๐Ÿ‘‡๏ธ (optional) store the packages to be removed pip freeze > to_remove.txt # ๐Ÿ‘‡๏ธ deactivate deactivate # ๐Ÿ‘‡๏ธ Remove the old virtual environment folder: macOS and Linux rm -rf venv # ๐Ÿ‘‡๏ธ Remove the old virtual environment folder: Windows rd /s /q "venv" # ๐Ÿ‘‡๏ธ initialize a new virtual environment python -m venv venv # ๐Ÿ‘‡๏ธ activate on Unix or MacOS source venv/bin/activate # ๐Ÿ‘‡๏ธ activate on Windows (cmd.exe) venv\Scripts\activate.bat # ๐Ÿ‘‡๏ธ activate on Windows (PowerShell) venv\Scripts\Activate.ps1 # ๐Ÿ‘‡๏ธ (optional) install the modules in your requirements.txt file pip install -r requirements.txt

If running python -m venv venv doesn't work, try the following commands:

  • python3 -m venv venv
  • py -m venv venv

We used the deactivate command to deactivate the virtual environment, deleted the venv folder and created a new virtual environment.

Your virtual environment will use the version of Python that was used to create it.

Make sure to use the correct activation command depending on your operating system.

You might have to upgrade your pip version in the new virtual environment.

shell
pip install --upgrade pip

You can use the pip install -r requirements.txt command in the new virtual environment if you need to install any packages from a requirements file.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2023 Borislav Hadzhiev