英文:
Trouble shooting using Pythonnet and setting Runtime.PythonDLL property
问题
抱歉,以下是您要翻译的内容:
"我尝试通过Pythonnet来使用.NET Framework 4.8的程序集。我正在使用Python 3.10和版本3.0.1。Pythonnet的文档中提到:
> 从版本3.0开始,您必须设置Runtime.PythonDLL属性或PYTHONNET_PYDLL环境变量,否则在调用Initialize时将收到BadPythonDllException(从MissingMethodException派生的内部异常)。典型的值是python38.dll(Windows)、libpython3.8.dylib(Mac)、libpython3.8.so(大多数其他类Unix操作系统)。
然而,文档不幸地没有说明如何设置该属性,我不明白如何做到这一点。
当我尝试以下操作:
import clr
from pythonnet import load
load('netfx')
clr.AddReference(r'path\to\my.dll')
不出所料,会出现以下错误:
Failed to initialize pythonnet: System.InvalidOperationException: 必须在初始化运行时之前设置此属性
在 Python.Runtime.Runtime.set_PythonDLL(String value)
在 Python.Runtime.Loader.Initialize(IntPtr data, Int32 size)
在 Python.Runtime.Runtime.set_PythonDLL(String value)
在 Python.Runtime.Loader.Initialize(IntPtr data, Int32 size)
[...]
in load
raise RuntimeError("Failed to initialize Python.Runtime.dll")
RuntimeError: Failed to initialize Python.Runtime.dll
现在的问题是,Runtime.PythonDLL属性或PYTHONNET_PYDLL环境变量应该在哪里以及如何设置。
谢谢,
Jens"
英文:
I try to use an assembly for .NET framework 4.8 via Pythonnet. I am using version 3.0.1 with Python 3.10. The documentation of Pythonnet is stating:
> You must set Runtime.PythonDLL property or PYTHONNET_PYDLL environment variable starting with version 3.0, otherwise you will receive BadPythonDllException (internal, derived from MissingMethodException) upon calling Initialize. Typical values are python38.dll (Windows), libpython3.8.dylib (Mac), libpython3.8.so (most other Unix-like operating systems).
However, the documentation unfortunately is not stating how the property is set and I do not understand how to do this.
When I try:
import clr
from pythonnet import load
load('netfx')
clr.AddReference(r'path\to\my.dll')
unsurprisingly the following error is coming up
Failed to initialize pythonnet: System.InvalidOperationException: This property must be set before runtime is initialized
bei Python.Runtime.Runtime.set_PythonDLL(String value)
bei Python.Runtime.Loader.Initialize(IntPtr data, Int32 size)
bei Python.Runtime.Runtime.set_PythonDLL(String value)
bei Python.Runtime.Loader.Initialize(IntPtr data, Int32 size)
[...]
in load
raise RuntimeError("Failed to initialize Python.Runtime.dll")
RuntimeError: Failed to initialize Python.Runtime.dll
The question now is, where and how the Runtime.PythonDLL property or PYTHONNET_PYDLL environment variable is set
Thanks,
Jens
答案1
得分: 1
我理解你的用例是要在Python中嵌入.NET,但我理解你所说的"设置Runtime.PythonDLL
属性"是为了在.NET中嵌入Python,这也是我的用例。不管怎样,也许以下信息会有用。
在GitHub的主要README.md底部隐藏着一个指向WiKi的链接(当然还有GitHub仓库顶部的标签),在那里有更多详细的信息和链接到有用文章。
主要的README.md
声称Runtime.PythonDLL
"必须设置",但他们的示例代码并没有说明如何设置它。此外,官方网站上的文档声称必须"引用" Python.Runtime.dll
,这进一步令人困惑。
根据我的经验,使用Visual Studio通过pythonnet
NuGet包安装时,Python.Runtime.dll
会自动被引用。也许在较早版本中,或者以其他方式安装Python.NET时,Python.Runtime.dll
不会自动被引用?
回答你的问题"如何设置Runtime.PythonDLL
属性?",我理解的做法是在进行其他通常的设置之前,将一个字符串路径赋给该属性:
Runtime.PythonDLL = @"C:\Users\<username>\AppData\Local\Programs\Python\Python310\python38.dll";
在我的情况下,我通过在Windows上使用where python
(在bash中使用which
,在PowerShell中使用Get-Command
)找到了这个路径:
C:\>where python
C:\Users\<username>\AppData\Local\Programs\Python\Python310\python.exe
英文:
The way I understand your use case is for Embedding .NET in Python however, the way I understand the requirement to "set Runtime.PythonDLL
property" is that is for Embedding Python in .NET which was my use case. Anyhow, maybe the following will be useful.
Hidden at the bottom of the main GitHub README.md is a link to the WiKi (and of course the tab at the top of the GitHub repo) where thankfully there is a lot more detailed information and links to useful articles.
The main README.md
asserts Runtime.PythonDLL
"must be set" yet their example code does not illustrate doing so. Furthermore, the documentation on the official website asserts Python.Runtime.dll
must be "referenced" which further confuses things.
In my experience Python.Runtime.dll
was automatically referenced when installing the pythonnet
NuGet package via Visual Studio. Perhaps Python.Runtime.dll
does not automatically get referenced in earlier version or maybe when Python.NET is installed in other ways other than by using NuGet?
To answer your question "how to set Runtime.PythonDLL
property?". My understanding is the way that is done is by assigning a string path to that property before the other usual setup:
Runtime.PythonDLL = @"C:\Users\<username>\AppData\Local\Programs\Python\Python310\python38.dll"
In my case, I found this path by using where python
on Windows (which
in bash, or Get-Command
in PowerShell):
C:\>where python
C:\Users\<username>\AppData\Local\Programs\Python\Python310\python.exe
答案2
得分: 0
我相信这是因为import clr
在内部调用了pythonnet.load
,在您使用的pythonnet版本中,这种情况不会打印任何警告。
例如,正确的方法是在您第一次调用import clr
之前调用load
。
英文:
I believe this is because import clr
internally calls pythonnet.load
, and in the version of pythonnet you are using this situation does not print any warning.
E.g. the right way is to call load
before you call import clr
for the first time.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论