Skip to content
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
1 change: 1 addition & 0 deletions changelog.d/1607.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add `--with` flag to `pipx run` to allow injecting dependencies
1 change: 0 additions & 1 deletion src/pipx/commands/inject.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ def inject_dep(

def inject(
venv_dir: Path,
package_name: Optional[str],
package_specs: Iterable[str],
requirement_files: Iterable[str],
pip_args: List[str],
Expand Down
1 change: 0 additions & 1 deletion src/pipx/commands/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,6 @@ def install_all(
for inject_package in venv_metadata.injected_packages.values():
commands.inject(
venv_dir=venv_dir,
package_name=None,
package_specs=[generate_package_spec(inject_package)],
requirement_files=[],
pip_args=pip_args,
Expand Down
34 changes: 24 additions & 10 deletions src/pipx/commands/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
import urllib.request
from pathlib import Path
from shutil import which
from typing import List, NoReturn, Optional, Union
from typing import List, NoReturn, Optional, Tuple, Union

from packaging.requirements import InvalidRequirement, Requirement

from pipx import paths
from pipx.commands.common import package_name_from_spec
from pipx.commands.inject import inject_dep
from pipx.constants import TEMP_VENV_EXPIRATION_THRESHOLD_DAYS, WINDOWS
from pipx.emojis import hazard
from pipx.util import (
Expand Down Expand Up @@ -43,8 +44,8 @@


def maybe_script_content(app: str, is_path: bool) -> Optional[Union[str, Path]]:
# If the app is a script, return its content.
# Return None if it should be treated as a package name.
"""If the app is a script, return its content.
Return None if it should be treated as a package name."""

# Look for a local file first.
app_path = Path(app)
Expand Down Expand Up @@ -111,6 +112,7 @@ def run_script(
def run_package(
app: str,
package_or_url: str,
dependencies: List[str],
app_args: List[str],
python: str,
pip_args: List[str],
Expand Down Expand Up @@ -157,26 +159,38 @@ def run_package(

if venv.has_app(app, app_filename):
logger.info(f"Reusing cached venv {venv_dir}")
venv.run_app(app, app_filename, app_args)
else:
logger.info(f"venv location is {venv_dir}")
_download_and_run(
venv, app, app_filename = _prepare_venv(
Path(venv_dir),
package_or_url,
app,
app_filename,
app_args,
python,
pip_args,
venv_args,
use_cache,
verbose,
)

for dep in dependencies:
inject_dep(
venv_dir=venv_dir,
package_name=None,
package_spec=dep,
pip_args=pip_args,
verbose=verbose,
include_apps=False,
include_dependencies=False,
force=False,
)
venv.run_app(app, app_filename, app_args)


def run(
app: str,
spec: str,
dependencies: List[str],
is_path: bool,
app_args: List[str],
python: str,
Expand Down Expand Up @@ -206,6 +220,7 @@ def run(
run_package(
package_name,
package_or_url,
dependencies,
app_args,
python,
pip_args,
Expand All @@ -216,18 +231,17 @@ def run(
)


def _download_and_run(
def _prepare_venv(
venv_dir: Path,
package_or_url: str,
app: str,
app_filename: str,
app_args: List[str],
python: str,
pip_args: List[str],
venv_args: List[str],
use_cache: bool,
verbose: bool,
) -> NoReturn:
) -> Tuple[Venv, str, str]:
venv = Venv(venv_dir, python=python, verbose=verbose)
venv.check_upgrade_shared_libs(pip_args=pip_args, verbose=verbose)

Expand Down Expand Up @@ -275,7 +289,7 @@ def _download_and_run(
# Let future _remove_all_expired_venvs know to remove this
(venv_dir / VENV_EXPIRED_FILENAME).touch()

venv.run_app(app, app_filename, app_args)
return venv, app, app_filename


def _get_temporary_venv_path(requirements: List[str], python: str, pip_args: List[str], venv_args: List[str]) -> Path:
Expand Down
9 changes: 8 additions & 1 deletion src/pipx/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ def run_pipx_command(args: argparse.Namespace, subparsers: Dict[str, argparse.Ar
commands.run(
args.app_with_args[0],
args.spec,
args.with_,
args.path,
args.app_with_args[1:],
args.python,
Expand Down Expand Up @@ -310,7 +311,6 @@ def run_pipx_command(args: argparse.Namespace, subparsers: Dict[str, argparse.Ar
elif args.command == "inject":
return commands.inject(
venv_dir,
None,
args.dependencies,
args.requirements,
pip_args,
Expand Down Expand Up @@ -846,6 +846,13 @@ def _add_run(subparsers: argparse._SubParsersAction, shared_parser: argparse.Arg
action="store_true",
help="Require app to be run from local __pypackages__ directory",
)
p.add_argument(
"--with",
dest="with_",
action="append",
default=[],
help="Extra dependencies to add to the temporary environment",
)
p.add_argument("--spec", help=SPEC_HELP)
add_python_options(p)
add_pip_venv_args(p)
Expand Down
21 changes: 21 additions & 0 deletions tests/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,3 +443,24 @@ def test_run_local_path_entry_point(pipx_temp_env, caplog, root):
run_pipx_cli_exit(["run", empty_project_path])

assert "Using discovered entry point for 'pipx run'" in caplog.text


@mock.patch("os.execvpe", new=execvpe_mock)
def test_run_with(capsys):
run_pipx_cli_exit(["run", "--with", "black", "pycowsay", "--help"])
captured = capsys.readouterr()
assert "injected package black into venv pycowsay" in captured.out


@mock.patch("os.execvpe", new=execvpe_mock)
def test_run_with_cache(capsys, caplog):
# Maybe there's a better way to remove the previous venv cache?
run_pipx_cli_exit(["run", "--no-cache", "pycowsay", "cowsay", "args"])
run_pipx_cli_exit(["run", "pycowsay", "cowsay", "args"], assert_exit=0)

caplog.set_level(logging.DEBUG)
caplog.clear()
run_pipx_cli_exit(["run", "--verbose", "--with", "black", "pycowsay", "args"], assert_exit=0)
captured = capsys.readouterr()
assert "Reusing cached venv" in caplog.text
assert "injected package black into venv pycowsay" in captured.out