回答編集履歴
1
pythonnet3
test
CHANGED
@@ -26,7 +26,6 @@
|
|
26
26
|
```cs
|
27
27
|
using System;
|
28
28
|
using System.IO;
|
29
|
-
using System.Linq;
|
30
29
|
using System.Windows.Forms;
|
31
30
|
using Python.Runtime;
|
32
31
|
|
@@ -34,52 +33,62 @@
|
|
34
33
|
{
|
35
34
|
public partial class Form1 : Form
|
36
35
|
{
|
36
|
+
private TextBox textBox1;
|
37
|
+
|
37
38
|
public Form1()
|
38
39
|
{
|
39
40
|
InitializeComponent();
|
40
41
|
|
42
|
+
FormClosing += Form1_FormClosing;
|
43
|
+
|
41
|
-
|
44
|
+
textBox1 = new TextBox
|
42
45
|
{
|
43
46
|
Dock = DockStyle.Fill,
|
44
47
|
Multiline = true,
|
45
48
|
Parent = this,
|
46
49
|
};
|
50
|
+
textBox1.Click += TextBox1_Click;
|
47
51
|
|
48
|
-
// python環境にパスを通す
|
49
|
-
// TODO: 環境に合わせてパスを直すこと
|
50
|
-
var PYTHON_HOME = Environment.ExpandEnvironmentVariables(@"C:\Users\hoge\anaconda3\envs\fuga");
|
51
52
|
|
52
|
-
|
53
|
+
var PYTHON_DLL_PATH = @"C:\Users\自分のユーザ名\anaconda3\envs\DIcom\python38.dll";
|
54
|
+
Runtime.PythonDLL = PYTHON_DLL_PATH;
|
53
55
|
|
56
|
+
var PYTHON_HOME = @"C:\Users\自分のユーザ名\anaconda3\envs\DIcom\";
|
57
|
+
Environment.SetEnvironmentVariable("PYTHONHOME", PYTHON_HOME, EnvironmentVariableTarget.Process);
|
54
58
|
PythonEngine.PythonHome = PYTHON_HOME;
|
59
|
+
|
60
|
+
var MODULE_PATH = @"C:\Users\自分のユーザ名\anaconda3\envs\DIcom\";
|
61
|
+
PythonEngine.PythonPath = string.Join(Path.PathSeparator.ToString(),
|
62
|
+
PythonEngine.PythonPath,
|
63
|
+
Path.Combine(MODULE_PATH, @"Lib\site-packages"));
|
55
64
|
|
56
65
|
PythonEngine.Initialize();
|
57
66
|
|
58
67
|
using (Py.GIL())
|
59
68
|
{
|
69
|
+
dynamic np = Py.Import("numpy");
|
70
|
+
dynamic np_version = np.__version__;
|
71
|
+
textBox1.Text += $"numpyバージョン:{np_version}\r\n";
|
72
|
+
}
|
73
|
+
}
|
74
|
+
|
75
|
+
private void TextBox1_Click(object sender, EventArgs e)
|
76
|
+
{
|
77
|
+
using (Py.GIL())
|
78
|
+
{
|
60
79
|
dynamic myMath = Py.Import("my_awesome_lib.my_math");
|
61
80
|
dynamic calculator = myMath.Calculator(5, 7);
|
62
81
|
|
63
|
-
textBox.Text += $"5 + 7 = {calculator.add()}\r\n";
|
82
|
+
textBox1.Text += $"5 + 7 = {calculator.add()}\r\n";
|
64
|
-
textBox.Text += $"sum(1,2,3,4,5) = {myMath.Calculator.sum(new[] { 1, 2, 3, 4, 5 })}\r\n";
|
83
|
+
textBox1.Text += $"sum(1,2,3,4,5) = {myMath.Calculator.sum(new[] { 1, 2, 3, 4, 5 })}\r\n";
|
65
84
|
dynamic dict = myMath.GetDict();
|
66
|
-
textBox.Text += dict[3];
|
85
|
+
textBox1.Text += $"{dict[3]}\r\n";
|
67
|
-
|
68
86
|
}
|
69
|
-
PythonEngine.Shutdown();
|
70
87
|
}
|
71
88
|
|
72
|
-
private
|
89
|
+
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
|
73
90
|
{
|
74
|
-
var envPaths = Environment.GetEnvironmentVariable("PATH").Split(Path.PathSeparator).ToList();
|
75
|
-
foreach (var path in paths)
|
76
|
-
{
|
77
|
-
if (path.Length > 0 && !envPaths.Contains(path))
|
78
|
-
{
|
79
|
-
|
91
|
+
PythonEngine.Shutdown();
|
80
|
-
}
|
81
|
-
}
|
82
|
-
Environment.SetEnvironmentVariable("PATH", string.Join(Path.PathSeparator.ToString(), envPaths), EnvironmentVariableTarget.Process);
|
83
92
|
}
|
84
93
|
}
|
85
94
|
}
|
@@ -106,4 +115,6 @@
|
|
106
115
|
3 : "dictionary",
|
107
116
|
}
|
108
117
|
```
|
118
|
+
[NuGet Gallery | pythonnet 3.0.1](https://www.nuget.org/packages/pythonnet/3.0.1)
|
119
|
+
|
109
120
|
![実行状態](https://ddjkaamml8q8x.cloudfront.net/questions/2023-04-17/28851859-a914-49c4-914b-3a31f19ad2b1.png)
|