Skip to content

IronPython.Runtime.Exceptions.ImportException: "No module named #768

@ManfreddEE

Description

@ManfreddEE

Calling the specified script via C# is working,
but the python-command 'from x.y import *' fails.
With this command, there should be imported a user defined python-class or other defined functions in subfolders.

ActualFolder
->script1.py
->script2.py
->....
->Subfolder
->scriptSub1.py
->scriptSub2.py
->....

e.g. command in script1.py
from Subfolder.scriptSub1 import *;

Everything works fine, when i call the script via the 'cmd.exe' (all subscripts are imported in a working way).
It's also working fine, when i use a 'C#-Process'

Using IronPython (V2.7.11) in the following way:

// 1)
var engine = Python.CreateEngine();

// 2) provide scriptPath and arguments
var scriptPath = @"Path\to\file\fileName.py";
string dir = Path.GetDirectoryName(scriptPath);

// 3)
ICollection<string> paths = engine.GetSearchPaths();
if (!String.IsNullOrWhiteSpace(dir))
    paths.Add(dir);
else
    paths.Add(Environment.CurrentDirectory);

engine.SetSearchPaths(paths);
var source = engine.CreateScriptSourceFromFile(scriptPath);

// 4) Execute scriptPath
var scope = engine.CreateScope();
source.Execute(scope);

In "source.Execute(scope)" following exception: IronPython.Runtime.Exceptions.ImportException: "No module named Subfolder.scriptSub1" is thrown.

Thanks for suggested solutions

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions