Skip to content

Commit 651c337

Browse files
committed
feat(python): use findpython
1 parent 603646c commit 651c337

File tree

11 files changed

+504
-304
lines changed

11 files changed

+504
-304
lines changed

poetry.lock

Lines changed: 16 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ dependencies = [
2727
"trove-classifiers (>=2022.5.19)",
2828
"virtualenv (>=20.26.6,<21.0.0)",
2929
"xattr (>=1.0.0,<2.0.0) ; sys_platform == 'darwin'",
30+
"findpython (>=0.6.2,<0.7.0)",
3031
]
3132
authors = [
3233
{ name = "Sébastien Eustace", email = "[email protected]" }
@@ -182,6 +183,7 @@ warn_unused_ignores = false
182183
module = [
183184
'deepdiff.*',
184185
'fastjsonschema.*',
186+
'findpython.*',
185187
'httpretty.*',
186188
'requests_toolbelt.*',
187189
'shellingham.*',

src/poetry/utils/env/env_manager.py

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -114,17 +114,8 @@ def base_env_name(self) -> str:
114114
def activate(self, python: str) -> Env:
115115
venv_path = self._poetry.config.virtualenvs_path
116116

117-
try:
118-
python_version = Version.parse(python)
119-
python = f"python{python_version.major}"
120-
if python_version.precision > 1:
121-
python += f".{python_version.minor}"
122-
except ValueError:
123-
# Executable in PATH or full executable path
124-
pass
125-
126-
python_ = Python.get_by_name(python)
127-
if python_ is None:
117+
python_instance = Python.get_by_name(python)
118+
if python_instance is None:
128119
raise PythonVersionNotFoundError(python)
129120

130121
create = False
@@ -138,10 +129,10 @@ def activate(self, python: str) -> Env:
138129
_venv = VirtualEnv(venv)
139130
current_patch = ".".join(str(v) for v in _venv.version_info[:3])
140131

141-
if python_.patch_version.to_string() != current_patch:
132+
if python_instance.patch_version.to_string() != current_patch:
142133
create = True
143134

144-
self.create_venv(executable=python_.executable, force=create)
135+
self.create_venv(python=python_instance, force=create)
145136

146137
return self.get(reload=True)
147138

@@ -154,14 +145,16 @@ def activate(self, python: str) -> Env:
154145
current_patch = current_env["patch"]
155146

156147
if (
157-
current_minor == python_.minor_version.to_string()
158-
and current_patch != python_.patch_version.to_string()
148+
current_minor == python_instance.minor_version.to_string()
149+
and current_patch != python_instance.patch_version.to_string()
159150
):
160151
# We need to recreate
161152
create = True
162153

163-
name = f"{self.base_env_name}-py{python_.minor_version.to_string()}"
164-
venv = venv_path / name
154+
venv = (
155+
venv_path
156+
/ f"{self.base_env_name}-py{python_instance.minor_version.to_string()}"
157+
)
165158

166159
# Create if needed
167160
if not venv.exists() or create:
@@ -174,15 +167,15 @@ def activate(self, python: str) -> Env:
174167
_venv = VirtualEnv(venv)
175168
current_patch = ".".join(str(v) for v in _venv.version_info[:3])
176169

177-
if python_.patch_version.to_string() != current_patch:
170+
if python_instance.patch_version.to_string() != current_patch:
178171
create = True
179172

180-
self.create_venv(executable=python_.executable, force=create)
173+
self.create_venv(python=python_instance, force=create)
181174

182175
# Activate
183176
envs[self.base_env_name] = {
184-
"minor": python_.minor_version.to_string(),
185-
"patch": python_.patch_version.to_string(),
177+
"minor": python_instance.minor_version.to_string(),
178+
"patch": python_instance.patch_version.to_string(),
186179
}
187180
self.envs_file.write(envs)
188181

@@ -203,8 +196,7 @@ def get(self, reload: bool = False) -> Env:
203196
if self._env is not None and not reload:
204197
return self._env
205198

206-
python = Python.get_preferred_python(config=self._poetry.config, io=self._io)
207-
python_minor = python.minor_version.to_string()
199+
python_minor: str | None = None
208200

209201
env = None
210202
envs = None
@@ -237,6 +229,13 @@ def get(self, reload: bool = False) -> Env:
237229

238230
venv_path = self._poetry.config.virtualenvs_path
239231

232+
if python_minor is None:
233+
# we only need to discover python version in this case
234+
python = Python.get_preferred_python(
235+
config=self._poetry.config, io=self._io
236+
)
237+
python_minor = python.minor_version.to_string()
238+
240239
name = f"{self.base_env_name}-py{python_minor.strip()}"
241240

242241
venv = venv_path / name
@@ -372,7 +371,7 @@ def in_project_venv_exists(self) -> bool:
372371
def create_venv(
373372
self,
374373
name: str | None = None,
375-
executable: Path | None = None,
374+
python: Python | None = None,
376375
force: bool = False,
377376
) -> Env:
378377
if self._env is not None and not force:
@@ -400,11 +399,11 @@ def create_venv(
400399
use_poetry_python = self._poetry.config.get("virtualenvs.use-poetry-python")
401400
venv_prompt = self._poetry.config.get("virtualenvs.prompt")
402401

403-
python = (
404-
Python(executable)
405-
if executable
406-
else Python.get_preferred_python(config=self._poetry.config, io=self._io)
407-
)
402+
specific_python_requested = python is not None
403+
if not python:
404+
python = Python.get_preferred_python(
405+
config=self._poetry.config, io=self._io
406+
)
408407

409408
venv_path = (
410409
self.in_project_venv
@@ -422,7 +421,7 @@ def create_venv(
422421
# If an executable has been specified, we stop there
423422
# and notify the user of the incompatibility.
424423
# Otherwise, we try to find a compatible Python version.
425-
if executable and use_poetry_python:
424+
if specific_python_requested and use_poetry_python:
426425
raise NoCompatiblePythonVersionFoundError(
427426
self._poetry.package.python_versions,
428427
python.patch_version.to_string(),

0 commit comments

Comments
 (0)