Issues while trying to use Virtual Environment #2345
Replies: 3 comments 3 replies
-
My understanding is that the python runtime detects when it's running in a venv and thus loads libraries from Try this: Environment.SetEnvironmentVariable("PATH", "/work/venv/bin:" + Environment.GetEnvironmentVariable("PATH"));
Environment.SetEnvironmentVariable("VIRTUAL_ENV", "/work/venv");
// DLL is not copied to the venv folder. You still need to find the DLL in your python installation directory.
// On linux, the library is always copied to /usr/lib/ but the version number can vary by distro, most are just major versioned
Runtime.PythonDLL = "/usr/lib/libpython3.so"; I'm running Dockerfile
Program.cs Environment.SetEnvironmentVariable("PATH", "/work/venv/bin:" + Environment.GetEnvironmentVariable("PATH"));
Environment.SetEnvironmentVariable("VIRTUAL_ENV", "/work/venv");
Runtime.PythonDLL = "/usr/lib/libpython3.so";
PythonEngine.Initialize();
dynamic sys = Py.Import("sys");
dynamic os = Py.Import("os");
Console.WriteLine("Python version: {0}", sys.version);
Console.WriteLine("Current working directory: {0}", os.getcwd());
Console.WriteLine("PythonPath: {0}", PythonEngine.PythonPath); Edit: fixed Runtime.PythonDLL |
Beta Was this translation helpful? Give feedback.
-
hi, var path = $"{Directory.GetCurrentDirectory()}\venv\Scripts\python"; I recieve exceptions: DllNotFoundException: Could not load C:\Documents\Projects\HomingTracker\Git\HomingTracker\HomingTracker\HomingTracker\bin\Debug\net6.0-windows\venv\Scripts\python. Win32Exception: The specified module could not be found. I tried to change path to var path = $"{Directory.GetCurrentDirectory()}\venv\Scripts\python.exe" and receive the followig exceptions; MissingMethodException: Failed to load symbol Py_IncRef. Win32Exception: The specified procedure could not be found. |
Beta Was this translation helpful? Give feedback.
-
Hello, I'm not sure whether it's the same issue, but I also failed to use virtual environment, following the wiki. Here is all of my codes, including venv creation: using Python.Runtime;
using System.Diagnostics;
var pythonDll = new FileInfo(
@"C:/Users/yueyinqiu/AppData/Local/Programs/Python/Python312/python312.dll");
var venv = new DirectoryInfo(@"./venv");
// create venv
Process.Start(
Path.GetFullPath("python", pythonDll.Directory!.FullName),
["-m", "venv", venv.FullName]).WaitForExit();
// set environment variables
var path = Environment.GetEnvironmentVariable("PATH")!.TrimEnd(';');
path = $"{path};{venv.FullName}";
Environment.SetEnvironmentVariable("PATH", path);
Environment.SetEnvironmentVariable("PYTHONHOME", venv.FullName);
var lib = Path.GetFullPath("Lib", venv.FullName);
var sitePackages = Path.GetFullPath("site-packages", lib);
var pythonPath = $"{sitePackages};{lib}";
Environment.SetEnvironmentVariable("PYTHONPATH", pythonPath);
// update PythonDLL & PythonHome & PythonPath
Runtime.PythonDLL = pythonDll.FullName;
PythonEngine.PythonHome = venv.FullName;
PythonEngine.PythonPath = pythonPath;
PythonEngine.Initialize(); It also crashed at
And @bcowdery , thanks for your suggestion. However I could not find the Perhaps I did something wrong when creating the virtual environment? |
Beta Was this translation helpful? Give feedback.
-
Environment
Details
I am trying to define the python to use venv and it crashes.
The code I try to run:
Runtime.PythonDLL = @"C:\Program Files\Python310\python310.dll";
//PythonEngine.PythonHome = "";
string pathToVirtualEnv = Directory.GetCurrentDirectory() + @"\venv";
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\site-packages;{pathToVirtualEnv}\Lib", EnvironmentVariableTarget.Process);
PythonEngine.PythonPath = PythonEngine.PythonPath + ";" + $"{pathToVirtualEnv}\Lib\site-packages;{pathToVirtualEnv}\Lib";
PythonEngine.PythonHome = pathToVirtualEnv;
PythonEngine.Initialize();
it crashes in the last command.
the only way it doesnt crash is if i run :
Runtime.PythonDLL = @"C:\Program Files\Python310\python310.dll";
PythonEngine.PythonHome = "";
PythonEngine.Initialize();
but then I can't define the virtual environment.
I tried several solutions in the web but none worked.
Thanks
Beta Was this translation helpful? Give feedback.
All reactions