55import shutil
66import subprocess
77import sys
8+ from pathlib import Path
89from types import ModuleType
910from typing import Any
1011
@@ -153,7 +154,7 @@ def get_installed_models() -> list[str]:
153154
154155def _get_am_spacy_venv_python () -> str :
155156 if is_win :
156- return os .path .join (_get_am_spacy_venv_path (), "Scripts" , "python" , " .exe" )
157+ return os .path .join (_get_am_spacy_venv_path (), "Scripts" , "python.exe" )
157158 return os .path .join (_get_am_spacy_venv_path (), "bin" , "python" )
158159
159160
@@ -168,17 +169,21 @@ def create_spacy_venv() -> None:
168169 """
169170
170171 spacy_venv_path = _get_am_spacy_venv_path ()
172+
173+ # delete in case it already exists from previously failed attempts
174+ shutil .rmtree (spacy_venv_path , ignore_errors = True )
175+
171176 python_path : str | None = venv_binary ("python" )
172177 assert python_path is not None
173178
174179 subprocess .run ([python_path , "-m" , "venv" , spacy_venv_path ], check = True )
175180
176181 if is_win :
177- spacy_venv_python = os .path .join (spacy_venv_path , "Scripts" , "python" , " .exe" )
182+ spacy_venv_python = os .path .join (spacy_venv_path , "Scripts" , "python.exe" )
178183 else :
179184 spacy_venv_python = os .path .join (spacy_venv_path , "bin" , "python" )
180185
181- # make sure pip, setuptools, and wheel are up to date
186+ # make sure pip, setuptools, and wheel are up-to- date
182187 subprocess .run (
183188 [
184189 spacy_venv_python ,
@@ -201,7 +206,21 @@ def create_spacy_venv() -> None:
201206
202207
203208def delete_spacy_venv () -> None :
204- shutil .rmtree (_get_am_spacy_venv_path ())
209+ spacy_venv_path = _get_am_spacy_venv_path ()
210+ try :
211+ shutil .rmtree (spacy_venv_path )
212+ except PermissionError :
213+ # windows does not like deleting files in use, so we add this flag file
214+ # and we delete the venv on startup if the file exists
215+ (Path (spacy_venv_path ) / ".delete_me" ).touch ()
216+
217+
218+ def maybe_delete_spacy_venv () -> None :
219+ # gracefully delete spacy venv on windows
220+ spacy_venv_path = _get_am_spacy_venv_path ()
221+ flag = Path (spacy_venv_path , ".delete_me" )
222+ if flag .exists ():
223+ shutil .rmtree (spacy_venv_path )
205224
206225
207226def install_model (model_name : str ) -> None :
0 commit comments