Skip to content

Commit e413273

Browse files
committed
If the virtualenv path exists but the virtualenv python binary does not, delete it
Signed-off-by: Pedro Algarvio <[email protected]>
1 parent e00da9d commit e413273

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

src/ptscripts/virtualenv.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,25 @@ def _create_virtualenv(self) -> None:
158158
# Late import to avoid circular import errors
159159
from ptscripts.__main__ import CWD
160160

161-
if self.venv_dir.exists():
162-
self.ctx.debug("Virtual environment path already exists")
163-
return
161+
if self.venv_dir.exists() and self.venv_python.exists():
162+
if not self.venv_python.exists():
163+
try:
164+
relative_venv_path = self.venv_dir.relative_to(CWD)
165+
except ValueError:
166+
relative_venv_path = self.venv_dir
167+
try:
168+
relative_venv_python_path = self.venv_python.relative_to(CWD)
169+
except ValueError:
170+
relative_venv_python_path = self.venv_python
171+
self.ctx.warn(
172+
f"The virtual environment path '{relative_venv_path}' exists but the "
173+
f"python binary '{relative_venv_python_path}' does not. Deleting the "
174+
"virtual environment."
175+
)
176+
shutil.rmtree(self.venv_dir)
177+
else:
178+
self.ctx.debug("Virtual environment path already exists")
179+
return
164180
virtualenv = shutil.which("virtualenv")
165181
if virtualenv:
166182
cmd = [

0 commit comments

Comments
 (0)