Skip to content

Setting Virtual Environment while Embedding Python in C# #1348

Closed
@shv07

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);

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