[IronPython] from __future__ import ...

Lukas Cenovsky cenovsky at bakalari.cz
Mon Dec 14 08:08:32 PST 2009


I've found the catch - the __future__.py module is in ironpython\lib 
folder which is automatically added to path by ipy.exe - that's why it 
worked from interpreter but not from the ScriptEngine.

--
-- Lukáš

Michael Foord wrote:
> __future__ is a standard library module that must be on the path in 
> order to do future imports. You must put the directory containing this 
> module (or preferably all the standard library) into sys.path before 
> importing it.
>
> The ScriptRuntime or ScriptEngine has a SetSearchPaths method that 
> allows you to do this from C#.
>
> All the best,
>
> Michael Foord
>
> On 14/12/2009 10:18, Lukas Cenovsky wrote:
>> Hi,
>> I've found that the from __future__ import ... works in .py file run 
>> by ipy.exe but not when I create an engine in C#.
>>
>> test.py:
>>
>> from __future__ import division
>> print 'done'
>>
>> test.cs:
>>
>> using Microsoft.Scripting;
>> using Microsoft.Scripting.Hosting;
>> using IronPython.Hosting;
>>
>> namespace TestDuture
>> {
>>    public class Test
>>    {
>>        static void Main(string[] args)
>>        {
>>            ScriptEngine engine = Python.CreateEngine();
>>            ScriptRuntime runtime = engine.Runtime;
>>            ScriptScope scope = runtime.CreateScope();
>>            ScriptSource source = 
>> engine.CreateScriptSourceFromFile("test.py");
>>            source.Execute(scope);
>>        }
>>    }
>> }
>>
>> build command:
>>
>> csc /r:Microsoft.Dynamic.dll /r:Microsoft.Scripting.core.dll 
>> /r:Microsoft.Scripting.dll /r:IronPython.dll 
>> /r:IronPython.Modules.dll test.cs
>>
>> Running test.exe throws exception:
>>
>> C:\Users\dev\Documents\FutureTest>test.exe
>>
>> Unhandled Exception: IronPython.Runtime.Exceptions.ImportException: 
>> No module named __future__
>>   at 
>> Microsoft.Scripting.Actions.Calls.MethodCandidate.Caller.Call(Object[] 
>> args, Boolean& shouldOptimize)
>>   at 
>> IronPython.Runtime.Types.BuiltinFunction.BuiltinFunctionCaller`5.Call4(CallSite 
>> site, CodeContext context, TFuncType func, T0 arg0, T1 arg1, T2 arg2, 
>> T3 arg3)
>>   at 
>> System.Dynamic.UpdateDelegates.UpdateAndExecute6[T0,T1,T2,T3,T4,T5,TRet](CallSite 
>> site, T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5)
>>   at IronPython.Runtime.Importer.Import(CodeContext context, String 
>> fullName, PythonTuple from, Int32 level)
>>   at 
>> IronPython.Runtime.Operations.PythonOps.ImportWithNames(CodeContext 
>> context, String fullName, String[] names, Int32 level)
>>   at Microsoft.Scripting.Utils.InvokeHelper`5.Invoke(Object arg0, 
>> Object arg1, Object arg2, Object arg3)
>>   at Microsoft.Scripting.Utils.ReflectedCaller.Invoke(Object[] args)
>>   at 
>> Microsoft.Scripting.Interpreter.CallInstruction.Run(InterpretedFrame 
>> frame)
>>   at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame 
>> frame)
>>   at Microsoft.Scripting.Interpreter.LightLambda.Run2[T0,T1,TRet](T0 
>> arg0, T1 arg1)
>>   at IronPython.Compiler.PythonScriptCode.RunWorker(CodeContext ctx)
>>   at IronPython.Compiler.PythonScriptCode.Run(Scope scope)
>>   at IronPython.Compiler.RuntimeScriptCode.InvokeTarget(Scope scope)
>>   at IronPython.Compiler.RuntimeScriptCode.Run(Scope scope)
>>   at Microsoft.Scripting.SourceUnit.Execute(Scope scope, ErrorSink 
>> errorSink)
>>   at Microsoft.Scripting.Hosting.ScriptSource.Execute(ScriptScope scope)
>>   at TestDuture.Test.Main(String[] args)
>>
>> Am I missing something? It works ok when I add __future__.py from 
>> Python 2.6.
>>
>> -- 
>> -- Lukáš
>>
>> _______________________________________________
>> Users mailing list
>> Users at lists.ironpython.com
>> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
>




More information about the Users mailing list