Код:
Код: Выделить всё
namespace PythonLoader
{
using System;
using System.Runtime.InteropServices;
using GetSomeInput;
using Python.Runtime;
public class Program
{
public static void Main(string[] args)
{
try
{
string script = Inputty.GetString("Module name: ", "script", false);
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Runtime.PythonDLL = @"C:\Python312\python312.dll";
}
else
{
// see https://stackoverflow.com/questions/20582270/distribution-independent-libpython-path
Runtime.PythonDLL = @"/usr/lib/x86_64-linux-gnu/libpython3.10.so";
}
Console.WriteLine("Using Python DLL " + Runtime.PythonDLL);
PythonEngine.Initialize();
PythonEngine.BeginAllowThreads();
using (Py.GIL())
{
dynamic app = Py.Import(script);
Console.WriteLine(app.multiply(2, 4));
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
}
}
Код: Выделить всё
def multiply(a, b):
return a * b
Код: Выделить всё
from script import multiply
Работает в Windows:
Код: Выделить всё
C:\Code\Misc\PythonLoader\PythonLoader\bin\Debug\net8.0>pythonloader
Module name: [script]
Using Python DLL C:\Python312\python312.dll
8
Код: Выделить всё
joel@ubuntu:~/Code/pythontest/bin/Debug/net8.0$ dotnet pythontest.dll
Python filename: [script.py]
Using Python DLL /usr/lib/x86_64-linux-gnu/libpython3.10.so
Python.Runtime.PythonException: No module named 'script'
at Python.Runtime.PythonException.ThrowLastAsClrException()
at Python.Runtime.NewReferenceExtensions.BorrowOrThrow(NewReference& reference)
at Python.Runtime.PyModule.Import(String name)
at Python.Runtime.Py.Import(String name)
at PythonLoader.Program.Main(String[] args) in /home/joel/Code/pythontest/Program.cs:line 49
Код: Выделить всё
C:\Code\Misc\PythonLoader\PythonLoader\bin\Debug\net8.0>dir
Volume in drive C is OS
Volume Serial Number is 541C-D54E
Directory of C:\Code\Misc\PythonLoader\PythonLoader\bin\Debug\net8.0
05/24/2024 08:34 PM .
05/24/2024 09:07 AM ..
01/11/2024 11:37 AM 322,048 GetSomeInput.dll
10/11/2023 12:15 AM 431,616 Python.Runtime.dll
05/24/2024 02:32 PM 7,378 PythonLoader.deps.json
05/24/2024 08:34 PM 6,656 PythonLoader.dll
05/24/2024 08:34 PM 142,848 PythonLoader.exe
05/24/2024 08:34 PM 10,932 PythonLoader.pdb
05/24/2024 09:17 AM 268 PythonLoader.runtimeconfig.json
05/24/2024 09:16 AM 37 script.py
05/24/2024 03:40 PM 29 __init__.py
05/24/2024 09:21 AM __pycache__
9 File(s) 921,812 bytes
3 Dir(s) 48,029,245,440 bytes free
Код: Выделить всё
joel@ubuntu:~/Code/pythontest/bin/Debug/net8.0$ ls -la
total 860
drwxrwxr-x 2 joel joel 4096 May 24 20:33 .
drwxrwxr-x 3 joel joel 4096 May 24 15:41 ..
-rwxrw-r-- 1 joel joel 322048 Jan 11 10:32 GetSomeInput.dll
-rw-rw-r-- 1 joel joel 31 May 24 15:52 __init__.py
-rwxrw-r-- 1 joel joel 431616 Oct 11 2023 Python.Runtime.dll
-rwxr-xr-x 1 joel joel 72448 May 24 20:33 pythontest
-rw-rw-r-- 1 joel joel 7171 May 24 15:41 pythontest.deps.json
-rw-rw-r-- 1 joel joel 6656 May 24 20:33 pythontest.dll
-rw-rw-r-- 1 joel joel 10964 May 24 20:33 pythontest.pdb
-rw-rw-r-- 1 joel joel 257 May 24 15:41 pythontest.runtimeconfig.json
-rw-rw-r-- 1 joel joel 38 May 24 20:31 script.py
Подробнее здесь: https://stackoverflow.com/questions/785 ... windows-11
Мобильная версия