C#利用IronPython调用python脚本(含第三方模块)


参考文章:c#调用python脚本

最近项目中遇到使用C#调用Python的项目,其中涉及batchcompute库。遇到一些问题,记录一下。

  • 项目使用的是IronPython 2.7.9版本包,需要在Nuget安装IronPython,
  • 如果报错,需要更新Nuget版本则下载对应版本即可:https://dist.nuget.org/index.html

添加引用:IronPython.dll,Microsoft.Scripting.dll(在IronPython的安装目录中)

一、C#调用简单python文件

文件名未hello.py的python文件内部代码:

#文件目录 /Module/python/hello.py
import time

def welcome(name):
    return "hello " + name

def Add(a, b):
    return a + b

C#文件相关代码:

if (true)
{
    Microsoft.Scripting.Hosting.ScriptRuntime pyRunTime = IronPython.Hosting.Python.CreateRuntime();
    dynamic obj = pyRunTime.UseFile("Module/python/hello.py");

    Console.WriteLine(obj.welcome("宝宝"));
    Console.WriteLine(obj.Add(5, 7));	//方法名必须大小写区分正确
    Console.ReadKey();
}

执行结果为:
在这里插入图片描述

二、C#调用含第三方库python文件

1、环境path写在C#文件中(未解决)

文件名未hello.py的python文件内部代码修改为:

import time

import batchcompute
from batchcompute import CN_ZHANGJIAKOU as REGION
from batchcompute import Client, ClientError

def welcome(name):
    return "hello " + name

def Add(a, b):
    return a + b

C#文件相关代码:

if (true)
{
    var options = new Dictionary<string, object>();
    options["Frames"] = true;
    options["FullFrames"] = true;
    var engine = IronPython.Hosting.Python.CreateEngine(options);
    var scope = engine.CreateScope();

    var paths = engine.GetSearchPaths();
    paths.Add(@"C:\Users\Administrator\AppData\Local\Programs\Python\Python38\Lib\site-packages\batchcompute-2.1.4-py3.8.egg");
    paths.Add(@"C:\Users\Administrator\AppData\Local\Programs\Python\Python38\Lib\site-packages");
    paths.Add(@"C:\Users\Administrator\AppData\Local\Programs\Python\Python38\Lib");
    paths.Add(@"C:\Users\Administrator\AppData\Local\Programs\Python\Python38");
    engine.SetSearchPaths(paths);

    var source = engine.CreateScriptSourceFromFile("Module/python/hello.py");    //ListClusterInstances.py
    source.Execute(scope);

    var welcome = scope.GetVariable<Func<string, string>>("welcome");
    var result1 = welcome("宝宝");
    Console.WriteLine(result1);

    var Add = scope.GetVariable<Func<int, int, int>>("Add");
    var result2 = Add(5, 7).ToString();
    Console.WriteLine(result2);
}

执行结果报出的错误始终是不知缘由,结果如下:
在这里插入图片描述

还望各路路过的大神能不吝赐教,指点一二,解除小白心中的疑惑


2、环境path写入python文件(已解决)

由参考文档看出 paths 添加的环境路径应放置在python文件中,而不是写在C#代码中。

意味着python代码如下:

import time
import sys
sys.path.append('C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python38\\Lib\\')
sys.path.append('C:C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python38\\Lib\\site-packages\\setuptools-12.0.3-py2.7.egg')
sys.path.append('C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python38\\Lib\\site-packages\\batchcompute-2.1.4-py3.8.egg')

import batchcompute
from batchcompute import CN_ZHANGJIAKOU as REGION
from batchcompute import Client, ClientError

def welcome(name):
    return "hello " + name

def Add(a, b):
    return a + b

参考文章: C#调用Python脚本并使用Python的第三方模块

  • 1
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
为了接入穿山甲广告SDK,您需要按照以下步骤进行操作: 1. 首先,您需要在Gradle文件中限制Gradle版本为3.3.3、3.4.3、3.5.4、3.6.4或4.0.1,以支持Android R标签的引入。 2. 在AndroidManifest文件中添加以下代码,用于启动穿山甲广告SDK的SplashActivity,并设置相关的配置属性: ``` <activity android:name="com.unity3d.player.chuanshanjia.CsjSplashActivity" android:configChanges="keyboard|orientation|screenSize" android:screenOrientation="sensorLandscape" android:theme="@style/Theme.Splash"> <!-- android:theme="@android:style/Theme.Black"> --> <intent-filter> <action android:name="android.intent.action.MAIN" /> <!-- 表示在点击app图标的时候启动此Activity --> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> ``` 3. 如果您的应用需要在Android 7.0及以上环境中运行,还需要在AndroidManifest文件中添加以下代码,用于适配Android 7.0以上的设备: ``` <provider android:name="com.bytedance.sdk.openadsdk.TTFileProvider" android:authorities="${applicationId}.TTFileProvider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /> </provider> ``` 以上就是接入穿山甲广告SDK的一般步骤,请根据您的具体情况进行配置。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Android 如何接入穿山甲广告?](https://blog.csdn.net/YoungOne2333/article/details/126381155)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [Android 接入穿山甲SDK之开屏广告](https://blog.csdn.net/qq_41973169/article/details/125428479)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值