Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#, IronPython error: IronPython.Runtime.Exceptions.ImportException - No module named requests

Using VS 2019 I installed the nuget packages IronPython v2.7.11 and DynamicLanguageRuntime v1.3.0 and am trying to run a python script. It ran fine until I added imports to the script:

import requests, time, socket
import random

Here’s the 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);
}

I found a few posts regarding similar issues and the solution most of them was to add search paths. Which I did:

searchPaths.Add(@"C:\Users\George\.conda\envs\spenv2");

This is where everything is installed for the Python environment, spendv2, is being used. Any recommendations on what I need to do? Thank you.

like image 631
spacedog Avatar asked Mar 09 '26 17:03

spacedog


1 Answers

I know that this is an old post, but I found something that works for me that can help in future questions about this.

  1. I installed IronPython.StdLib from the NuGet Package.
  2. I downloaded the needed library (Tkinter) with my IDE (PyCharm) and then I added the folder to the Lib folder in my C# project.
  3. I added
    var libs = new[] {@"{PATH OF YOUR PROJECT}\Lib"};
    engine.SetSearchPaths(libs);
    

And that works for me.

like image 70
Ulises HR Avatar answered Mar 11 '26 06:03

Ulises HR



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!