Replies: 10 comments 1 reply
-
|
you saved my day. thx |
Beta Was this translation helpful? Give feedback.
-
|
Starting with Python.NET 3.0 you will also need to set |
Beta Was this translation helpful? Give feedback.
-
thnx for the heads up! |
Beta Was this translation helpful? Give feedback.
-
|
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. |
Beta Was this translation helpful? Give feedback.
This comment has been minimized.
This comment has been minimized.
-
|
I found, to use a Notice
Not sure why yet. |
Beta Was this translation helpful? Give feedback.
-
Just moving the PythonEngine.Initialize(); before PythonEngine worked for me, thanks. |
Beta Was this translation helpful? Give feedback.
-
|
If anyone has some nice examples (this repo lacks them) would be great to see them./ |
Beta Was this translation helpful? Give feedback.
-
|
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. |
Beta Was this translation helpful? Give feedback.
-
|
@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. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
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.pathin 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)
Beta Was this translation helpful? Give feedback.
All reactions