Skip to content

Commit 63071d4

Browse files
committed
Add a --dry-run option to pip install
1 parent b49f5bf commit 63071d4

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

news/11096.feature.rst

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add ``--dry-run`` option to ``pip install``, to let it print what it would install but
2+
not actually change anything in the target environment.

src/pip/_internal/commands/install.py

+22
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,17 @@ def add_options(self) -> None:
8585
self.cmd_opts.add_option(cmdoptions.pre())
8686

8787
self.cmd_opts.add_option(cmdoptions.editable())
88+
self.cmd_opts.add_option(
89+
"--dry-run",
90+
action="store_true",
91+
dest="dry_run",
92+
default=False,
93+
help=(
94+
"Don't actually install anything, just print what would be. "
95+
"Can be used in combination with --ignore-installed "
96+
"to 'resolve' the requirements."
97+
),
98+
)
8899
self.cmd_opts.add_option(
89100
"-t",
90101
"--target",
@@ -342,6 +353,17 @@ def run(self, options: Values, args: List[str]) -> int:
342353
reqs, check_supported_wheels=not options.target_dir
343354
)
344355

356+
if options.dry_run:
357+
items = [
358+
f"{item.name}-{item.metadata['version']}"
359+
for item in sorted(
360+
requirement_set.all_requirements, key=lambda x: str(x.name)
361+
)
362+
]
363+
if items:
364+
write_output("Would install %s", " ".join(items))
365+
return SUCCESS
366+
345367
try:
346368
pip_req = requirement_set.get_requirement("pip")
347369
except KeyError:

tests/functional/test_install.py

+9
Original file line numberDiff line numberDiff line change
@@ -2243,3 +2243,12 @@ def test_install_logs_pip_version_in_debug(
22432243
result = script.pip("install", "-v", fake_package)
22442244
pattern = "Using pip .* from .*"
22452245
assert_re_match(pattern, result.stdout)
2246+
2247+
2248+
def test_install_dry_run(script: PipTestEnvironment, data: TestData) -> None:
2249+
"""Test that pip install --dry-run logs what it would install."""
2250+
result = script.pip(
2251+
"install", "--dry-run", "--find-links", data.find_links, "simple"
2252+
)
2253+
assert "Would install simple-3.0" in result.stdout
2254+
assert "Successfully installed" not in result.stdout

0 commit comments

Comments
 (0)