c# - C#,IronPython 错误:IronPython.Runtime.Exceptions.ImportException - 没有名为请求的模块
问题描述
使用 VS 2019 我安装了 nuget 包 IronPython v2.7.11 和 DynamicLanguageRuntime v1.3.0 并尝试运行 python 脚本。在我将导入添加到脚本之前它运行良好:
import requests, time, socket
import random
这是C#:
static void Main(string[] args)
{
DateTime start = DateTime.Now;
ScriptEngine engine = Python.CreateEngine();
var searchPaths = engine.GetSearchPaths();
// Path to where everything is installed for this Python enviroment
searchPaths.Add(@"C:\Users\George\.conda\envs\spenv2");
int url_id = 1701;
string url = "https://www.yellowpages.com/search?search_terms=Health&geo_location_terms=Aliso+Viejo%2C+CA";
ScriptScope scope = engine.CreateScope();
engine.ExecuteFile(@"D:\Apps\Data Destinations\Scraping\ScrapeProjects\Job1.v4_For_IronPython_CSharp\ScrapeUrlPages.py", scope);
dynamic testFunction = scope.GetVariable("scrape_all_pages_for_this_search");
dynamic result = testFunction(url_id, url);
Console.WriteLine(result);
}
我发现了一些关于类似问题的帖子,其中大多数的解决方案是添加搜索路径。我做了什么:
searchPaths.Add(@"C:\Users\George\.conda\envs\spenv2");
这是为 Python 环境安装所有东西的地方,spendv2,正在使用。关于我需要做什么的任何建议?谢谢你。
解决方案
我看到的是您正在使用 get 作为搜索路径,因此您加载了字符串集合,但缺少的是engine.SetSearchPaths(searchPaths);
让它工作。
推荐阅读
- java - 数组溢出检查
- android - 在匕首模块的上下文中,Kotlin 对象和类有什么区别
- sql - 在包含记录的现有表中,如何创建一个新的 datetime2(2) 列并使用基于另一列的值填充它?
- django - CSRF禁止403的所有可能原因
- swift5 - Swift中的嵌套函数意外行为?
- c# - 玩家不断穿墙
- python - USB 设备没有 langid
- reactjs - 如何使用 ”
“在函数内部? - ios - 安装 FBAudienceNetwork (5.10.1) 失败
- java - 有没有办法在创建新项目期间更改 IntelliJ 中的设置并禁用 Main 类的自动生成?