使用VS 2019安装了NuGet包IronPython v2.7.11和DynamicLanguageRuntime v1.3.0,正在尝试运行Python脚本。在脚本中添加了import后,出现了问题:
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。有什么建议吗?谢谢。