python - 从标准 Python 调用 IronPython

标签 python .net interop ironpython python.net

如何从 Python 中调用 IronPython 函数?有没有一种简单的方法来连接两者。我需要 IronPython 中不可用的一整套适当的 Python 库和 Python .net 目前没有的最新 CLR 的灵 active 。

到目前为止,我尝试过编译一个 IronPython dll,但我无法在 Python 中正确加载它。

我试图让 IronPython n 可以从 Python 调用

foo.py

def foo():
    print 'hello world'

compile_ipy.py

import clr
clr.CompileModules("foo.dll", "foo.py")

我尝试从 Python 调用 Iron Python

call_ipy_from_py1.py

import ctypes
dll = ctypes.WindDLL('foo.dll')
dll.foo() # AttributeError: function 'foo' not found

call_ipy_from_py2.py

import ctypes
dll = ctypes.cdll.LoadLibrary('foo.dll')
dll.foo() # AttributeError: function 'foo' not found

最佳答案

.NET DLL 与 C DLL(您可以通过 ctypes 使用)不同。但是,您可以使用 Python.NET 库来调用函数,如下所示:

import clr # Imports Python.Runtime.dll
import foo # Imports foo.dll
foo.foo()

我不知道“Python .net 目前没有的最新 CLR”是什么意思。您可以使用此处提供的版本:http://www.lfd.uci.edu/~gohlke/pythonlibs/#pythonnet如果您需要使用 CPython 3,我将它与 .NET 4.5.1 库一起使用,没有任何问题。

关于python - 从标准 Python 调用 IronPython,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27800327/

相关文章:

python - Flask如何处理URL中的特殊字符

android - 以编程方式将 Windows 10 与移动设备配对,而无需在 Windows 端进行用户交互?

c# - 尝试发布 SQL CLR 数据库的日期时间类型不匹配

.net - codecampserver 中的 StaticFactory<T> 是众所周知的模式吗?

c# - 如何使用 Excel Interop 获取 CustomDocumentProperties?

java - 访问 Java 类的静态字段时出现问题

php - 在当前目录中启动服务器 (php/apache)

python - scrapy选择器字符串不接受国际字符

python - 如何让 Discord Bot 播放 YouTube 音频

vb6 - 从 vb6 加载 dll 时找不到文件