FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
PyNetExamples/EmbeddingPython/Program.cs at master · sschukat/PyNetExamples · GitHub
sschukat
PyNetExamples
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
PyNetExamples
/
EmbeddingPython
/
Program.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
132 lines (117 loc) · 4.7 KB
Breadcrumbs
PyNetExamples
/
EmbeddingPython
/
Program.cs
Copy path
File metadata and controls
132 lines (117 loc) · 4.7 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// <copyright file="Program.cs" company="Stefan Schukat">
// Copyright (c) Stefan Schukat. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
// </copyright>
namespace
EmbeddingPython
{
using
System
;
using
System
.
IO
;
using
System
.
Runtime
.
InteropServices
;
using
Microsoft
.
Win32
;
using
Python
.
Runtime
;
/// <summary>
/// Class which contains the entry point for the program.
/// </summary>
internal
static
class
Program
{
[
DllImport
(
"kernel32.dll"
,
CharSet
=
CharSet
.
Unicode
,
SetLastError
=
true
)
]
[
return
:
MarshalAs
(
UnmanagedType
.
Bool
)
]
private
static
extern
bool
SetDllDirectory
(
string
pathName
)
;
/// <summary>
/// Returns the installation path of an installed Python 3.6 64 Bit from the registry.
/// </summary>
/// <returns>Path to Python installtion or null if not found.</returns>
private
static
DirectoryInfo
GetPythonInstallationPathFromRegistry
(
)
{
DirectoryInfo
installationPath
=
null
;
using
(
var
localMachineKey
=
RegistryKey
.
OpenBaseKey
(
RegistryHive
.
LocalMachine
,
RegistryView
.
Registry64
)
)
{
using
(
var
installPathKey
=
localMachineKey
.
OpenSubKey
(
@"SOFTWARE\Python\PythonCore\3.6\InstallPath"
)
)
{
var
installationPathValue
=
installPathKey
?
.
GetValue
(
null
)
as
string
;
installationPath
=
new
DirectoryInfo
(
installationPathValue
)
;
}
}
return
installationPath
;
}
/// <summary>
/// Sets up the path to an existing Python Enviroment.
/// </summary>
/// <returns>True if an existing python installation was found.</returns>
private
static
bool
PreparePythonEnvironment
(
)
{
var
installationPath
=
GetPythonInstallationPathFromRegistry
(
)
;
if
(
installationPath
!=
null
&&
installationPath
.
Exists
)
{
return
SetDllDirectory
(
installationPath
.
FullName
)
;
}
return
false
;
}
/// <summary>
/// Returns the type nam eof a Python object as a string.
/// </summary>
/// <param name="handle">Handle to the Pyhton object.</param>
/// <returns>Name of the Python object.</returns>
private
static
string
GetPythonTypeName
(
IntPtr
handle
)
{
using
(
var
excType
=
new
PyObject
(
handle
)
)
{
using
(
var
typeName
=
excType
.
GetAttr
(
"__name__"
)
)
{
return
typeName
.
ToString
(
)
;
}
}
}
/// <summary>
/// Runs a simple python interactive loop in a console.
/// </summary>
private
static
void
RunSimpleInteractiveLoop
(
)
{
if
(
PreparePythonEnvironment
(
)
)
{
PythonEngine
.
Initialize
(
)
;
if
(
PythonEngine
.
IsInitialized
)
{
var
localEnvironment
=
new
PyDict
(
)
;
Console
.
WriteLine
(
$
"Python
{
PythonEngine
.
Version
}
in a .NET shell
\n
Type exit() to exit."
)
;
while
(
true
)
{
Console
.
Write
(
">>> "
)
;
var
command
=
Console
.
ReadLine
(
)
;
if
(
command
==
"exit()"
)
{
break
;
}
try
{
try
{
// First try evaluation
var
result
=
PythonEngine
.
Eval
(
command
,
locals
:
localEnvironment
.
Handle
)
;
Console
.
WriteLine
(
result
.
ToString
(
)
)
;
}
catch
(
PythonException
exc
)
when
(
GetPythonTypeName
(
exc
.
PyType
)
==
"SyntaxError"
)
{
// Try exec statement
PythonEngine
.
Exec
(
command
,
locals
:
localEnvironment
.
Handle
)
;
}
}
catch
(
PythonException
exc
)
{
Console
.
WriteLine
(
exc
.
Message
)
;
}
}
PythonEngine
.
Shutdown
(
)
;
}
}
}
/// <summary>
/// The main entry point for the application.
/// </summary>
/// <param name="args">Command line arguments for the application.</param>
private
static
void
Main
(
string
[
]
args
)
{
RunSimpleInteractiveLoop
(
)
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL