🌐 AI搜索 & 代理 主页
Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Derive program name from venv if possible
  • Loading branch information
filmor committed Oct 25, 2025
commit 9e6ffba2de96c4ee5e53afcacd6d97bc35f8bf02
42 changes: 41 additions & 1 deletion src/runtime/Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Text;
using System.Threading;
using System.Collections.Generic;
using System.IO;
using Python.Runtime.Native;
using System.Linq;
using static System.FormattableString;
Expand Down Expand Up @@ -33,7 +34,8 @@ public static string? PythonDLL
private static string? GetDefaultDllName()
{
string dll = Environment.GetEnvironmentVariable("PYTHONNET_PYDLL");
if (dll is not null) return dll;
if (!string.IsNullOrEmpty(dll))
return dll;

string verString = Environment.GetEnvironmentVariable("PYTHONNET_PYVER");
if (!Version.TryParse(verString, out var version)) return null;
Expand Down Expand Up @@ -96,6 +98,42 @@ internal static int GetRun()
return runNumber;
}

static void EnsureProgramName()
{
if (!string.IsNullOrEmpty(PythonEngine.ProgramName))
return;

string fromEnv = Environment.GetEnvironmentVariable("PYTHONNET_PYEXE");
if (!string.IsNullOrEmpty(fromEnv))
{
PythonEngine.ProgramName = fromEnv;
return;
}

string venv = Environment.GetEnvironmentVariable("VIRTUAL_ENV");
if (!string.IsNullOrEmpty(venv))
{
if (IsWindows)
{
var path = Path.Combine(venv, "Scripts", "python.exe");
if (System.IO.File.Exists(path))
{
PythonEngine.ProgramName = path;
return;
}
}
else
{
var path = Path.Combine(venv, "bin", "python");
if (System.IO.File.Exists(path))
{
PythonEngine.ProgramName = path;
return;
}
}
}
}

internal static bool HostedInPython;
internal static bool ProcessIsTerminating;

Expand All @@ -117,6 +155,8 @@ internal static void Initialize(bool initSigs = false)
);
if (!interpreterAlreadyInitialized)
{
EnsureProgramName();

Py_InitializeEx(initSigs ? 1 : 0);

NewRun();
Expand Down
Loading