Skip to content

Commit

Permalink
Added an example project that installs multiple wheels
Browse files Browse the repository at this point in the history
  • Loading branch information
henon committed Sep 4, 2020
1 parent 597245b commit acac5bc
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Python.Included.sln
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Python.Deployment.EmbeddedR
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Python.Deployment.Download", "examples\Python.Deployment.Download\Python.Deployment.Download.csproj", "{7EC83CDC-5FA7-4ABE-82E8-EE58E9523212}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Python.Deployment.InstallWheel", "examples\Python.Deployment.InstallWheel\Python.Deployment.InstallWheel.csproj", "{FBEB579E-6E90-4804-BF51-D5B38BAD3789}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -58,6 +60,10 @@ Global
{7EC83CDC-5FA7-4ABE-82E8-EE58E9523212}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7EC83CDC-5FA7-4ABE-82E8-EE58E9523212}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7EC83CDC-5FA7-4ABE-82E8-EE58E9523212}.Release|Any CPU.Build.0 = Release|Any CPU
{FBEB579E-6E90-4804-BF51-D5B38BAD3789}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FBEB579E-6E90-4804-BF51-D5B38BAD3789}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FBEB579E-6E90-4804-BF51-D5B38BAD3789}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FBEB579E-6E90-4804-BF51-D5B38BAD3789}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -67,6 +73,7 @@ Global
{4C92618B-BC6A-4AFA-9F49-3808D975D0DD} = {7B4C443D-A615-4717-83B0-036B98B3E6B0}
{0E7266AC-C5B3-44D7-94AF-B7A06128386C} = {7B4C443D-A615-4717-83B0-036B98B3E6B0}
{7EC83CDC-5FA7-4ABE-82E8-EE58E9523212} = {7B4C443D-A615-4717-83B0-036B98B3E6B0}
{FBEB579E-6E90-4804-BF51-D5B38BAD3789} = {7B4C443D-A615-4717-83B0-036B98B3E6B0}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {1AD41278-71D6-4DE3-A5A9-D18A2067B168}
Expand Down
66 changes: 66 additions & 0 deletions examples/Python.Deployment.InstallWheel/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System;
using System.IO;
using System.Threading.Tasks;
using Python.Runtime;

namespace Python.Deployment.InstallWheel
{
class Program
{
static async Task Main(string[] args)
{
// ================================================
// This example demonstrates how to embed a Python distribution (v3.8.5) and install it locally
// ================================================

// set the installation source to be the embedded python zip from our resources
Python.Deployment.Installer.Source = new Deployment.Installer.EmbeddedResourceInstallationSource()
{
Assembly = typeof(Program).Assembly,
ResourceName = "python-3.7.3-embed-amd64.zip",
};

// install in local directory. if you don't set it will install in local app data of your user account
Python.Deployment.Installer.InstallPath = Path.GetFullPath(".");

// see what the installer is doing
Python.Deployment.Installer.LogMessage += Console.WriteLine;


// install from the given source
await Python.Deployment.Installer.SetupPython(force:true);

await Python.Deployment.Installer.InstallWheel(typeof(Program).Assembly,
"numpy-1.16.3-cp37-cp37m-win_amd64.whl");

// ok, now use pythonnet from that installation
PythonEngine.Initialize();

// call Python's sys.version to prove we are executing the right version
dynamic sys=PythonEngine.ImportModule("sys");
Console.WriteLine("### Python version:\n\t" + sys.version);

// call os.getcwd() to prove we are executing the locally installed embedded python distribution
dynamic os = PythonEngine.ImportModule("os");
Console.WriteLine("### Current working directory:\n\t" + os.getcwd());
Console.WriteLine("### PythonPath:\n\t" + PythonEngine.PythonPath);

PythonEngine.Exec(@"
import sys
import math
import numpy as np
print ('Hello world!')
print ('version:' + sys.version)
np.arange(1) # check if numpy is properly loaded
a1 = np.arange(60000).reshape(300, 200)
a2 = np.arange(80000).reshape(200, 400)
result = np.matmul(a1, a2)
print('result: ' + str(result))
");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<None Remove="numpy-1.16.3-cp37-cp37m-win_amd64.whl" />
<None Remove="python-3.7.3-embed-amd64.zip" />
<None Remove="python-3.8.5-embed-amd64.zip" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="numpy-1.16.3-cp37-cp37m-win_amd64.whl" />
<EmbeddedResource Include="python-3.7.3-embed-amd64.zip" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="pythonnet_netstandard_py37_win" Version="2.5.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Python.Deployment\Python.Deployment.csproj" />
</ItemGroup>
</Project>
Binary file not shown.
Binary file not shown.

0 comments on commit acac5bc

Please sign in to comment.