Skip to content

Commit 342d00d

Browse files
committed
feat: add --with option to "pipx run" command
Closes #1607
1 parent 29c1978 commit 342d00d

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

changelog.d/1607.feature.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add `--with` flag to `pipx run` to allow injecting dependencies

src/pipx/commands/run.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
from pipx import paths
1616
from pipx.commands.common import package_name_from_spec
17+
from pipx.commands.inject import inject_dep
1718
from pipx.constants import TEMP_VENV_EXPIRATION_THRESHOLD_DAYS, WINDOWS
1819
from pipx.emojis import hazard
1920
from pipx.util import (
@@ -111,6 +112,7 @@ def run_script(
111112
def run_package(
112113
app: str,
113114
package_or_url: str,
115+
dependencies: List[str],
114116
app_args: List[str],
115117
python: str,
116118
pip_args: List[str],
@@ -156,6 +158,7 @@ def run_package(
156158
_prepare_venv_cache(venv, bin_path, use_cache)
157159

158160
if venv.has_app(app, app_filename):
161+
# TODO: Should refactor and consider injecting here
159162
logger.info(f"Reusing cached venv {venv_dir}")
160163
venv.run_app(app, app_filename, app_args)
161164
else:
@@ -166,6 +169,7 @@ def run_package(
166169
app,
167170
app_filename,
168171
app_args,
172+
dependencies,
169173
python,
170174
pip_args,
171175
venv_args,
@@ -177,6 +181,7 @@ def run_package(
177181
def run(
178182
app: str,
179183
spec: str,
184+
dependencies: List[str],
180185
is_path: bool,
181186
app_args: List[str],
182187
python: str,
@@ -206,6 +211,7 @@ def run(
206211
run_package(
207212
package_name,
208213
package_or_url,
214+
dependencies,
209215
app_args,
210216
python,
211217
pip_args,
@@ -222,6 +228,7 @@ def _download_and_run(
222228
app: str,
223229
app_filename: str,
224230
app_args: List[str],
231+
dependencies: List[str],
225232
python: str,
226233
pip_args: List[str],
227234
venv_args: List[str],
@@ -275,6 +282,18 @@ def _download_and_run(
275282
# Let future _remove_all_expired_venvs know to remove this
276283
(venv_dir / VENV_EXPIRED_FILENAME).touch()
277284

285+
for dep in dependencies:
286+
inject_dep(
287+
venv_dir=venv_dir,
288+
package_name=None,
289+
package_spec=dep,
290+
pip_args=pip_args,
291+
verbose=verbose,
292+
include_apps=False,
293+
include_dependencies=False,
294+
force=False,
295+
)
296+
278297
venv.run_app(app, app_filename, app_args)
279298

280299

src/pipx/main.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ def run_pipx_command(args: argparse.Namespace, subparsers: Dict[str, argparse.Ar
267267
commands.run(
268268
args.app_with_args[0],
269269
args.spec,
270+
args.with_,
270271
args.path,
271272
args.app_with_args[1:],
272273
args.python,
@@ -845,6 +846,13 @@ def _add_run(subparsers: argparse._SubParsersAction, shared_parser: argparse.Arg
845846
action="store_true",
846847
help="Require app to be run from local __pypackages__ directory",
847848
)
849+
p.add_argument(
850+
"--with",
851+
dest="with_",
852+
action="append",
853+
default=[],
854+
help="Extra dependencies to add to the temporary environment",
855+
)
848856
p.add_argument("--spec", help=SPEC_HELP)
849857
add_python_options(p)
850858
add_pip_venv_args(p)

tests/test_run.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,3 +443,10 @@ def test_run_local_path_entry_point(pipx_temp_env, caplog, root):
443443
run_pipx_cli_exit(["run", empty_project_path])
444444

445445
assert "Using discovered entry point for 'pipx run'" in caplog.text
446+
447+
448+
@mock.patch("os.execvpe", new=execvpe_mock)
449+
def test_run_with(pipx_temp_env, capsys):
450+
run_pipx_cli_exit(["run", "--with", "black", "pycowsay", "--help"])
451+
captured = capsys.readouterr()
452+
assert "injected package black into venv pycowsay" in captured.out

0 commit comments

Comments
 (0)