44import sys
55from typing import Dict , Optional , Tuple
66
7+ import virtualenv
8+
79from devchat .utils import get_logger , get_logging_file
810
911from .envs import MAMBA_BIN_PATH
@@ -66,7 +68,10 @@ def get_dep_hash(reqirements_file: str) -> str:
6668 return hashlib .md5 (content .encode ("utf-8" )).hexdigest ()
6769
6870 def ensure (
69- self , env_name : str , py_version : str , reqirements_file : Optional [str ] = None
71+ self ,
72+ env_name : str ,
73+ py_version : Optional [str ] = None ,
74+ reqirements_file : Optional [str ] = None ,
7075 ) -> Optional [str ]:
7176 """
7277 Ensure the python environment exists with the given name and version.
@@ -82,7 +87,7 @@ def ensure(
8287 # check the version of the python executable
8388 current_version = self .get_py_version (py )
8489
85- if current_version != py_version :
90+ if py_version and current_version != py_version :
8691 should_remove_old = True
8792
8893 if reqirements_file and self .should_reinstall (env_name , reqirements_file ):
@@ -99,8 +104,13 @@ def ensure(
99104 self .remove (env_name )
100105
101106 # create the environment
102- print (f"- Creating { env_name } with { py_version } ..." , flush = True )
103- create_ok , msg = self .create (env_name , py_version )
107+ if py_version :
108+ print (f"- Creating { env_name } with { py_version } ..." , flush = True )
109+ create_ok , msg = self .create (env_name , py_version )
110+ else :
111+ print (f"- Creating { env_name } with current Python version..." , flush = True )
112+ create_ok , msg = self .create_with_virtualenv (env_name )
113+
104114 if not create_ok :
105115 print (f"- Failed to create { env_name } ." , flush = True )
106116 print (f"\n For more details, check { log_file } ." , flush = True )
@@ -139,6 +149,21 @@ def ensure(
139149 print ("\n ```" , flush = True )
140150 return self .get_py (env_name )
141151
152+ def create_with_virtualenv (self , env_name : str ) -> Tuple [bool , str ]:
153+ """
154+ Create a new python environment using virtualenv with the current Python interpreter.
155+ """
156+ env_path = os .path .join (MAMBA_PY_ENVS , env_name )
157+ if os .path .exists (env_path ):
158+ return True , ""
159+
160+ try :
161+ # Use virtualenv.cli_run to create a virtual environment
162+ virtualenv .cli_run ([env_path , "--python" , sys .executable ])
163+ return True , ""
164+ except Exception as e :
165+ return False , str (e )
166+
142167 def install (self , env_name : str , requirements_file : str ) -> Tuple [bool , str ]:
143168 """
144169 Install requirements into the python environment.
@@ -256,7 +281,8 @@ def get_py(self, env_name: str) -> Optional[str]:
256281 env_path = None
257282 if sys .platform == "win32" :
258283 env_path = os .path .join (MAMBA_PY_ENVS , env_name , "python.exe" )
259- # env_path = os.path.join(MAMBA_PY_ENVS, env_name, "Scripts", "python.exe")
284+ if not os .path .exists (env_path ):
285+ env_path = os .path .join (MAMBA_PY_ENVS , env_name , "Scripts" , "python.exe" )
260286 else :
261287 env_path = os .path .join (MAMBA_PY_ENVS , env_name , "bin" , "python" )
262288
0 commit comments