Topics

Ironpython 2.7.9 ImportException: 'No Module Named errno' when importing another module in .NET application

Stéphane Lozier
 

I think it was in lexer.py, there's something like:

try:
   eval("foo",...)
except SyntaxError:
   ...
else:
   import ...
   ...

The SyntaxError is thrown on the import. If you move the import into the try clause after the eval the exception will be caught and it'll work with IronPython.

Beyond that I have no idea if there are other incompatibilities since I've never used this module.

On Fri, Nov 30, 2018, 6:01 PM micoo via Groups.Io <micoo=microsoft.com@groups.io wrote:
Appears I misled us. The IronPython.Modules assembly was not getting copied to bin but not the directly the app was actually running from. 

I threw:
            Action<Type> noop = _ => { };
            noop(typeof(IronPython.Modules.CTypes));
 
at the top of the class and the assembly is loaded the and errno error is gone.

I'm able to execute scripts now pretty easily, and am hitting a Syntax Error when importing jinja2 like you mentioned:


Non-ASCII character '\xc2' in file C:\Python27\Lib\site-packages\jinja2\_identifier.py on line 2, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

Tried a number of searches and tweaks. Do you have more specific knowledge on what needs to be patched to enable jinja2? Does it require a specific verson? We're currently targeting 2.10

Thanks for your help! 

micoo@...
 

Appears I misled us. The IronPython.Modules assembly was not getting copied to bin but not the directly the app was actually running from. 

I threw:
            Action<Type> noop = _ => { };
            noop(typeof(IronPython.Modules.CTypes));
 
at the top of the class and the assembly is loaded the and errno error is gone.

I'm able to execute scripts now pretty easily, and am hitting a Syntax Error when importing jinja2 like you mentioned:


Non-ASCII character '\xc2' in file C:\Python27\Lib\site-packages\jinja2\_identifier.py on line 2, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

Tried a number of searches and tweaks. Do you have more specific knowledge on what needs to be patched to enable jinja2? Does it require a specific verson? We're currently targeting 2.10

Thanks for your help! 

Stéphane Lozier
 

I'm not sure why removing the site-packages from the search paths would have an effect on loading IronPython.Modules since it occurs in within Python.CreateEngine(). You can check that it gets loaded by adding something like:

Debug.Assert(AppDomain.CurrentDomain.GetAssemblies().Select(x => x.GetName().Name).Contains("IronPython.Modules"));

right after you create the engine.

Are you sure "import jinja2" was still failing with the ImportError and not something else? Note that jinja2 will fail with a SyntaxError if you try to import it from IronPython. You'll have to tweak lexer.py to get it working.

On Fri, Nov 30, 2018 at 12:40 PM micoo via Groups.Io <micoo=microsoft.com@groups.io> wrote:

Thanks for the reply. I did a little more exploring this morning and I think Stéphane is probably right, or at the very least on the right path.

If I remove the following line:

searchPaths.Add($@"{pythonRootPath}\Lib\site-packages\");

I can run the following script with no issues where previously I was unable to import either of these packages without an import error:
engine.Execute(@"
    import os, json
    print os.getcwd()
    print json.dumps({'x': 1, 'y': { 'hello': 'world' }, 'z': True}, indent=4)");

So if I can't add the path my package is in to sys path...how can I import it? I've tried fully qualifying the path as "C:\python27\Lib\site-packages\jinja2" and that doesn't work.

micoo@...
 

Thanks for the reply. I did a little more exploring this morning and I think Stéphane is probably right, or at the very least on the right path.

If I remove the following line:

searchPaths.Add($@"{pythonRootPath}\Lib\site-packages\");

I can run the following script with no issues where previously I was unable to import either of these packages without an import error:
engine.Execute(@"
    import os, json
    print os.getcwd()
    print json.dumps({'x': 1, 'y': { 'hello': 'world' }, 'z': True}, indent=4)");

So if I can't add the path my package is in to sys path...how can I import it? I've tried fully qualifying the path as "C:\python27\Lib\site-packages\jinja2" and that doesn't work.

Alex Earl
 

Also, are you adding the normal C Python site-packages to your path? You should definitely not do that, there are incompatibilities with the normal C Python stdlib. This is why we have an IronPython.StdLib package with modifications.


On Wed, Nov 28, 2018 at 2:35 PM Stéphane Lozier <stephane.lozier@...> wrote:
Sounds like IronPython.Modules isn't loaded. errno is a builtin module (in IronPython.Modules) so that is the error you would get if you tried to import os without it. Did you check that it gets copied to your bin folder?

On Sun, Nov 25, 2018 at 2:48 PM micoo via Groups.Io <micoo=microsoft.com@groups.io> wrote:
I originally asked this on SO and it got nothing, so hopefully this is a more direct way of getting some help.

I am attempting to run a simple python script within my .net application using IronPython. I have installed the nuget package into the project (I have NOT installed the ironpython cli on my machine) and have authored this code to handle setting paths, reading output, and setting input.
 
    this.engine = Python.CreateEngine();
    this.scope = this.engine.CreateScope();
 
    this.streamOut = new MemoryStream();
    this.streamErr = new MemoryStream();
    this.engine.Runtime.IO.SetOutput(this.streamOut, Encoding.Default);
    this.engine.Runtime.IO.SetErrorOutput(this.streamErr, Encoding.Default);
 
    // this is a locally set environment variable 
    string pythonRootPath = Environment.GetEnvironmentVariable("PythonPath");
    ICollection<string> searchPaths = engine.GetSearchPaths();
    searchPaths.Add($@"{pythonRootPath}\Lib\site-packages\");
    searchPaths.Add($@"{pythonRootPath}\Lib");
    engine.SetSearchPaths(searchPaths);
 
    this.engine.Execute(@"import jinja2");
 
Executing the script results in the following exception
 
    IronPython.Runtime.Exceptions.ImportException: 'No module named errno'
 
My C# project has references to the following relevant (to IronPython) assemblies:
 
 - IronPython (v2.7.9)
 - IronPython.Modules
 - IronPython.SQLite
 - IronPython.Wpf
 - Microsoft.Dynamic
 - Microsoft.Scripting
 - Microsoft.Scripting.Metadata
 
My paths appear to be well enough set for my purposes as it's not the import of jinja2 itself that causes the error and I am able to import some standard library modules. However, I've seen that the 'os' package fails to import for the same reason as jinja2. I also cannot import errno directly as expected, but I can import it directly if I run it via the python 2.7 cli so the package is installed.
 
I'm very much out of ideas here if anyone has any suggestions.



Stéphane Lozier
 

Sounds like IronPython.Modules isn't loaded. errno is a builtin module (in IronPython.Modules) so that is the error you would get if you tried to import os without it. Did you check that it gets copied to your bin folder?

On Sun, Nov 25, 2018 at 2:48 PM micoo via Groups.Io <micoo=microsoft.com@groups.io> wrote:
I originally asked this on SO and it got nothing, so hopefully this is a more direct way of getting some help.

I am attempting to run a simple python script within my .net application using IronPython. I have installed the nuget package into the project (I have NOT installed the ironpython cli on my machine) and have authored this code to handle setting paths, reading output, and setting input.
 
    this.engine = Python.CreateEngine();
    this.scope = this.engine.CreateScope();
 
    this.streamOut = new MemoryStream();
    this.streamErr = new MemoryStream();
    this.engine.Runtime.IO.SetOutput(this.streamOut, Encoding.Default);
    this.engine.Runtime.IO.SetErrorOutput(this.streamErr, Encoding.Default);
 
    // this is a locally set environment variable 
    string pythonRootPath = Environment.GetEnvironmentVariable("PythonPath");
    ICollection<string> searchPaths = engine.GetSearchPaths();
    searchPaths.Add($@"{pythonRootPath}\Lib\site-packages\");
    searchPaths.Add($@"{pythonRootPath}\Lib");
    engine.SetSearchPaths(searchPaths);
 
    this.engine.Execute(@"import jinja2");
 
Executing the script results in the following exception
 
    IronPython.Runtime.Exceptions.ImportException: 'No module named errno'
 
My C# project has references to the following relevant (to IronPython) assemblies:
 
 - IronPython (v2.7.9)
 - IronPython.Modules
 - IronPython.SQLite
 - IronPython.Wpf
 - Microsoft.Dynamic
 - Microsoft.Scripting
 - Microsoft.Scripting.Metadata
 
My paths appear to be well enough set for my purposes as it's not the import of jinja2 itself that causes the error and I am able to import some standard library modules. However, I've seen that the 'os' package fails to import for the same reason as jinja2. I also cannot import errno directly as expected, but I can import it directly if I run it via the python 2.7 cli so the package is installed.
 
I'm very much out of ideas here if anyone has any suggestions.

micoo@...
 

I originally asked this on SO and it got nothing, so hopefully this is a more direct way of getting some help.

I am attempting to run a simple python script within my .net application using IronPython. I have installed the nuget package into the project (I have NOT installed the ironpython cli on my machine) and have authored this code to handle setting paths, reading output, and setting input.
 
    this.engine = Python.CreateEngine();
    this.scope = this.engine.CreateScope();
 
    this.streamOut = new MemoryStream();
    this.streamErr = new MemoryStream();
    this.engine.Runtime.IO.SetOutput(this.streamOut, Encoding.Default);
    this.engine.Runtime.IO.SetErrorOutput(this.streamErr, Encoding.Default);
 
    // this is a locally set environment variable 
    string pythonRootPath = Environment.GetEnvironmentVariable("PythonPath");
    ICollection<string> searchPaths = engine.GetSearchPaths();
    searchPaths.Add($@"{pythonRootPath}\Lib\site-packages\");
    searchPaths.Add($@"{pythonRootPath}\Lib");
    engine.SetSearchPaths(searchPaths);
 
    this.engine.Execute(@"import jinja2");
 
Executing the script results in the following exception
 
    IronPython.Runtime.Exceptions.ImportException: 'No module named errno'
 
My C# project has references to the following relevant (to IronPython) assemblies:
 
 - IronPython (v2.7.9)
 - IronPython.Modules
 - IronPython.SQLite
 - IronPython.Wpf
 - Microsoft.Dynamic
 - Microsoft.Scripting
 - Microsoft.Scripting.Metadata
 
My paths appear to be well enough set for my purposes as it's not the import of jinja2 itself that causes the error and I am able to import some standard library modules. However, I've seen that the 'os' package fails to import for the same reason as jinja2. I also cannot import errno directly as expected, but I can import it directly if I run it via the python 2.7 cli so the package is installed.
 
I'm very much out of ideas here if anyone has any suggestions.