Skip to content

Add the standard library to a .NET project

StΓ©phane Lozier edited this page Nov 6, 2026 · 9 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.

To ship the standard library with your application, or embed it in your assembly, see Embedding the standard library.

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