Skip to content

Commit

Permalink
Merge pull request #4 from kitman0000/main
Browse files Browse the repository at this point in the history
fix: Can't auto load the env in some cases
  • Loading branch information
AuroraZiling authored Mar 29, 2024
2 parents af625ca + 1c1b923 commit e5959b6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
35 changes: 31 additions & 4 deletions src/PipManager/Services/Configuration/ConfigurationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,41 @@ public string FindPythonPathByPipDir(string pipDir)
[GeneratedRegex("__version__ = \"(.*?)\"", RegexOptions.IgnoreCase, "zh-CN")]
private static partial Regex GetPipVersionInInitFile();

/// <summary>
/// Get the pip directory
/// </summary>
/// <param name="pythonDirectory">Python path</param>
/// <returns>Pip directory if exists, otherwise null</returns>
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit e5959b6

Please sign in to comment.