Skip to content

Commit 49ed469

Browse files
author
Fraser Greenroyd
authored
Fixed VirtualEnvironmentExists (#128)
2 parents 94dc680 + 7969283 commit 49ed469

2 files changed

Lines changed: 27 additions & 9 deletions

File tree

Python_Engine/Query/Version.cs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ public static partial class Query
3636
[Output("version", "Python version of the requested Python executable.")]
3737
public static PythonVersion Version(string pythonExecutable)
3838
{
39+
if (!File.Exists(pythonExecutable))
40+
{
41+
return PythonVersion.Undefined;
42+
}
43+
3944
Process process = new Process()
4045
{
4146
StartInfo = new ProcessStartInfo()
@@ -47,16 +52,25 @@ public static PythonVersion Version(string pythonExecutable)
4752
UseShellExecute = false,
4853
}
4954
};
50-
string versionString;
51-
using (Process p = Process.Start(process.StartInfo))
55+
56+
try
57+
{
58+
string versionString;
59+
using (Process p = Process.Start(process.StartInfo))
60+
{
61+
p.WaitForExit();
62+
if (p.ExitCode != 0)
63+
BH.Engine.Base.Compute.RecordError($"Error getting Python version.\n{p.StandardError.ReadToEnd()}");
64+
versionString = p.StandardOutput.ReadToEnd().TrimEnd();
65+
}
66+
67+
return (PythonVersion)Enum.Parse(typeof(PythonVersion), "v" + versionString.Replace("Python ", "").Replace(".", "_"));
68+
}
69+
catch
5270
{
53-
p.WaitForExit();
54-
if (p.ExitCode != 0)
55-
BH.Engine.Base.Compute.RecordError($"Error getting Python version.\n{p.StandardError.ReadToEnd()}");
56-
versionString = p.StandardOutput.ReadToEnd().TrimEnd();
71+
return PythonVersion.Undefined;
5772
}
5873

59-
return (PythonVersion) Enum.Parse(typeof(PythonVersion), "v" + versionString.Replace("Python ", "").Replace(".", "_"));
6074
}
6175
}
6276
}

Python_Engine/Query/VirtualEnvironment.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,14 @@ public static partial class Query
4141
[PreviousVersion("7.0", "BH.Engine.Python.Query.VirtualEnvironmentExists(System.String)")]
4242
public static bool VirtualEnvironmentExists(string envName, PythonVersion pythonVersion = PythonVersion.Undefined)
4343
{
44+
string executable = VirtualEnvironmentExecutable(envName);
4445
bool directoryExists = Directory.Exists(VirtualEnvironmentDirectory(envName));
45-
bool executableExists = File.Exists(VirtualEnvironmentExecutable(envName));
46+
bool executableExists = File.Exists(executable);
4647
bool kernelExists = Directory.Exists(VirtualEnvironmentKernel(envName));
47-
bool versionMatches = Version(VirtualEnvironmentExecutable(envName)) == pythonVersion;
48+
bool versionMatches = false;
49+
50+
if (executableExists)
51+
versionMatches = Version(executable) == pythonVersion;
4852

4953
if (pythonVersion == PythonVersion.Undefined)
5054
return directoryExists && executableExists && kernelExists;

0 commit comments

Comments
 (0)