diff --git a/news/11096.feature.rst b/news/11096.feature.rst new file mode 100644 index 00000000000..cf85edf4cdc --- /dev/null +++ b/news/11096.feature.rst @@ -0,0 +1 @@ +Add ``--dry-run`` to pip install. diff --git a/src/pip/_internal/commands/install.py b/src/pip/_internal/commands/install.py index 55edb280c96..7d7f0791f32 100644 --- a/src/pip/_internal/commands/install.py +++ b/src/pip/_internal/commands/install.py @@ -85,6 +85,17 @@ def add_options(self) -> None: self.cmd_opts.add_option(cmdoptions.pre()) self.cmd_opts.add_option(cmdoptions.editable()) + self.cmd_opts.add_option( + "--dry-run", + action="store_true", + dest="dry_run", + default=False, + help=( + "Don't actually install anything, just print what would be. " + "Can be used in combination with --ignore-installed " + "to 'resolve' the requirements." + ), + ) self.cmd_opts.add_option( "-t", "--target", @@ -341,6 +352,17 @@ def run(self, options: Values, args: List[str]) -> int: reqs, check_supported_wheels=not options.target_dir ) + if options.dry_run: + items = [ + f"{item.name}-{item.metadata['version']}" + for item in sorted( + requirement_set.all_requirements, key=lambda x: str(x.name) + ) + ] + if items: + write_output("Would install %s", " ".join(items)) + return SUCCESS + try: pip_req = requirement_set.get_requirement("pip") except KeyError: diff --git a/tests/functional/test_install.py b/tests/functional/test_install.py index bec8b72fc96..36150858df3 100644 --- a/tests/functional/test_install.py +++ b/tests/functional/test_install.py @@ -2243,3 +2243,12 @@ def test_install_logs_pip_version_in_debug( result = script.pip("install", "-v", fake_package) pattern = "Using pip .* from .*" assert_re_match(pattern, result.stdout) + + +def test_install_dry_run(script: PipTestEnvironment, data: TestData) -> None: + """Test that pip install --dry-run logs what it would install.""" + result = script.pip( + "install", "--dry-run", "--find-links", data.find_links, "simple" + ) + assert "Would install simple-3.0" in result.stdout + assert "Successfully installed" not in result.stdout