Skip to content

Commit 7ef46d4

Browse files
twastvedtGitHub Enterprise
authored andcommitted
Merge pull request #7 from Dynamo/DYN-7554-python-libraries
DYN-7554: Python libraries (also DYN-6919)
2 parents f6c0d91 + d8304c8 commit 7ef46d4

32 files changed

+364
-88
lines changed

DSPythonNet3/DSPythonNet3.csproj

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,18 @@
33
<ProjectGuid>{F1541C2D-80A9-4FE7-8D9E-75A8B9FF3479}</ProjectGuid>
44
<OutputType>Library</OutputType>
55
<AppDesignerFolder>Properties</AppDesignerFolder>
6-
<RootNamespace>DSPythonNet3</RootNamespace>
7-
<AssemblyName>DSPythonNet3</AssemblyName>
8-
<OutputPath>$(SolutionDir)\package_output\DSPythonNet3\extra\</OutputPath>
9-
<DotNet>net8.0</DotNet>
10-
<TargetFramework>$(DotNet)</TargetFramework>
11-
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
12-
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
13-
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
14-
</PropertyGroup>
15-
<PropertyGroup>
6+
<OutputPath>$(BuildOutput)extra\</OutputPath>
7+
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
168
<NoWarn>MSB3539;CS1591;NUnit2005;NUnit2007;CS0618;CS0612;CS0672</NoWarn>
179
</PropertyGroup>
1810
<ItemGroup>
19-
<PackageReference Include="DynamoVisualProgramming.Core" Version="3.4.0-beta6616" ExcludeAssets="runtime" />
20-
<PackageReference Include="DynamoVisualProgramming.DynamoServices" Version="3.4.0-beta6616" ExcludeAssets="runtime" />
21-
<PackageReference Include="pythonnet" Version="3.1.0-preview-ADSK-*" />
22-
</ItemGroup>
23-
<ItemGroup>
24-
<Reference Include="Python.Deployment">
25-
<HintPath>..\extern\Python\Python.Deployment.dll</HintPath>
26-
<Private>True</Private>
27-
</Reference>
28-
<Reference Include="Python.Included">
29-
<HintPath>..\extern\Python\Python.Included.dll</HintPath>
30-
<Private>True</Private>
31-
</Reference>
11+
<InternalsVisibleTo Include="PythonMigrationViewExtension" />
12+
<InternalsVisibleTo Include="DynamoPythonTests" />
13+
14+
<PackageReference Include="DynamoVisualProgramming.Core" Version="$(DynamoPackageVersion)" ExcludeAssets="runtime" />
15+
<PackageReference Include="DynamoVisualProgramming.DynamoServices" Version="$(DynamoPackageVersion)" ExcludeAssets="runtime" />
16+
<PackageReference Include="Python.Included" Version="3.11.6" />
17+
<PackageReference Include="pythonnet" Version="3.1.0-preview-ADSK-*" />
3218
</ItemGroup>
3319
<ItemGroup>
3420
<Compile Update="Properties\Resources.Designer.cs">
@@ -52,4 +38,18 @@
5238
<LastGenOutput>Resources.en-US.Designer.cs</LastGenOutput>
5339
</EmbeddedResource>
5440
</ItemGroup>
55-
</Project>
41+
<ItemGroup>
42+
<ProjectReference Include="..\DSPythonNet3Wheels\DSPythonNet3Wheels.csproj" />
43+
</ItemGroup>
44+
<ItemGroup>
45+
<None Update="pkg.json">
46+
<TargetPath>..\%(Filename)%(Extension)</TargetPath>
47+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
48+
</None>
49+
</ItemGroup>
50+
51+
<Target Name="PrepareExtension" AfterTargets="AfterBuild">
52+
<WriteLinesToFile File="$(BuildOutput)pkg.json" Lines="$([System.IO.File]::ReadAllText($(BuildOutput)pkg.json).Replace('$Version$','$(Version)').Replace('$DynamoVersion$','$(DynamoVersion)').Replace('$DllVersion$','$(MS_PACKAGE_VERSION)'))" Overwrite="true" Encoding="Unicode" />
53+
</Target>
54+
55+
</Project>

DSPythonNet3/DSPythonNet3CodeCompletionProviderCore.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ import clr
7171

7272
private object ExecutePythonScriptCode(string code)
7373
{
74-
DSPythonNet3Evaluator.InstallPython();
74+
DSPythonNet3Evaluator.InstallPythonAsync().Wait();
7575

7676
if (!PythonEngine.IsInitialized)
7777
{
@@ -375,7 +375,7 @@ public DSPythonNet3CodeCompletionProviderCore()
375375
BasicVariableTypes.Add(Tuple.Create(LIST_VARIABLE, typeof(PyList)));
376376
BasicVariableTypes.Add(Tuple.Create(DICT_VARIABLE, typeof(PyDict)));
377377

378-
DSPythonNet3Evaluator.InstallPython();
378+
DSPythonNet3Evaluator.InstallPythonAsync().Wait();
379379

380380
if (!PythonEngine.IsInitialized)
381381
{

DSPythonNet3/DSPythonNet3Evaluator.cs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Collections;
3-
using System.Collections.Generic;
4-
using System.Linq;
3+
using System.Diagnostics;
54
using System.Reflection;
65
using Autodesk.DesignScript.Runtime;
76
using DSPythonNet3.Encoders;
@@ -247,12 +246,18 @@ public override object Evaluate(
247246
return null;
248247
}
249248

250-
InstallPython();
249+
InstallPythonAsync().Wait();
251250
if (!PythonEngine.IsInitialized)
252251
{
253252
PythonEngine.Initialize();
254253
PythonEngine.BeginAllowThreads();
255-
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+
}
256261
}
257262
using (Py.GIL())
258263
{
@@ -263,8 +268,8 @@ public override object Evaluate(
263268

264269
using (PyModule scope = Py.CreateScope())
265270
{
266-
// Reset the 'sys.path' value to the default python paths on node evaluation.
267-
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}";
268273
scope.Exec(pythonNodeSetupCode);
269274

270275
ProcessAdditionalBindings(scope, bindingNames, bindingValues);
@@ -322,7 +327,7 @@ public static object EvaluatePythonScript(
322327
/// NOTE: Calling SetupPython multiple times will add the install location to the path many times,
323328
/// potentially causing the environment variable to overflow.
324329
/// </summary>
325-
internal static void InstallPython()
330+
internal static async Task InstallPythonAsync()
326331
{
327332
if (!isPythonInstalled)
328333
{
@@ -340,7 +345,11 @@ internal static void InstallPython()
340345
Python.Included.PythonEnv.DeployEmbeddedPython = false;
341346
}
342347

343-
Python.Included.Installer.SetupPython().Wait();
348+
await Python.Included.Installer.SetupPython();
349+
350+
Assembly wheelsAssembly = Assembly.LoadFile(Path.Join(Path.GetDirectoryName(Assembly.GetAssembly(typeof(DSPythonNet3Evaluator)).Location), "DSPythonNet3Wheels.dll") );
351+
await Task.WhenAll(wheelsAssembly.GetManifestResourceNames().Where(x => x.Contains(".whl")).Select(wheel => Python.Included.Installer.InstallWheel(wheelsAssembly, wheel)));
352+
344353
isPythonInstalled = true;
345354
}
346355
catch (Exception e)
@@ -438,7 +447,7 @@ private static void InitializeEncoders()
438447
/// </summary>
439448
/// <param name="e">Exception to inspect</param>
440449
/// <returns>Trace back message</returns>
441-
private static string GetTraceBack(Exception e)
450+
private static string? GetTraceBack(Exception e)
442451
{
443452
if (e is not PythonException pythonExc || pythonExc.Traceback == null)
444453
{
@@ -452,7 +461,7 @@ private static string GetTraceBack(Exception e)
452461
throw new NotSupportedException(Properties.Resources.InternalErrorTraceBackInfo);
453462
}
454463

455-
return (string)field.Invoke(pythonExc, [pythonExc.Traceback]);
464+
return (string?)field.Invoke(pythonExc, [pythonExc.Traceback]);
456465
}
457466

458467
#region Marshalling
@@ -661,6 +670,7 @@ private static bool IsMarkedToSkipConversion(PyObject pyObj)
661670

662671
private DataMarshaler inputMarshaler;
663672
private DataMarshaler outputMarshaler;
673+
private string path;
664674

665675
#endregion
666676

DSPythonNet3/Properties/AssemblyInfo.cs

Lines changed: 0 additions & 14 deletions
This file was deleted.

pkg.json renamed to DSPythonNet3/pkg.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"license": "MIT",
33
"file_hash": null,
44
"name": "DSPythonNet3",
5-
"version": "1.0.0",
5+
"version": "$Version$",
66
"description": "***This is PythonNet3 version***",
77
"group": "",
88
"keywords": [
@@ -13,14 +13,14 @@
1313
"dependencies": [],
1414
"host_dependencies": [],
1515
"contents": "",
16-
"engine_version": "3.3.0.5104",
16+
"engine_version": "$DynamoVersion$",
1717
"engine": "dynamo",
1818
"engine_metadata": "",
1919
"site_url": "https://dynamobim.org/",
2020
"repository_url": "https://github.com/DynamoDS/Dynamo",
2121
"contains_binaries": true,
2222
"node_libraries": [
23-
"DSPythonNet3Empty, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
23+
"DSPythonNet3Empty, Version=$DllVersion$, Culture=neutral, PublicKeyToken=null"
2424
],
2525
"copyright_holder": "DynamoTeam",
2626
"copyright_year": "2024"
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputPath>$(BuildOutput)extra\</OutputPath>
4+
</PropertyGroup>
5+
6+
<ItemGroup>
7+
<None Remove="Resources\*.whl" />
8+
<EmbeddedResource Include="Resources\*.whl" />
9+
</ItemGroup>
10+
11+
<Target Name="UpdateWheels">
12+
<RemoveDir Directories="Resources" />
13+
<MakeDir Directories="Resources" />
14+
<Exec Command="pip download --only-binary :all: --platform win_amd64 --implementation cp --python-version 3.11 --dest Resources --no-cache -r requirements.txt" />
15+
</Target>
16+
</Project>
Binary file not shown.
8.13 KB
Binary file not shown.
17.6 KB
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)