-
Notifications
You must be signed in to change notification settings - Fork 314
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.
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
}
}
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:
- C:\Project\Library
- D:\PythonLibst
- F:\Scripts
Still looking for more? Browse the Discussions tab, where you can ask questions to the IronPython community.
🐍 IronPython