diff --git a/docs/managing-environments.md b/docs/managing-environments.md index 1716a2f9d50..a3a0cc37f57 100644 --- a/docs/managing-environments.md +++ b/docs/managing-environments.md @@ -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 %}} @@ -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 @@ -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 ``` diff --git a/src/poetry/console/commands/env/info.py b/src/poetry/console/commands/env/info.py index abb78f30cda..2de77f73a6d 100644 --- a/src/poetry/console/commands/env/info.py +++ b/src/poetry/console/commands/env/info.py @@ -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 @@ -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 diff --git a/tests/console/commands/env/test_info.py b/tests/console/commands/env/test_info.py index e4f5826e49c..bdc8a0eb57b 100644 --- a/tests/console/commands/env/test_info.py +++ b/tests/console/commands/env/test_info.py @@ -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