Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Setting Virtual Environment while Embedding Python in C##2334

Unanswered
shv07 asked this question inQ&A
Discussion options

Environment

  • Pythonnet version: Latest as on 7 Jan 2021 cloned from github official repo
  • Python version: 3.7.8
  • Operating System: Windows 10
  • .NET Runtime: .Net Core 3.1

Details

P.S. - I was able to solve this issue, however since I didn't find any answer responding to this aspect anywhere, thought it might help the community.

  • I was trying to run some python codes from my dotnet core project by setting up a local python 3.7 virtual environment created using venv. I setup the compile-time constants in project properties accordingly and followed the steps mentionedhere to use my virtual environment. I was trying to import numpy which was already installed in the venv, but every time I was getting a missing basic python library error (like codec etc).

  • On further inspection I found that the virtual environment directory does not have these "basic" python libraries. These libraries are present in the parent python 3.7 directory (the python which was used to create the venv itself). And since the path to the parent library is alerady present in the PythonPath, they are referred from there when you run python in CMD.

  • But as mentioned in your documentation, instead of appending new values to PYTHONPATH, you are changing it completely and pointing it towards just the venv directory which does not have all the python files required.

  • I was finally able to run python and import the modules in venv after appending the venv path to original PYTHONPATH, instead of assigning it directly.

  • To summarise, the PythonEngine.PythonPath should have the </path/to/Lib/>, </path/to/Lib/SitePackages/> of the virtual environment python directory and also of the parent python used to create the virtual env. (Another approach could be to check the paths insys.path in the python virtual environment and ensure those values are present here too.)

  • What commands did I run to trigger this issue?

stringpathToVirtualEnv=/path/to/venv/;Environment.SetEnvironmentVariable("PATH",pathToVirtualEnv,EnvironmentVariableTarget.Process);Environment.SetEnvironmentVariable("PYTHONHOME",pathToVirtualEnv,EnvironmentVariableTarget.Process);Environment.SetEnvironmentVariable("PYTHONPATH",$"{pathToVirtualEnv}\\Lib\\site-packages;{pathToVirtualEnv}\\Lib",EnvironmentVariableTarget.Process);PythonEngine.PythonHome=pathToVirtualEnv;PythonEngine.PythonPath=Environment.GetEnvironmentVariable("PYTHONPATH",EnvironmentVariableTarget.Process);
  • One of the errors which I faced
ImportError :Nomodulenamed'_ctypes'

This change to the above code finally worked for me. Note - my initial PYTHONPATH points to python path corresponding the parent python I used to create the virtual environment (i.e. Python 3.7)

PythonEngine.PythonPath=PythonEngine.PythonPath+";"+Environment.GetEnvironmentVariable("PYTHONPATH",EnvironmentVariableTarget.Process);
You must be logged in to vote

Replies: 10 comments 1 reply

Comment options

you saved my day. thx

You must be logged in to vote
0 replies
Comment options

Starting with Python.NET 3.0 you will also need to setRuntime.PythonDLL to the python dynamic library, otherwise you will getTypeInitializationException.

You must be logged in to vote
0 replies
Comment options

Starting with Python.NET 3.0 you will also need to setRuntime.PythonDLL to the python dynamic library, otherwise you will getTypeInitializationException.

thnx for the heads up!

You must be logged in to vote
0 replies
Comment options

How I got it working

*Python 3.8
*VS2022
*Target framework 4.7.2
*Platform Target x64 (note you must change this, you can't leave it on ANYCPU)
*Pythonnet 3.0.1 (via nuget)

    private static void setup_py_venv_003()    {        Runtime.PythonDLL = @"C:\Python38\python38.dll";        var pathToVirtualEnv = Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), @"..\..\..\venv"));        Console.WriteLine(pathToVirtualEnv);        Console.WriteLine(Runtime.PythonDLL);        Console.WriteLine(PythonEngine.Platform);        Console.WriteLine(PythonEngine.MinSupportedVersion);        Console.WriteLine(PythonEngine.MaxSupportedVersion);        Console.WriteLine(PythonEngine.BuildInfo);        Console.WriteLine(PythonEngine.PythonPath);        string additional = $"{pathToVirtualEnv};{pathToVirtualEnv}\\Lib\\site-packages;{pathToVirtualEnv}\\Lib";        PythonEngine.PythonPath = PythonEngine.PythonPath + ";" + additional;        Console.WriteLine(PythonEngine.PythonPath);        PythonEngine.Initialize();        PythonEngine.BeginAllowThreads();    }

So I am adding three parts on to the end of the existing python path

  1. Root {pathToVirtualEnv}
  2. Site Packages {pathToVirtualEnv}\Lib\site-packages
  3. Lib folder {pathToVirtualEnv}\Lib

Output

C:\Users\sean\_CODE\_SMALL\PythonCore\venv----------C:\Python38\python38.dllwin324.74.11.2147483647.2147483647tags/v3.8.0:fa919fd, Oct 14 2019, 19:37:50C:\Python38\python38.zip;C:\Python38\Lib\;C:\Python38\DLLs\;C:\Users\sean\_CODE\_SMALL\PythonCore\ConsoleCshrp\bin\Debug----------C:\Python38\python38.zip;C:\Python38\Lib\;C:\Python38\DLLs\;C:\Users\sean\_CODE\_SMALL\PythonCore\ConsoleCshrp\bin\Debug;C:\Users\sean\_CODE\_SMALL\PythonCore\venv;C:\Users\sean\_CODE\_SMALL\PythonCore\venv\Lib\site-packages;C:\Users\sean\_CODE\_SMALL\PythonCore\venv\Lib----------

Then it goes on to run these successfully

 private static void run_some_tests()    {        using (Py.GIL())        {            dynamic np = Py.Import("numpy");            Console.WriteLine(np.cos(np.pi * 2));            dynamic sin = np.sin;            Console.WriteLine(sin(5));            double c = (double)(np.cos(5) + sin(5));            Console.WriteLine(c);            dynamic a = np.array(new List<float> { 1, 2, 3 });            Console.WriteLine(a.dtype);            dynamic b = np.array(new List<float> { 6, 5, 4 }, dtype: np.int32);            Console.WriteLine(b.dtype);            Console.WriteLine(a * b);            Console.ReadKey();        }    }

A bit silly, but note in your venv you obviously need to have installed numpy into your venv first.

You must be logged in to vote
0 replies

This comment has been minimized.

Comment options

I found, to use avenv, what finally worked for me, was a modified version ofthe documentation:

Runtime.PythonDLL = @"C:\Users\<username>\AppData\Local\Programs\Python\Python310\python310.dll";var pathToVirtualEnv = @"path\to\env";// be sure not to overwrite your existing "PATH" environmental variable.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.Initialize();PythonEngine.PythonHome = pathToVirtualEnv;PythonEngine.PythonPath = Environment.GetEnvironmentVariable("PYTHONPATH", EnvironmentVariableTarget.Process);

Notice

  1. Comment out:// Environment.SetEnvironmentVariable("PYTHONHOME", pathToVirtualEnv, EnvironmentVariableTarget.Process);
  2. Then call:PythonEngine.Initialize(); beforePythonEngine

Not sure why yet.

You must be logged in to vote
1 reply
@boxibi24
Comment options

I found, to use avenv, what finally worked for me, was a modified version ofthe documentation:

Runtime.PythonDLL = @"C:\Users\<username>\AppData\Local\Programs\Python\Python310\python310.dll";var pathToVirtualEnv = @"path\to\env";// be sure not to overwrite your existing "PATH" environmental variable.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.Initialize();PythonEngine.PythonHome = pathToVirtualEnv;PythonEngine.PythonPath = Environment.GetEnvironmentVariable("PYTHONPATH", EnvironmentVariableTarget.Process);

Notice

  1. Comment out:// Environment.SetEnvironmentVariable("PYTHONHOME", pathToVirtualEnv, EnvironmentVariableTarget.Process);
  2. Then call:PythonEngine.Initialize(); beforePythonEngine

Not sure why yet.

Took me a whole day to get my VirtualEnvironment working by following the exact steps above (inNotice).
In my case, if I either miss step 1 or 2 the Unity app will freeze upon executingPythonEngine.Initialize().
FYI: I'm usingpythonnet 3.0.5 in Unity 6.2 on my Windows machine.

Comment options

I found, to use avenv, what finally worked for me, was a modified version ofthe documentation:

Runtime.PythonDLL = @"C:\Users\<username>\AppData\Local\Programs\Python\Python310\python310.dll";var pathToVirtualEnv = @"path\to\env";// be sure not to overwrite your existing "PATH" environmental variable.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.Initialize();PythonEngine.PythonHome = pathToVirtualEnv;PythonEngine.PythonPath = Environment.GetEnvironmentVariable("PYTHONPATH", EnvironmentVariableTarget.Process);

Notice

  1. Comment out:// Environment.SetEnvironmentVariable("PYTHONHOME", pathToVirtualEnv, EnvironmentVariableTarget.Process);
  2. Then call:PythonEngine.Initialize(); beforePythonEngine

Not sure why yet.

Just moving the PythonEngine.Initialize(); before PythonEngine worked for me, thanks.

You must be logged in to vote
0 replies
Comment options

If anyone has some nice examples (this repo lacks them) would be great to see them./

You must be logged in to vote
0 replies
Comment options

On Windows the provided example fromhttps://github.com/pythonnet/pythonnet/wiki/Using-Python.NET-with-Virtual-Environments worked just fine (no need to call PythonEngine.Initialize(); before setting PythonHome and PythonPath). However, I can not get this working on Linux (Ubuntu 20.04). If someone has any advices, please add a comment below. Thanks.

You must be logged in to vote
0 replies
Comment options

@helldanno Instead of ";" and "\" on Windows, I think Linux uses ":" and "/".

Maybe change code to use System.IO.Path.DirectorySeparatorChar instead of "\" backslash

For environment variable delimiter find it with below. There might be a better way.
string environmentVarDelimiter = (Environment.OSVersion.Platform == PlatformID.Win32NT) ? ";" : ":";

You must be logged in to vote
0 replies
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
11 participants
@shv07@lostmsu@tracktownsoftware@mgamache@calebd-anderson@screig@danp-hh@bukowa@vks2@boxibi24@cwt2021
Converted from issue

This discussion was converted from issue #1348 on February 29, 2024 08:04.


[8]ページ先頭

©2009-2025 Movatter.jp