Skip to content

Commit d8304c8

Browse files
committed
More accurate default sys.path.
1 parent 25ad9a7 commit d8304c8

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

DSPythonNet3/DSPythonNet3Evaluator.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections;
3+
using System.Diagnostics;
34
using System.Reflection;
45
using Autodesk.DesignScript.Runtime;
56
using DSPythonNet3.Encoders;
@@ -250,7 +251,13 @@ public override object Evaluate(
250251
{
251252
PythonEngine.Initialize();
252253
PythonEngine.BeginAllowThreads();
253-
254+
255+
using (Py.GIL())
256+
using (PyModule scope = Py.CreateScope())
257+
{
258+
scope.Exec("import sys" + Environment.NewLine + "path = str(sys.path)");
259+
path = scope.Get<string>("path");
260+
}
254261
}
255262
using (Py.GIL())
256263
{
@@ -261,8 +268,8 @@ public override object Evaluate(
261268

262269
using (PyModule scope = Py.CreateScope())
263270
{
264-
// Reset the 'sys.path' value to the default python paths on node evaluation.
265-
var pythonNodeSetupCode = "import sys" + Environment.NewLine + "sys.path = sys.path[0:3]";
271+
// Reset the 'sys.path' value to the default python paths on node evaluation. See https://github.com/DynamoDS/Dynamo/pull/10977.
272+
var pythonNodeSetupCode = "import sys" + Environment.NewLine + $"sys.path = {path}";
266273
scope.Exec(pythonNodeSetupCode);
267274

268275
ProcessAdditionalBindings(scope, bindingNames, bindingValues);
@@ -663,6 +670,7 @@ private static bool IsMarkedToSkipConversion(PyObject pyObj)
663670

664671
private DataMarshaler inputMarshaler;
665672
private DataMarshaler outputMarshaler;
673+
private string path;
666674

667675
#endregion
668676

DSpythonNet3Tests/PythonLibraryTests.cs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ public void TestSciPyAvailable()
1010
{
1111
string code = @"
1212
from scipy import special
13-
OUT = special.exp10(3)
13+
OUT = special.round(3.3333333)
1414
";
1515
var empty = new ArrayList();
1616
var result = DSPythonNet3Evaluator.EvaluatePythonScript(code, empty, empty);
17-
Assert.That(result, Is.EqualTo(1000));
17+
Assert.That(result, Is.EqualTo(3.0));
1818
}
1919

2020
[Test]
@@ -107,15 +107,10 @@ import kiwisolver as kiwi
107107
public void TestMatplotlibAvailable()
108108
{
109109
string code = @"
110-
import sys
111-
OUT = sys.version
112-
import unicodedata2
113-
#print(unicodedata.name('A'))
110+
import matplotlib as mpl
114111
115-
#import matplotlib as mpl
116-
117-
#cmap = mpl.colormaps['plasma']
118-
#OUT = cmap.name
112+
cmap = mpl.colormaps['plasma']
113+
OUT = cmap.name
119114
";
120115
var empty = new ArrayList();
121116
var result = DSPythonNet3Evaluator.EvaluatePythonScript(code, empty, empty);
@@ -175,7 +170,7 @@ public void TestPandasAvailable()
175170
import pandas as pd
176171
177172
ser = pd.Series(range(5), index=list('abcde'))
178-
OUT = ser['d']
173+
OUT = ser['d'].item()
179174
";
180175
var empty = new ArrayList();
181176
var result = DSPythonNet3Evaluator.EvaluatePythonScript(code, empty, empty);

0 commit comments

Comments
 (0)