标签: pythonnet

如何在Windows上安装Python for .NET

我下载了Python for .NET.
里面的拉链是clr.pyd,nPython.exe,Python.Runtime.dll和2个调试数据库文件.
我把clr.pyd和Python.Runtime.dll放在我的python DLL目录C:\ Python27\DLLs中,认为这就是安装所需的全部内容.然后我打开Python GUI并输入import clr,我得到:

Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    import clr
SystemError: dynamic module not initialized properly
Run Code Online (Sandbox Code Playgroud)

新的python但不是.NET,想要使用CPython而不是IronPython.我在这个装置中缺少什么?Python for .NET的自述文件说有一个Windows软件包的安装,但我找到的只是zip文件.

.net c# python python.net pythonnet

16
推荐指数
2
解决办法
3万
查看次数

在 Python 中为 .NET 实现 C# 接口

我得到了一个用 C# 编写的库,我正在尝试使用 Python for .NET 调用它。

我需要一个实例的主类有一个构造函数,如:

GDhuClient(IGDhuSettings)
Run Code Online (Sandbox Code Playgroud)

没有(公开的)实现IGDhuSettings接口的类。当我创建一个 Python 类来实现它时,例如,

class PyGDhuSettings(IGDhuSettings):
    ...
Run Code Online (Sandbox Code Playgroud)

TypeError: interface takes exactly one argument如果我没有__new__方法或者我以正常方式定义一个方法,我会得到:

def __new__(cls):
    return super().__new__(cls)
Run Code Online (Sandbox Code Playgroud)

如果我尝试实例化接口,就好像它是一个类,我要么得到相同的错误(没有或 >1 个参数),要么<whatever> does not implement IGDhuSettings我传递一个参数。

查看Python for .NET 源代码

using System;
using System.Reflection;
using System.Runtime.InteropServices;

namespace Python.Runtime
{
    /// <summary>
    /// Provides the implementation for reflected interface types. Managed
    /// interfaces are represented in Python by actual Python type objects.
    /// Each of those …
Run Code Online (Sandbox Code Playgroud)

c# python interface python.net pythonnet

9
推荐指数
1
解决办法
3005
查看次数

在 Python3.6 中调用 C# 代码

由于完全不了解 C# 中的编码,我希望在我的 python 代码中调用一个 C# 函数。我知道围绕同一问题有很多问答,但由于某些奇怪的原因,我无法从示例 python 模块导入一个简单的 c# 类库。

以下是我所做的 -

C# 类库设置

我正在使用 VS 2017 CE。

TestClassLibrary在以下类型下创建了一个新项目ClassLibrary(.NET Standard)

项目内部的类如下——

MyClass.cs

using System;
namespace TestClassLibrary
{
    public class MyClass
    {
        public string function()
        {
            return "Hello World!";
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这已成功构建,在.dll目录下生成文件\bin\Debug\netstandard2.0TestClassLibrary.dll

现在,我切换到 python3.6(在 virtualenv 上运行,支持 pythonnet 2.3.0)

主文件

import sys
sys.path.append(r"<Ablsloute Path to \bin>\Debug\netstandard2.0")
import clr
clr.AddReference(r"TestClassLibrary")
from TestClassLibrary import MyClass
Run Code Online (Sandbox Code Playgroud)

当我运行时python main.py,代码失败并出现错误 -

Traceback (most recent call …
Run Code Online (Sandbox Code Playgroud)

.net c# python python.net pythonnet

7
推荐指数
1
解决办法
3507
查看次数

Python for .NET:如何使用同一DLL的不同版本显式创建C#类的实例?

我有一个.cs文件

namespace SomeNamepace
{


    public struct SomeStruct
    {
        ....
    }

    public static class SomeClass
    {
        ....
    }
Run Code Online (Sandbox Code Playgroud)

到目前为止,我使用它与PythonNET一样

import clr
clr.AddReference('c:\\Test\Module.dll')
from SomeNamepace import SomeClass, SomeStruct

SomeClass.SomeMethod(...)
Run Code Online (Sandbox Code Playgroud)

我现在的问题是我需要使用具有相同名称且没有设置版本号的dll,因此PythonNET不会将它们视为两个不同的dll,而是相同.即使我使用AddReference的完整路径导入它们.

现在我想明确地使用它们,如下所述:

Python for .NET:在多个版本中使用相同的.NET程序集

喜欢

lib = clr.AddReference('c:\\Test\Module.dll')
Run Code Online (Sandbox Code Playgroud)

我尝试了很多东西来创建一个SomeClass类似的实例

lib.SomeNamespace.SomeClass()
Run Code Online (Sandbox Code Playgroud)

要么

import System
System.Activator.CreateInstance(lib.GetType('SomeNamespace.SomeClass'))
Run Code Online (Sandbox Code Playgroud)

或使用方法InitializeCreateInstance

或者如下面的链接中所述

from System import Type
type1 = lib.GetType('SomeNamespace.SomeClass')
constructor1 = type1.GetConstructor(Type.EmptyTypes)    
Run Code Online (Sandbox Code Playgroud)

最后一切都失败了,没找到的东西,没有方法等等.

这样做的正确语法是什么?

.net c# python python.net pythonnet

6
推荐指数
1
解决办法
1509
查看次数

使用pythonnet导入DLL

我正在尝试在 python 中导入和使用 DLL。因此我正在使用pythonnet。

import sys
import clr

sys.path.append('C:\PathToDllFolder')

clr.AddReference('MyDll.dll')
Run Code Online (Sandbox Code Playgroud)

但是代码会产生以下错误:

Traceback (most recent call last):
  File "E:\NET\NET_test.py", line 6, in <module>
    clr.AddReference('MyDll.dll')
System.IO.FileNotFoundException: Unable to find assembly 'MyDll.dll'.
   bei Python.Runtime.CLRModule.AddReference(String name)
Run Code Online (Sandbox Code Playgroud)

DLL 的目标运行时为:v4.0.30319

有什么方法可以找出导入失败的原因以及如何修复它?

(如有必要,我也可以提供 DLL)

.net python dll python.net pythonnet

6
推荐指数
3
解决办法
1万
查看次数

如何获得readthedocs.org构建以忽略我的require.txt?

我在github上有一个小型应用程序项目,该项目可在Windows上运行,并且需要pythonnet

我的requirement.txt包含:

beautifulsoup4==4.6
pythonnet==2.3
Run Code Online (Sandbox Code Playgroud)

现在我想为它建立一个文档并放上它readthedocs.org。将文档推送到github,将项目导入之后readthedocs.org,我尝试构建文档,但是此操作失败,并显示以下信息:

Collecting pythonnet==2.3 (from -r /home/docs/checkouts/readthedocs.org/user_builds/trelloradar/checkouts/latest/requirements.txt (line 2))
Using cached pythonnet-2.3.0.tar.gz
Building wheels for collected packages: pythonnet
Running setup.py bdist_wheel for pythonnet: started
Running setup.py bdist_wheel for pythonnet: finished with status 'error'
Complete output from command /home/docs/checkouts/readthedocs.org/user_builds/trelloradar/envs/latest/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-iU2ADR/pythonnet/setup.py';  f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" bdist_wheel -d /tmp/tmpnPH_1rpip-wheel- --python-tag cp27:
running bdist_wheel
running build
running build_ext
/bin/sh: 1: mono: not found
Traceback (most recent call …
Run Code Online (Sandbox Code Playgroud)

python windows requirements.txt read-the-docs pythonnet

2
推荐指数
1
解决办法
317
查看次数