Skip to content

Commit 7fc00f8

Browse files
committed
Add support for including system site packages
1 parent a9699a4 commit 7fc00f8

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/Bonsai.Scripting.Python/EnvironmentConfig.cs

+7
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public EnvironmentConfig(string pythonHome, string pythonVersion)
1313
Path = pythonHome;
1414
PythonHome = pythonHome;
1515
PythonVersion = pythonVersion;
16+
IncludeSystemSitePackages = true;
1617
}
1718

1819
public string Path { get; private set; }
@@ -21,6 +22,8 @@ public EnvironmentConfig(string pythonHome, string pythonVersion)
2122

2223
public string PythonVersion { get; private set; }
2324

25+
public bool IncludeSystemSitePackages { get; private set; }
26+
2427
public static EnvironmentConfig FromConfigFile(string configFileName)
2528
{
2629
var config = new EnvironmentConfig();
@@ -39,6 +42,10 @@ static string GetConfigValue(string line)
3942
{
4043
config.PythonHome = GetConfigValue(line);
4144
}
45+
else if (line.StartsWith("include-system-site-packages"))
46+
{
47+
config.IncludeSystemSitePackages = bool.Parse(GetConfigValue(line));
48+
}
4249
else if (line.StartsWith("version"))
4350
{
4451
var pythonVersion = GetConfigValue(line);

src/Bonsai.Scripting.Python/EnvironmentHelper.cs

+12
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ public static string GetPythonPath(EnvironmentConfig config)
8080
}
8181

8282
sitePackages = Path.Combine(config.Path, "Lib", "site-packages");
83+
if (config.IncludeSystemSitePackages && config.Path != config.PythonHome)
84+
{
85+
var systemSitePackages = Path.Combine(config.PythonHome, "Lib", "site-packages");
86+
sitePackages = $"{sitePackages}{Path.PathSeparator}{systemSitePackages}";
87+
}
8388
}
8489
else
8590
{
@@ -92,6 +97,13 @@ public static string GetPythonPath(EnvironmentConfig config)
9297
}
9398

9499
sitePackages = Path.Combine(config.Path, "lib", $"python{config.PythonVersion}", "site-packages");
100+
if (config.IncludeSystemSitePackages)
101+
{
102+
var localAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
103+
var localFolder = Directory.GetParent(localAppData).FullName;
104+
var systemSitePackages = Path.Combine(localFolder, "lib", $"python{config.PythonVersion}", "site-packages");
105+
sitePackages = $"{sitePackages}{Path.PathSeparator}{systemSitePackages}";
106+
}
95107
}
96108

97109
return $"{basePath}{Path.PathSeparator}{config.Path}{Path.PathSeparator}{sitePackages}";

0 commit comments

Comments
 (0)