Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --executable option to env info command #7547

Merged
merged 2 commits into from
Apr 17, 2023
Merged
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
11 changes: 11 additions & 0 deletions docs/managing-environments.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pyenv install 3.9.8
pyenv local 3.9.8 # Activate Python 3.9 for the current project
poetry install
```

{{% /note %}}

{{% note %}}
Expand Down Expand Up @@ -106,6 +107,13 @@ to `env info`:
poetry env info --path
```

If you only want to know the path to the python executable (useful for running mypy from a global environment without installing it in the virtual environment), you can pass the `--executable` option
to `env info`:

```bash
poetry env info --executable
```

## Listing the environments associated with the project

You can also list all the virtual environments associated with the current project
Expand Down Expand Up @@ -140,10 +148,13 @@ poetry env remove test-O3eWbxRl-py3.7
```

You can delete more than one environment at a time.

```bash
poetry env remove python3.6 python3.7 python3.8
```

Use the `--all` option to delete all virtual environments at once.

```bash
poetry env remove --all
```
Expand Down
15 changes: 14 additions & 1 deletion src/poetry/console/commands/env/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ class EnvInfoCommand(Command):
name = "env info"
description = "Displays information about the current environment."

options = [option("path", "p", "Only display the environment's path.")]
options = [
option("path", "p", "Only display the environment's path."),
option(
"executable", "e", "Only display the environment's python executable path."
),
]

def handle(self) -> int:
from poetry.utils.env import EnvManager
Expand All @@ -30,6 +35,14 @@ def handle(self) -> int:

return 0

if self.option("executable"):
if not env.is_venv():
return 1

self.line(str(env.python))

return 0

self._display_complete_info(env)
return 0

Expand Down
6 changes: 6 additions & 0 deletions tests/console/commands/env/test_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,9 @@ def test_env_info_displays_path_only(tester: CommandTester):
tester.execute("--path")
expected = str(Path("/prefix")) + "\n"
assert tester.io.fetch_output() == expected


def test_env_info_displays_executable_only(tester: CommandTester):
tester.execute("--executable")
expected = str(sys.executable) + "\n"
assert tester.io.fetch_output() == expected