@@ -114,17 +114,8 @@ def base_env_name(self) -> str:
114
114
def activate (self , python : str ) -> Env :
115
115
venv_path = self ._poetry .config .virtualenvs_path
116
116
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 :
128
119
raise PythonVersionNotFoundError (python )
129
120
130
121
create = False
@@ -138,10 +129,10 @@ def activate(self, python: str) -> Env:
138
129
_venv = VirtualEnv (venv )
139
130
current_patch = "." .join (str (v ) for v in _venv .version_info [:3 ])
140
131
141
- if python_ .patch_version .to_string () != current_patch :
132
+ if python_instance .patch_version .to_string () != current_patch :
142
133
create = True
143
134
144
- self .create_venv (executable = python_ . executable , force = create )
135
+ self .create_venv (python = python_instance , force = create )
145
136
146
137
return self .get (reload = True )
147
138
@@ -154,14 +145,16 @@ def activate(self, python: str) -> Env:
154
145
current_patch = current_env ["patch" ]
155
146
156
147
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 ()
159
150
):
160
151
# We need to recreate
161
152
create = True
162
153
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
+ )
165
158
166
159
# Create if needed
167
160
if not venv .exists () or create :
@@ -174,15 +167,15 @@ def activate(self, python: str) -> Env:
174
167
_venv = VirtualEnv (venv )
175
168
current_patch = "." .join (str (v ) for v in _venv .version_info [:3 ])
176
169
177
- if python_ .patch_version .to_string () != current_patch :
170
+ if python_instance .patch_version .to_string () != current_patch :
178
171
create = True
179
172
180
- self .create_venv (executable = python_ . executable , force = create )
173
+ self .create_venv (python = python_instance , force = create )
181
174
182
175
# Activate
183
176
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 (),
186
179
}
187
180
self .envs_file .write (envs )
188
181
@@ -203,8 +196,7 @@ def get(self, reload: bool = False) -> Env:
203
196
if self ._env is not None and not reload :
204
197
return self ._env
205
198
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
208
200
209
201
env = None
210
202
envs = None
@@ -237,6 +229,13 @@ def get(self, reload: bool = False) -> Env:
237
229
238
230
venv_path = self ._poetry .config .virtualenvs_path
239
231
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
+
240
239
name = f"{ self .base_env_name } -py{ python_minor .strip ()} "
241
240
242
241
venv = venv_path / name
@@ -372,7 +371,7 @@ def in_project_venv_exists(self) -> bool:
372
371
def create_venv (
373
372
self ,
374
373
name : str | None = None ,
375
- executable : Path | None = None ,
374
+ python : Python | None = None ,
376
375
force : bool = False ,
377
376
) -> Env :
378
377
if self ._env is not None and not force :
@@ -400,11 +399,11 @@ def create_venv(
400
399
use_poetry_python = self ._poetry .config .get ("virtualenvs.use-poetry-python" )
401
400
venv_prompt = self ._poetry .config .get ("virtualenvs.prompt" )
402
401
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
+ )
408
407
409
408
venv_path = (
410
409
self .in_project_venv
@@ -422,7 +421,7 @@ def create_venv(
422
421
# If an executable has been specified, we stop there
423
422
# and notify the user of the incompatibility.
424
423
# 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 :
426
425
raise NoCompatiblePythonVersionFoundError (
427
426
self ._poetry .package .python_versions ,
428
427
python .patch_version .to_string (),
0 commit comments