7

The following import works inside ipy.exe prompt but fails using IronPython ScriptRuntime inside a C# 4.0 program.

import ConfigParser

C# code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;

namespace CSharpDynamic
{
    class Program
    {
        static int Main(string[] args)
        {
            ScriptRuntime python = Python.CreateRuntime();
            dynamic dynamicIni =
python.UseFile(@"c:\test\WebCast\DynamicIni.py");

            return 0;
        }
    }
}

CPython uses PYTHONPATH environment variable. How do I configure this in IronPython when using ScriptRuntime?

1 Answer 1

14

You want to use GetSearchPaths and SetSearchPaths on your engine object. You could parse the env variable of your choice and populate the search path when you initialize your engine. For example:

var engine = Python.CreateEngine(DefaultEngineOptions());
var paths = engine.GetSearchPaths();
paths.Add("c:\\my_libs");
engine.SetSearchPaths(paths);
Sign up to request clarification or add additional context in comments.

1 Comment

The IronPython interpreter (ipe.exe) uses the IRONPYTHONPATH environment variable. It may be a good idea to read that into SetSearchPaths as well, depending on the application.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.