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

Commit a5660ea

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

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/runtime/Runtime.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,27 @@ 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+
string exeName = IsWindows ? "python.exe" : "python";
115+
string fullPath = System.IO.Path.Combine(venv, "bin", exeName);
116+
PythonEngine.ProgramName = fullPath;
117+
}
118+
}
119+
99120
internal static bool HostedInPython;
100121
internal static bool ProcessIsTerminating;
101122

@@ -117,6 +138,8 @@ internal static void Initialize(bool initSigs = false)
117138
);
118139
if (!interpreterAlreadyInitialized)
119140
{
141+
EnsureProgramName();
142+
120143
Py_InitializeEx(initSigs ? 1 : 0);
121144

122145
NewRun();

0 commit comments

Comments
 (0)