Skip to content

Setting Virtual Environment while Embedding Python in C# #1348

Closed

Description

@shv07

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 mentioned here 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 in sys.path in the python virtual environment and ensure those values are present here too.)

  • What commands did I run to trigger this issue?

    string pathToVirtualEnv = /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 : No module named '_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);

Activity

  1. vks2 commented on Jan 12, 2021

    @vks2

    you saved my day. thx

  2. lostmsu commented on Feb 21, 2021

    @lostmsu
    Member

    Starting with Python.NET 3.0 you will also need to set Runtime.PythonDLL to the python dynamic library, otherwise you will get TypeInitializationException.

  3. shv07 commented on Apr 20, 2021

    @shv07
    Author

    Starting with Python.NET 3.0 you will also need to set Runtime.PythonDLL to the python dynamic library, otherwise you will get TypeInitializationException.

    thnx for the heads up!

  4. screig commented on Nov 8, 2022

    @screig

    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.dll
    win32
    4.7
    4.11.2147483647.2147483647
    tags/v3.8.0:fa919fd, Oct 14 2019, 19:37:50
    C:\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.

  5. cwt2021 commented on Jan 13, 2023

    @cwt2021
  6. calebd-anderson commented on Feb 10, 2023

    @calebd-anderson

    I found, to use a venv, what finally worked for me, was a modified version of the 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(); before PythonEngine

    Not sure why yet.

  7. mgamache commented on Dec 18, 2023

    @mgamache

    I found, to use a venv, what finally worked for me, was a modified version of the 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(); before PythonEngine

    Not sure why yet.

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

  8. bukowa commented on Dec 20, 2023

    @bukowa

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

  9. danp-hh commented on Feb 14, 2024

    @danp-hh

    On Windows the provided example from https://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.

  10. tracktownsoftware commented on Feb 28, 2024

    @tracktownsoftware

    @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) ? ";" : ":";

  11. locked and limited conversation to collaborators on Feb 29, 2024
  12. converted this issue into a discussion #2334 on Feb 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions