From 1c1b92326e6ec9de1d12ab5614c9b87a4c8a7417 Mon Sep 17 00:00:00 2001 From: Ken000666 Date: Fri, 29 Mar 2024 18:13:07 +0800 Subject: [PATCH] fix: Can't auto load the env in some cases --- .../Configuration/ConfigurationService.cs | 35 ++++++++++++++++--- .../Environment/AddEnvironmentViewModel.cs | 4 +-- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/PipManager/Services/Configuration/ConfigurationService.cs b/src/PipManager/Services/Configuration/ConfigurationService.cs index 3748977..bcd9d23 100644 --- a/src/PipManager/Services/Configuration/ConfigurationService.cs +++ b/src/PipManager/Services/Configuration/ConfigurationService.cs @@ -48,14 +48,41 @@ public string FindPythonPathByPipDir(string pipDir) [GeneratedRegex("__version__ = \"(.*?)\"", RegexOptions.IgnoreCase, "zh-CN")] private static partial Regex GetPipVersionInInitFile(); + /// + /// Get the pip directory + /// + /// Python path + /// Pip directory if exists, otherwise null + private string? GetPipDirectories(string pythonDirectory) + { + var sitePackageDirectory = Path.Combine(pythonDirectory, @"Lib\site-packages"); + if (!Directory.Exists(sitePackageDirectory)) + { + return null; + } + + var pipDirectory = Path.Combine(sitePackageDirectory, "pip"); + if (!Directory.Exists(pipDirectory)) + { + return null; + } + + return pipDirectory; + } + public EnvironmentItem? GetEnvironmentItem(string pythonPath) { var pythonVersion = FileVersionInfo.GetVersionInfo(pythonPath).FileVersion!; var pythonDirectory = Directory.GetParent(pythonPath)!.FullName; - var pipDirectory = Path.Combine(pythonDirectory, @"Lib\site-packages"); - var pipDir = Directory.GetDirectories(pipDirectory, "pip")[0]; - var pipVersion = GetPipVersionInInitFile().Match(File.ReadAllText(Path.Combine(pipDir, @"__init__.py"))).Groups[1].Value; - return pipDir.Length > 0 ? new EnvironmentItem(pipVersion, pythonPath, pythonVersion) : null; + var pipDirectory = GetPipDirectories(pythonDirectory); + + if (pipDirectory == null) + { + return null; + } + + var pipVersion = GetPipVersionInInitFile().Match(File.ReadAllText(Path.Combine(pipDirectory, @"__init__.py"))).Groups[1].Value; + return new EnvironmentItem(pipVersion, pythonPath, pythonVersion); } public EnvironmentItem? GetEnvironmentItemFromCommand(string command, string arguments) diff --git a/src/PipManager/ViewModels/Pages/Environment/AddEnvironmentViewModel.cs b/src/PipManager/ViewModels/Pages/Environment/AddEnvironmentViewModel.cs index f6fb88d..d968b98 100644 --- a/src/PipManager/ViewModels/Pages/Environment/AddEnvironmentViewModel.cs +++ b/src/PipManager/ViewModels/Pages/Environment/AddEnvironmentViewModel.cs @@ -74,8 +74,8 @@ await Task.Run(() => var value = System.Environment.GetEnvironmentVariable("Path")!.Split(';'); foreach (var item in value) { - if (!item.Contains("Python") || item.Contains("Scripts") || - !File.Exists(Path.Combine(item, "python.exe"))) continue; + if (!File.Exists(Path.Combine(item, "python.exe"))) + continue; var environmentItem = _configurationService.GetEnvironmentItem(Path.Combine(item, "python.exe")); if (environmentItem == null) continue;