The Wayback Machine - https://web.archive.org/web/20221003052022/https://github.com/pythonnet/pythonnet/issues/1348
Skip to content
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

Setting Virtual Environment while Embedding Python in C# #1348

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

Setting Virtual Environment while Embedding Python in C# #1348

shv07 opened this issue Jan 8, 2021 · 3 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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants