Skip to content

Add the standard library to a .NET project

gerardojm edited this page Jul 10, 2026 · 8 revisions

ScriptEngine.SetSearchPaths returns sets the search paths used by the engine for loading files when a script wants to import or require another file of code.

Example

The structure of the project

C:\
 ├── Project\
 │    ├── Program.cs
 │    └── Scripts\
 │         └── utils.py
 │           

class Program
{
    static void Main()
    {
        // Create the IronPython engine
        ScriptEngine engine = Python.CreateEngine();

        var path = engine.GetSearchPaths(); // This method returns the search paths used by the engine for loading files

        path.Add(@"C:\main\Scripts"); // Add the path

       engine.SetSearchPaths(path); // Sets the search path
       engine.ExecuteFile(@"C:\Scripts\utils.py"); // Execute script 
    }
}

Adding multiple paths

If the modules are distributed in different locations:

C:\
 └── Project\
      └── Library\
          └── library.py

D:\
 └── PythonLibst\
      └── utils.py

F:\
 └── Scripts\
      └── maths.py

library.py:


import utils
import maths

print(utils.sayHello())
print(maths.addition(5, 8))

utils.py

def sayHello():
    return "Hello from Python"

maths.py

def addition(a, b):
    return a + b

C#

ScriptEngine engine = Python.CreateEngine();

var paths = new List<string>
{
   @"C:\Project\Library",
   @"D:\PythonLibst",
   @"F:\Scripts"
};


engine.SetSearchPaths(paths);

engine.ExecuteFile(@"C:\Project\Library\library.py");

IronPython will search for modules in this order:

  1. C:\Project\Library
  2. D:\PythonLibst
  3. F:\Scripts

Clone this wiki locally