🌐 AI搜索 & 代理 主页
Skip to content

Commit 51d6aa8

Browse files
committed
Derive program name from venv if possible
1 parent 8d8bf26 commit 51d6aa8

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

src/runtime/Runtime.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,42 @@ internal static int GetRun()
9696
return runNumber;
9797
}
9898

99+
static void EnsureProgramName()
100+
{
101+
if (!string.IsNullOrEmpty(PythonEngine.ProgramName))
102+
return;
103+
104+
string fromEnv = Environment.GetEnvironmentVariable("PYTHONNET_PYEXE");
105+
if (!string.IsNullOrEmpty(fromEnv))
106+
{
107+
PythonEngine.ProgramName = fromEnv;
108+
return;
109+
}
110+
111+
string venv = Environment.GetEnvironmentVariable("VIRTUAL_ENV");
112+
if (!string.IsNullOrEmpty(venv))
113+
{
114+
if (IsWindows)
115+
{
116+
var path = Path.Combine(venv, "Scripts", "python.exe");
117+
if (System.IO.File.Exists(path))
118+
{
119+
PythonEngine.ProgramName = path;
120+
return;
121+
}
122+
}
123+
else
124+
{
125+
var path = Path.Combine(venv, "bin", "python");
126+
if (System.IO.File.Exists(path))
127+
{
128+
PythonEngine.ProgramName = path;
129+
return;
130+
}
131+
}
132+
}
133+
}
134+
99135
internal static bool HostedInPython;
100136
internal static bool ProcessIsTerminating;
101137

@@ -117,6 +153,8 @@ internal static void Initialize(bool initSigs = false)
117153
);
118154
if (!interpreterAlreadyInitialized)
119155
{
156+
EnsureProgramName();
157+
120158
Py_InitializeEx(initSigs ? 1 : 0);
121159

122160
NewRun();

0 commit comments

Comments
 (0)