C#, IronPython error: IronPython.Runtime.Exceptions.ImportException - No module named requests(C#,IronPython错误:IronPython.Rounme.Exceptions.ImportException-没有模块命名请求)
问题描述
使用VS 2019,我安装了Nuget包IronPythonv2.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:UsersGeorge.condaenvsspenv2");
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:AppsData DestinationsScrapingScrapeProjectsJob1.v4_For_IronPython_CSharpScrapeUrlPages.py", scope);
dynamic testFunction = scope.GetVariable("scrape_all_pages_for_this_search");
dynamic result = testFunction(url_id, url);
Console.WriteLine(result);
}
我发现了一些关于类似问题的帖子,解决方案大多是添加搜索路径。我做到了:
searchPaths.Add(@"C:UsersGeorge.condaenvsspenv2");
这是为使用的Python环境spendv2安装所有内容的地方。 对我需要做的事情有什么建议吗? 谢谢。
推荐答案
我知道这是一个旧帖子,但我发现了一些对我有效的东西,可以帮助我解决未来有关此问题的问题。
- 我从NuGet包安装了
IronPython.StdLib
。 - 我使用IDE(PyCharm)下载了所需的库(
Tkinter
),然后将该文件夹添加到C#项目的Lib
文件夹中。 - 我添加了
var libs = new[] {@"{PATH OF YOUR PROJECT}Lib"}; engine.SetSearchPaths(libs);
这对我很管用。
这篇关于C#,IronPython错误:IronPython.Rounme.Exceptions.ImportException-没有模块命名请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!