Skip to content

Commit 5ef87d9

Browse files
committed
add option virtualenvs.prefer-shell-python
1 parent fd6cea8 commit 5ef87d9

File tree

4 files changed

+40
-18
lines changed

4 files changed

+40
-18
lines changed

src/poetry/config/config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class Config:
3838
"in-project": None,
3939
"path": os.path.join("{cache-dir}", "virtualenvs"),
4040
"options": {"always-copy": False, "system-site-packages": False},
41+
"prefer-shell-python": False,
4142
},
4243
"experimental": {"new-installer": True},
4344
"installer": {"parallel": True, "max-workers": None},
@@ -138,6 +139,7 @@ def _get_normalizer(name: str) -> Callable:
138139
"virtualenvs.in-project",
139140
"virtualenvs.options.always-copy",
140141
"virtualenvs.options.system-site-packages",
142+
"virtualenvs.options.prefer-shell-python",
141143
"experimental.new-installer",
142144
"installer.parallel",
143145
}:

src/poetry/console/application.py

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import contextlib
21
import logging
32
import re
43
import subprocess
@@ -19,6 +18,7 @@
1918
from cleo.exceptions import CleoException
2019
from cleo.formatters.style import Style
2120
from cleo.io.inputs.argv_input import ArgvInput
21+
from cleo.io.outputs.output import Verbosity
2222
from poetry.core.utils._compat import PY37
2323

2424
from poetry.__version__ import __version__
@@ -229,6 +229,30 @@ def _configure_io(self, io: "IO") -> None:
229229

230230
return super()._configure_io(io)
231231

232+
def _detect_active_python(self, io: "IO") -> str:
233+
executable = None
234+
235+
try:
236+
io.write_line(
237+
"Trying to detect current active python executable as specified in the config.",
238+
verbosity=Verbosity.VERBOSE,
239+
)
240+
executable = decode(
241+
subprocess.check_output(
242+
list_to_shell_command(
243+
["python", "-c", '"import sys; print(sys.executable)"']
244+
),
245+
shell=True,
246+
).strip()
247+
)
248+
io.write_line(f"Found: {executable}", verbosity=Verbosity.VERBOSE)
249+
except CalledProcessError:
250+
io.write_line(
251+
"Unable to detect the current active python executable. Falling back to default.",
252+
verbosity=Verbosity.VERBOSE,
253+
)
254+
return executable
255+
232256
def register_command_loggers(
233257
self, event: "ConsoleCommandEvent", event_name: str, _: Any
234258
) -> None:
@@ -288,27 +312,15 @@ def configure_env(
288312
poetry = command.poetry
289313

290314
executable = None
291-
find_compatible = None
292315

293-
# add on option to trigger this
294-
with contextlib.suppress(CalledProcessError):
295-
executable = decode(
296-
subprocess.check_output(
297-
list_to_shell_command(
298-
[
299-
"python",
300-
"-c",
301-
'"import sys; print(sys.executable)"',
302-
]
303-
),
304-
shell=True,
305-
).strip()
306-
)
307-
find_compatible = True
316+
if poetry.config.get("virtualenvs.prefer-shell-python"):
317+
executable = self._detect_active_python(io)
308318

309319
env_manager = EnvManager(poetry)
310320
env = env_manager.create_venv(
311-
io, executable=executable, find_compatible=find_compatible
321+
io,
322+
executable=executable,
323+
find_compatible=True if executable else None,
312324
)
313325

314326
if env.is_venv() and io.is_verbose():

src/poetry/console/commands/config.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ def unique_config_values(self) -> Dict[str, Tuple[Any, Any, Any]]:
7878
lambda val: str(Path(val)),
7979
str(Path(CACHE_DIR) / "virtualenvs"),
8080
),
81+
"virtualenvs.prefer-shell-python": (
82+
boolean_validator,
83+
boolean_normalizer,
84+
False,
85+
),
8186
"experimental.new-installer": (
8287
boolean_validator,
8388
boolean_normalizer,

tests/console/commands/test_config.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ def test_list_displays_default_value_if_not_set(
5656
virtualenvs.options.always-copy = false
5757
virtualenvs.options.system-site-packages = false
5858
virtualenvs.path = {venv_path} # {config_cache_dir / 'virtualenvs'}
59+
virtualenvs.prefer-shell-python = false
5960
"""
6061

6162
assert expected == tester.io.fetch_output()
@@ -79,6 +80,7 @@ def test_list_displays_set_get_setting(
7980
virtualenvs.options.always-copy = false
8081
virtualenvs.options.system-site-packages = false
8182
virtualenvs.path = {venv_path} # {config_cache_dir / 'virtualenvs'}
83+
virtualenvs.prefer-shell-python = false
8284
"""
8385

8486
assert config.set_config_source.call_count == 0
@@ -126,6 +128,7 @@ def test_list_displays_set_get_local_setting(
126128
virtualenvs.options.always-copy = false
127129
virtualenvs.options.system-site-packages = false
128130
virtualenvs.path = {venv_path} # {config_cache_dir / 'virtualenvs'}
131+
virtualenvs.prefer-shell-python = false
129132
"""
130133

131134
assert config.set_config_source.call_count == 1

0 commit comments

Comments
 (0)