Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fac1347

Browse files
committed
SetPythonPath prints sys.path on failure
1 parent 0f5cd13 commit fac1347

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

src/embed_tests/TestPythonEngineProperties.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -185,24 +185,24 @@ public void SetPythonPath()
185185

186186
using (Py.GIL())
187187
{
188+
// path should not be set to PythonEngine.PythonPath here.
189+
// PythonEngine.PythonPath gets the default module search path, not the full search path.
190+
// The list sys.path is initialized with this value on interpreter startup;
191+
// it can be (and usually is) modified later to change the search path for loading modules.
192+
// See https://docs.python.org/3/c-api/init.html#c.Py_GetPath
193+
// After PythonPath is set, then PythonEngine.PythonPath will correctly return the full search path.
194+
195+
string[] paths = Py.Import("sys").GetAttr("path").As<string[]>();
196+
path = string.Join(System.IO.Path.PathSeparator.ToString(), paths);
197+
188198
try
189199
{
190200
Py.Import(moduleName);
191201
}
192202
catch (PythonException ex)
193203
{
194-
throw new Exception($"Py.Import(\"{moduleName}\") failed before setting PythonEngine.PythonPath", ex);
204+
throw new Exception($"Py.Import(\"{moduleName}\") failed before setting PythonEngine.PythonPath. sys.path={path}", ex);
195205
}
196-
197-
string[] paths = Py.Import("sys").GetAttr("path").As<string[]>();
198-
path = string.Join(System.IO.Path.PathSeparator.ToString(), paths);
199-
200-
// path should not be set to PythonEngine.PythonPath here.
201-
// PythonEngine.PythonPath gets the default module search path, not the full search path.
202-
// The list sys.path is initialized with this value on interpreter startup;
203-
// it can be (and usually is) modified later to change the search path for loading modules.
204-
// See https://docs.python.org/3/c-api/init.html#c.Py_GetPath
205-
// After PythonPath is set, then PythonEngine.PythonPath will correctly return the full search path.
206206
}
207207

208208
PythonEngine.PythonPath = path;

0 commit comments

Comments
 (0)