首页 > 解决方案 > 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,正在使用。关于我需要做什么的任何建议?谢谢你。

标签: c#python-requestsironpython

解决方案


我看到的是您正在使用 get 作为搜索路径,因此您加载了字符串集合,但缺少的是engine.SetSearchPaths(searchPaths);让它工作。


推荐阅读