-
Notifications
You must be signed in to change notification settings - Fork 750
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
Comments
you saved my day. thx |
Starting with Python.NET 3.0 you will also need to set |
thnx for the heads up! |
How I got it working *Python 3.8
So I am adding three parts on to the end of the existing python path
Output
Then it goes on to run these successfully
A bit silly, but note in your venv you obviously need to have installed numpy into your venv first. |
This comment was marked as off-topic.
This comment was marked as off-topic.
I found, to use a
Notice
Not sure why yet. |
Just moving the PythonEngine.Initialize(); before PythonEngine worked for me, thanks. |
If anyone has some nice examples (this repo lacks them) would be great to see them./ |
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. |
@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. |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
Uh oh!
There was an error while loading. Please reload this page.
Environment
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?
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)
The text was updated successfully, but these errors were encountered: