1212from importlib import import_module
1313from shutil import which
1414from string import printable
15- from subprocess import PIPE , Popen
15+ from subprocess import PIPE , Popen , check_call
1616
1717from lib .logger import log_setup
1818from lib .system import Cuda , Packages , ROCm , System
@@ -177,11 +177,17 @@ def _output_runtime_info(self) -> None:
177177
178178 def _check_pip (self ) -> None :
179179 """ Check installed pip version """
180- try :
181- _pip = T .cast ("pip" , import_module ("pip" )) # type:ignore[valid-type]
182- except ModuleNotFoundError :
183- logger .error ("Import pip failed. Please Install python3-pip and try again" )
184- sys .exit (1 )
180+ for i in range (2 ):
181+ try :
182+ _pip = T .cast ("pip" , import_module ("pip" )) # type:ignore[valid-type]
183+ break
184+ except ModuleNotFoundError :
185+ if i == 0 :
186+ logger .info ("Installing pip..." )
187+ check_call ([sys .executable , "-m" , "ensurepip" , "--default-pip" ])
188+ continue
189+ logger .error ("Import pip failed. Please Install python3-pip and try again" )
190+ sys .exit (1 )
185191 logger .info ("Pip version: %s" , _pip .__version__ ) # type:ignore[attr-defined]
186192
187193 def _configure_keras (self ) -> None :
@@ -211,8 +217,8 @@ def _configure_keras(self) -> None:
211217 def set_config (self ) -> None :
212218 """ Set the backend in the faceswap config file """
213219 config = {"backend" : self .backend }
214- pypath = os .path .dirname (os .path .realpath (__file__ ))
215- config_file = os .path .join (pypath , "config" , ".faceswap" )
220+ py_path = os .path .dirname (os .path .realpath (__file__ ))
221+ config_file = os .path .join (py_path , "config" , ".faceswap" )
216222 with open (config_file , "w" , encoding = "utf8" ) as cnf :
217223 json .dump (config , cnf )
218224 logger .info ("Faceswap config written to: %s" , config_file )
@@ -347,7 +353,7 @@ def _get_missing_conda(self) -> dict[str, list[dict[T.Literal["name", "package"]
347353 # Ref: https://github.com/ContinuumIO/anaconda-issues/issues/6833
348354 # This versioning will fail in parse_requirements, so we need to do it here
349355 package ["package" ] = f"{ req .name } =*=xft_*" # Swap out for explicit XFT version
350- if exists is not None and not exists [1 ].startswith ("xft" ): # Replace noxft version
356+ if exists is not None and not exists [1 ].startswith ("xft" ): # Replace no-xft vers
351357 exists = None
352358 if not exists :
353359 logger .debug ("Adding new Conda package '%s'" , package ["package" ])
@@ -856,16 +862,16 @@ def _from_pip(self,
856862 extra_args : list[str] | None, optional
857863 Any extra arguments to provide to pip. Default: ``None`` (no extra arguments)
858864 """
859- pipexe = [sys .executable ,
860- "-u" , "-m" , "pip" , "install" , "--no-cache-dir" , "--progress-bar=raw" ]
865+ pip_exe = [sys .executable ,
866+ "-u" , "-m" , "pip" , "install" , "--no-cache-dir" , "--progress-bar=raw" ]
861867
862868 if not self ._env .system .is_admin and not self ._env .system .is_virtual_env :
863- pipexe .append ("--user" ) # install as user to solve perm restriction
869+ pip_exe .append ("--user" ) # install as user to solve perm restriction
864870 if extra_args is not None :
865- pipexe .extend (extra_args )
866- pipexe .extend ([p ["package" ] for p in packages ])
871+ pip_exe .extend (extra_args )
872+ pip_exe .extend ([p ["package" ] for p in packages ])
867873 names = [p ["name" ] for p in packages ]
868- installer = Installer (self ._env , names , pipexe , False , self ._is_gui )
874+ installer = Installer (self ._env , names , pip_exe , False , self ._is_gui )
869875 if installer () != 0 :
870876 msg = f"Unable to install Python packages: { ', ' .join (names )} "
871877 logger .warning ("%s. Please install these packages manually" , msg )
@@ -888,16 +894,16 @@ def _from_conda(self,
888894 Returns
889895 -------
890896 bool
891- ``True`` if the package was succesfully installed otherwise ``False``
897+ ``True`` if the package was successfully installed otherwise ``False``
892898 """
893899 conda = which ("conda" )
894900 assert conda is not None
895- condaexe = [conda , "install" , "-y" , "-c" , channel ,
896- "--override-channels" , "--strict-channel-priority" ]
897- condaexe += [p ["package" ] for p in packages ]
901+ conda_exe = [conda , "install" , "-y" , "-c" , channel ,
902+ "--override-channels" , "--strict-channel-priority" ]
903+ conda_exe += [p ["package" ] for p in packages ]
898904 names = [p ["name" ] for p in packages ]
899- retcode = Installer (self ._env , names , condaexe , True , self ._is_gui )()
900- if retcode != 0 :
905+ ret_code = Installer (self ._env , names , conda_exe , True , self ._is_gui )()
906+ if ret_code != 0 :
901907 logger .warning ("Unable to install Conda packages: %s. "
902908 "Please install these packages manually" , ', ' .join (names ))
903909 _InstallState .failed = True
0 commit comments