@@ -33,11 +33,14 @@ namespace BH.Engine.Python
3333{
3434 public static partial class Compute
3535 {
36+ [ PreviousVersion ( "8.0" , "BH.Engine.Python.Compute.BasePythonEnvironment(System.Boolean, System.Boolean)" ) ]
3637 [ Description ( "Retrieve or reinstall the base Python Environment for BHoM workflows." ) ]
38+ [ Input ( "version" , "The target version of python to be installed or retrieved." ) ]
3739 [ Input ( "reload" , "Reload the base Python environment rather than recreating it, if it already exists." ) ]
3840 [ Input ( "run" , "Start the installation/retrieval of the BHoM Base Python Environment." ) ]
3941 [ Output ( "env" , "The base Python Environment for all BHoM workflows." ) ]
4042 public static PythonEnvironment BasePythonEnvironment (
43+ PythonVersion version = PythonVersion . v3_10 ,
4144 bool reload = true ,
4245 bool run = false
4346 )
@@ -52,10 +55,10 @@ public static PythonEnvironment BasePythonEnvironment(
5255 // create PythonEnvironments directory if it doesnt already exist
5356 Directory . CreateDirectory ( Query . DirectoryEnvironments ( ) ) ;
5457 }
55-
58+
5659 // determine whether the base environment already exists
57- string targetExecutable = Path . Combine ( Query . DirectoryBaseEnvironment ( ) , "python.exe" ) ;
58- bool exists = Directory . Exists ( Query . DirectoryBaseEnvironment ( ) ) && File . Exists ( targetExecutable ) ;
60+ string targetExecutable = Path . Combine ( Query . DirectoryBaseEnvironment ( version ) , "python.exe" ) ;
61+ bool exists = File . Exists ( targetExecutable ) ;
5962
6063 if ( exists && reload )
6164 return new PythonEnvironment ( ) { Name = Query . ToolkitName ( ) , Executable = targetExecutable } ;
@@ -64,50 +67,14 @@ public static PythonEnvironment BasePythonEnvironment(
6467 // remove all existing environments and kernels
6568 RemoveEverything ( ) ;
6669
67- // download the target Python version and convert into a "full" python installation bypassing admin rights
68- string executable = PythonVersion . v3_10_5 . DownloadPython ( Query . ToolkitName ( ) ) ;
69- string pipInstaller = DownloadGetPip ( Path . GetDirectoryName ( executable ) ) ;
70- string baseEnvironmentDirectory = Path . GetDirectoryName ( executable ) ;
71-
72- // install pip into the python installation
73- Process process = new Process ( )
74- {
75- StartInfo = new ProcessStartInfo ( )
76- {
77- FileName = Modify . AddQuotesIfRequired ( executable ) ,
78- Arguments = Modify . AddQuotesIfRequired ( pipInstaller ) + " --no-warn-script-location" ,
79- RedirectStandardError = true ,
80- UseShellExecute = false ,
81- }
82- } ;
83- using ( Process p = Process . Start ( process . StartInfo ) )
84- {
85- string standardError = p . StandardError . ReadToEnd ( ) ;
86- p . WaitForExit ( ) ;
87- if ( p . ExitCode != 0 )
88- BH . Engine . Base . Compute . RecordError ( $ "Error installing pip.\n { standardError } ") ;
89- File . Delete ( pipInstaller ) ;
90- }
91-
92- // delete files with the suffix ._pth from installedDirectory
93- List < string > pthFiles = Directory . GetFiles ( baseEnvironmentDirectory , "*.*" , SearchOption . TopDirectoryOnly ) . Where ( s => s . EndsWith ( "._pth" ) ) . ToList ( ) ;
94- foreach ( string pthFile in pthFiles )
95- {
96- File . Delete ( pthFile ) ;
97- }
98-
99- // move files with the suffix .dll and .pyd from installedDirectory into a DLLs directory
100- string libDirectory = Directory . CreateDirectory ( Path . Combine ( baseEnvironmentDirectory , "DLLs" ) ) . FullName ;
101- List < string > libFiles = Directory . GetFiles ( baseEnvironmentDirectory , "*.*" , SearchOption . TopDirectoryOnly ) . Where ( s => ( s . EndsWith ( ".dll" ) || s . EndsWith ( ".pyd" ) ) && ! Path . GetFileName ( s ) . Contains ( "python" ) && ! Path . GetFileName ( s ) . Contains ( "vcruntime" ) ) . ToList ( ) ;
102- foreach ( string libFile in libFiles )
103- {
104- File . Move ( libFile , Path . Combine ( libDirectory , Path . GetFileName ( libFile ) ) ) ;
105- }
70+ // download and run the installer for the target Python version
71+ string exe = version . DownloadPythonVersion ( ) ;
10672
10773 // install essential packages into base environment
108- InstallPackages ( executable , new List < string > ( ) { "virtualenv" , "jupyterlab" , "black" , "pylint" } ) ;
74+ InstallPackages ( exe , new List < string > ( ) { "virtualenv" , "jupyterlab" , "black" , "pylint" } ) ;
75+ InstallPackageLocal ( exe , Path . Combine ( Query . DirectoryCode ( ) , Query . ToolkitName ( ) ) ) ;
10976
110- return new PythonEnvironment ( ) { Name = Query . ToolkitName ( ) , Executable = executable } ;
77+ return new PythonEnvironment ( ) { Name = Query . ToolkitName ( ) , Executable = exe } ;
11178 }
11279 }
11380}
0 commit comments