Last updated: Apr 9, 2024
Reading timeยท4 min

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.
# ๐๏ธ 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.

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.
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.
# ๐๏ธ 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.
# ๐๏ธ 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).
Alternatively, you can use xargs to remove all packages installed by pip.
# ๐๏ธ 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.
pip install -r to_remove.txt
You might have to install pip, setuptools and wheel after running the
command.
# ๐๏ธ 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
Alternatively, you can simply recreate your virtual environment.
# ๐๏ธ (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 venvpy -m venv venvWe 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.
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.
You can learn more about the related topics by checking out the following tutorials:
--no-cache-dir option