You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When installing editable dependencies with pip or other python package managers into a virtualenv, they end up in site-packages as .pth files which contain the actual absolute path of the package.
Usually, python is able to follow these as if they were symlinks and import packages that were installed that way. However, this doesn't seem to work with Python.NET. I just get an error in Py.Import:
An exception of type 'Python.Runtime.PythonException' occurred in Python.Runtime but was not handled in user code: 'No module named 'mymodule''
I do have a workaround for this, which is just to read all the .pth files and add those paths to PYTHONPATH manually, but it would be much nicer if this could just work out of the box.
stringvirtualEnvPath=$@".\myDir\.venv";stringvenvSitePackagesPath=$@"{sVirtualEnvPath}\Lib\site-packages";// Resolve editable dependencies manually because Python.NET doesn't do sostring[]editableDependencyReferences=Directory.GetFiles(venvSitePackagesPath,"*.pth");string[]editableDependencyPaths=editableDependencyReferences.Select(filePath =>File.ReadAllText(filePath).Trim()).ToArray();stringpythonPath=$"{venvSitePackagesPath};"+@"C:\Program Files\Python312\Lib;"+@"C:\Program Files\Python312\DLLs;"+string.Join(";",editableDependencyPaths);// Add them to the pathEnvironment.SetEnvironmentVariable("PATH",Environment.GetEnvironmentVariable("PATH").TrimEnd(';')+";"+virtualEnvPath,EnvironmentVariableTarget.Process);Environment.SetEnvironmentVariable("PYTHONHOME",virtualEnvPath,EnvironmentVariableTarget.Process);Environment.SetEnvironmentVariable("PYTHONPATH",pythonPath,EnvironmentVariableTarget.Process);PythonEngine.PythonHome=virtualEnvPath;PythonEngine.PythonPath=pythonPath;PythonEngine.Initialize();using(Py.GIL()){dynamicmymodule=Py.Import("mymodule");// Succeeds now!}
The text was updated successfully, but these errors were encountered:
This is a higher-level interface that calls the current “import hook function” (with an explicit level of 0, meaning absolute import). It invokes the __import__() function from the __builtins__ of the current globals. This means that the import is done using whatever import hooks are installed in the current environment.
We do exactly what the built in import does.
If you can identify an actual issue with our setup (e.g. with regard to the initial configuration), feel free to reopen. But "parsing" pth files (which your code example is doing in a too simplistic fashion, pth files are allowed to contain Python code) can't be the answer and I don't consider this as of now a bug in pythonnet itself.
When installing editable dependencies with pip or other python package managers into a virtualenv, they end up in
site-packages
as.pth
files which contain the actual absolute path of the package.Usually, python is able to follow these as if they were symlinks and import packages that were installed that way. However, this doesn't seem to work with Python.NET. I just get an error in
Py.Import
:I do have a workaround for this, which is just to read all the
.pth
files and add those paths to PYTHONPATH manually, but it would be much nicer if this could just work out of the box.The text was updated successfully, but these errors were encountered: