Skip to content

Commit

Permalink
Add pip uninstall -a/--all to uninstall all installed requirements
Browse files Browse the repository at this point in the history
This ensures that all packages installed in an environment are
uninstalled.
  • Loading branch information
Nalin-Angrish authored and pradyunsg committed Mar 27, 2023
1 parent 82b42c8 commit 99fef4f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
3 changes: 3 additions & 0 deletions news/9751.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Added a ``-a`` and ``--all`` arguments to ``pip uninstall``
so that if the developer wants to start afresh in a virtualenv
by clearing up all dependencies, he/she can do that easily.
21 changes: 20 additions & 1 deletion src/pip/_internal/commands/uninstall.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
from pip._internal.cli.req_command import SessionCommandMixin, warn_if_run_as_root
from pip._internal.cli.status_codes import SUCCESS
from pip._internal.exceptions import InstallationError
from pip._internal.metadata import get_default_environment
from pip._internal.req import parse_requirements
from pip._internal.req.constructors import (
install_req_from_line,
install_req_from_parsed_requirement,
)
from pip._internal.utils.compat import stdlib_pkgs
from pip._internal.utils.misc import (
check_externally_managed,
protect_pip_from_modification_on_windows,
Expand All @@ -35,7 +37,8 @@ class UninstallCommand(Command, SessionCommandMixin):

usage = """
%prog [options] <package> ...
%prog [options] -r <requirements file> ..."""
%prog [options] -r <requirements file> ...
%prog [options] -a"""

def add_options(self) -> None:
self.cmd_opts.add_option(
Expand All @@ -57,6 +60,13 @@ def add_options(self) -> None:
action="store_true",
help="Don't ask for confirmation of uninstall deletions.",
)
self.cmd_opts.add_option(
"-a",
"--all",
dest="all",
action="store_true",
help="Uninstall all Installed packages.",
)
self.cmd_opts.add_option(cmdoptions.root_user_action())
self.cmd_opts.add_option(cmdoptions.override_externally_managed())
self.parser.insert_option_group(0, self.cmd_opts)
Expand Down Expand Up @@ -88,6 +98,15 @@ def run(self, options: Values, args: List[str]) -> int:
)
if req.name:
reqs_to_uninstall[canonicalize_name(req.name)] = req
if options.all:
env = get_default_environment()
for dist in env.iter_installed_distributions(
local_only=False, skip=stdlib_pkgs | {"pip"}
):
reqs_to_uninstall[dist.canonical_name] = install_req_from_line(
dist.canonical_name,
isolated=options.isolated_mode,
)
if not reqs_to_uninstall:
raise InstallationError(
f"You must give at least one requirement to {self.name} (see "
Expand Down

0 comments on commit 99fef4f

Please sign in to comment.