8000 Setting Virtual Environment while Embedding Python in C# · Issue #1348 · pythonnet/pythonnet · GitHub
[go: up one dir, main page]

Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Setting Virtual Environment while Embedding Python in C# #1348

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
shv07 opened this issue Jan 8, 2021 · 10 comments
Closed

Setting Virtual Environment while Embedding Python in C# #1348

shv07 opened this issue Jan 8, 2021 · 10 comments

Comments

@shv07
Copy link
shv07 commented Jan 8, 2021

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);
@vks2
Copy link
vks2 commented Jan 12, 2021

you saved my day. thx

@lostmsu
Copy link
Member
lostmsu commented Feb 21, 2021

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

@lostmsu lostmsu closed this as completed Feb 21, 2021
@shv07
Copy link
Author
shv07 commented Apr 20, 2021

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!

@screig
Copy link

screig commented Nov 8, 2022

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.

@cwt2021

This comment was marked as off-topic.

@calebd-anderson
Copy link
calebd-anderson commented Feb 10, 2023

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.

@mgamache
Copy link

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.

@bukowa
Copy link
bukowa commented Dec 20, 2023

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

@danp-hh
Copy link
danp-hh commented Feb 14, 2024

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.

@tracktownsoftware
Copy link
tracktownsoftware commented Feb 28, 2024

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

@pythonnet pythonnet locked and limited conversation to collaborators Feb 29, 2024
@filmor filmor converted this issue into discussion #2334 Feb 29, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants
0