-
Environment
Details
This error occurs in virtualenv and conda, and has not been tested in other ways. (Initialized successfully in windows.)
string winPath = @"D:\tools\python310";
string linuxPath = "/opt/miniconda3/envs/py310";
if(RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
string pathToVirtualEnv = winPath;
Runtime.PythonDLL = Path.Combine(pathToVirtualEnv, "python310.dll");
PythonEngine.PythonHome = Path.Combine(pathToVirtualEnv, "python.exe");
PythonEngine.PythonPath = @$"{pathToVirtualEnv}\Lib\site-packages;{pathToVirtualEnv}\Lib;{pathToVirtualEnv}\DLLs";
}
else if(RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
//Virtualenv
var pathToVirtualEnv = "/opt/miniconda3/envs/py310";
Runtime.PythonDLL = $"{pathToVirtualEnv}/lib/libpython3.10.so";
var path = Environment.GetEnvironmentVariable("PATH").TrimEnd(';');
path = string.IsNullOrEmpty(path) ? pathToVirtualEnv : path + ";" + pathToVirtualEnv;
Environment.SetEnvironmentVariable("PATH", path, EnvironmentVariableTarget.Process);
Environment.SetEnvironmentVariable("PATH", pathToVirtualEnv, EnvironmentVariableTarget.Process);
Environment.SetEnvironmentVariable("PYTHONHOME", pathToVirtualEnv, EnvironmentVariableTarget.Process);
Environment.SetEnvironmentVariable("PYTHONPATH", $"{pathToVirtualEnv}/lib/python310.zip;{pathToVirtualEnv}/lib/python3.10;{pathToVirtualEnv}/lib/python3.10/lib-dynload;{pathToVirtualEnv}/lib/python3.10/site-packages;~/.local/lib/python3.10/site-packages", EnvironmentVariableTarget.Process);
var pythonpath = Environment.GetEnvironmentVariable("PYTHONPATH", EnvironmentVariableTarget.Process);;
Console.WriteLine(pythonpath);
PythonEngine.PythonHome = $"{pathToVirtualEnv}/bin/python";
PythonEngine.PythonPath = pythonpath;
} else {
Console.WriteLine("This OS is not support");
return;
}
PythonEngine.Initialize(); |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
|
The reason for not accepting the answer is that there is a question in the python environment. |
Beta Was this translation helpful? Give feedback.
-
|
I had a similar issue, but a completely different environment, so I'm not sure if it applies. By default, pythonnet works with the .NET Framework environment, not core. Instructions for loading core: In my environment that alone isn't enough, because some of my dependencies could work with .NET 6 or 7, but there was no directive either way. My options were to load the .NET 7 runtuime or add in a reference to the runtimeconfig. You're running out of a .NET 6 application, so I would think the runtimeconfig.json for your app would be used? |
Beta Was this translation helpful? Give feedback.
-
|
Oh my god! |
Beta Was this translation helpful? Give feedback.
Oh my god!
It's a mistake.
We must use ":" instead of ";"