Path: blob/master/venv/Lib/site-packages/pip/_internal/commands/uninstall.py
811 views
# The following comment should be removed at some point in the future.1# mypy: disallow-untyped-defs=False23from __future__ import absolute_import45from pip._vendor.packaging.utils import canonicalize_name67from pip._internal.cli.base_command import Command8from pip._internal.cli.req_command import SessionCommandMixin9from pip._internal.exceptions import InstallationError10from pip._internal.req import parse_requirements11from pip._internal.req.constructors import (12install_req_from_line,13install_req_from_parsed_requirement,14)15from pip._internal.utils.misc import protect_pip_from_modification_on_windows161718class UninstallCommand(Command, SessionCommandMixin):19"""20Uninstall packages.2122pip is able to uninstall most installed packages. Known exceptions are:2324- Pure distutils packages installed with ``python setup.py install``, which25leave behind no metadata to determine what files were installed.26- Script wrappers installed by ``python setup.py develop``.27"""2829usage = """30%prog [options] <package> ...31%prog [options] -r <requirements file> ..."""3233def __init__(self, *args, **kw):34super(UninstallCommand, self).__init__(*args, **kw)35self.cmd_opts.add_option(36'-r', '--requirement',37dest='requirements',38action='append',39default=[],40metavar='file',41help='Uninstall all the packages listed in the given requirements '42'file. This option can be used multiple times.',43)44self.cmd_opts.add_option(45'-y', '--yes',46dest='yes',47action='store_true',48help="Don't ask for confirmation of uninstall deletions.")4950self.parser.insert_option_group(0, self.cmd_opts)5152def run(self, options, args):53session = self.get_default_session(options)5455reqs_to_uninstall = {}56for name in args:57req = install_req_from_line(58name, isolated=options.isolated_mode,59)60if req.name:61reqs_to_uninstall[canonicalize_name(req.name)] = req62for filename in options.requirements:63for parsed_req in parse_requirements(64filename,65options=options,66session=session):67req = install_req_from_parsed_requirement(68parsed_req,69isolated=options.isolated_mode70)71if req.name:72reqs_to_uninstall[canonicalize_name(req.name)] = req73if not reqs_to_uninstall:74raise InstallationError(75'You must give at least one requirement to {self.name} (see '76'"pip help {self.name}")'.format(**locals())77)7879protect_pip_from_modification_on_windows(80modifying_pip="pip" in reqs_to_uninstall81)8283for req in reqs_to_uninstall.values():84uninstall_pathset = req.uninstall(85auto_confirm=options.yes, verbose=self.verbosity > 0,86)87if uninstall_pathset:88uninstall_pathset.commit()899091