Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Discussion options

Environment

  • Pythonnet version: 3.0.4
  • Python version: 3.11.3
  • Operating System: Linux Mint 21.1
  • .NET Runtime: NET 8

Details

  • I have a C# NET application that works well under Windows but fails with multiple different Python versions under Linux. Pythonnet fails when I try to initialize it with PythonEngine.Initialize()
  • I'm compiling this CLI application in VS2022 under Windows using net8.0
  • Because pythonnet requires libpython3.11.so, I'm compiling Python from source on the target system with the --enable-shared flag. To compile Python 3.11.3 I'm using the following commands:
./configure  --enable-shared
make -s -j2
  • Is there a compilation flag I'm missing?
if (!Linux)
{
    PythonHome = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "python-3.11.3-embed-amd64");
    PythonDLL = "python311.dll";
    PythonPath = $"{PythonHome}/Lib";
    path = PythonHome + ";" + Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Process);
}
else
{
    PythonHome = "/home/user/cli/Python-3.11.3";
    PythonPath = $"{PythonHome}/Lib";
    PythonDLL = $"libpython3.11.so";
    path = PythonHome + ":" + Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Process);

}
Environment.SetEnvironmentVariable("PATH", path, EnvironmentVariableTarget.Process);
Console.WriteLine("PATH = " + Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Process));
Environment.SetEnvironmentVariable("PYTHONHOME", PythonHome, EnvironmentVariableTarget.Process);
Console.WriteLine("PYTHONHOME = " + Environment.GetEnvironmentVariable("PYTHONHOME", EnvironmentVariableTarget.Process));
Environment.SetEnvironmentVariable("PYTHONPATH", PythonPath, EnvironmentVariableTarget.Process);
Console.WriteLine("PYTHONPATH = " + Environment.GetEnvironmentVariable("PYTHONPATH", EnvironmentVariableTarget.Process));
Environment.SetEnvironmentVariable("PYTHONNET_PYDLL", PythonDLL, EnvironmentVariableTarget.Process);
Console.WriteLine("PYTHONNET_PYDLL = " + Environment.GetEnvironmentVariable("PYTHONNET_PYDLL", EnvironmentVariableTarget.Process));

if (Linux)
{
    Runtime.PythonDLL = PythonDLL;
    PythonEngine.PythonHome = PythonHome;
    PythonEngine.PythonPath = PythonPath;

}
Console.WriteLine("Initializing.");
PythonEngine.Initialize();
Console.WriteLine("Allowing threads.");
PythonEngine.BeginAllowThreads();
Console.WriteLine("Initialized.");

No NET exception is thrown. It simply segfaults during initialization.

PATH = /home/user/cli/Python-3.11.3:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
PYTHONHOME = /home/user/cli/Python-3.11.3
PYTHONPATH = /home/user/cli/Python-3.11.3/Lib
PYTHONNET_PYDLL = libpython3.11.so
Initializing.
Segmentation fault (core dumped)
You must be logged in to vote

Replies: 1 comment

Comment options

I found the issue. No matter what I set the PATH, PYTHONHOME, PYTHONPATH, or PYTHONNET_PYDLL variables to during runtime, PythonEngine always tries to initialize it with the system python3 executable. In fact, even after modifying my PATH to point to the directory containing my desired python executable (before executing the C# code), it still defaults to the system python unless I create a symlink.

Is there a way to configure the desired python during runtime?

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
1 participant
Converted from issue

This discussion was converted from issue #2479 on October 08, 2024 03:40.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.