Python.net 在 Ubuntu 22.04 上使用模块时出现问题,但可以在 Windows 11 上使用

问题描述 投票:0回答:1

我有一个非常简单的 C# 应用程序,在 net8.0 上使用 Python.net 3.0.3,它尝试调用 bin/Debug/net8.0 目录中的 Python 模块,该模块返回两个数字的乘积。在 Windows 上它可以工作,在 Ubuntu 上我收到一个

Python.Runtime.PythonException
和消息
No module named 'script'
。两台机器上的文件是相同的。

代码:

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());
            }
        }
    }
}

文件

script.py

def multiply(a, b):
  return a * b

文件

__init__.py

from script import multiply

这两个文件都被复制到输出目录(例如 bin/Debug/net8.0。

适用于 Windows:

C:\Code\Misc\PythonLoader\PythonLoader\bin\Debug\net8.0>pythonloader
Module name:  [script]
Using Python DLL C:\Python312\python312.dll
8

在 Ubuntu 上失败:

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

Windows 上的目录内容:

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    <DIR>          .
05/24/2024  09:07 AM    <DIR>          ..
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    <DIR>          __pycache__
               9 File(s)        921,812 bytes
               3 Dir(s)  48,029,245,440 bytes free

Ubuntu 上的目录内容:

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

任何帮助将不胜感激!

c# python.net
1个回答
0
投票

Python 默认情况下不会在当前目录中搜索导入。

© www.soinside.com 2019 - 2024. All rights reserved.