Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pip uninstall -a/--all to wipe an entire environment #9751

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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