[IronPython] Embedding IronPython 2.0

Michael Foord fuzzyman at voidspace.org.uk
Fri Jan 30 18:29:22 CET 2009


Severin wrote:
> Good point! I forgot that I was having the *.py files in a subdirectory.
>
> Is it also possible to set the 'current' directory for the engine?
>
> For instance I would like to have the engine living in a subfolder of 
> my project and then all path's would be relative to this subfolder.
>

Windows only has a concept of a current directory per process (actually 
technically per drive per process but never mind). Changing your current 
directory in your host app will change it for all contained engines... 
Any of your Python scripts changing the current directory will also 
change it for the host app.

For Python scripts in general (not just IronPython) it is much better to 
do relative path resolution yourself, relative to the current __file__ 
or sys.executable (which you are likely to need to set in a hosted 
environment).

All the best,

Michael



> thank you,
> Severin
>
> On Fri, Jan 30, 2009 at 4:56 PM, Michael Foord 
> <fuzzyman at voidspace.org.uk <mailto:fuzzyman at voidspace.org.uk>> wrote:
>
>     Severin wrote:
>
>         Hello,
>
>         I'm digging out this thread again.
>
>         I'm trying to run python scripts from C# using the DLR.
>
>         Simple examples work, namely if I have just one function in
>         one python file.
>
>         How does it work when one python file imports from another
>         python file? I get "No module named x" ImportExceptions when
>         trying to compile these python files.
>
>
>     If you're running them from '.py' source files then you aren't
>     compiling them - you're just executing them from an embedded
>     environment.
>
>     The import machinery should work exactly the same as for CPython,
>     but you may need to setup the initial sys.path depending on how
>     you are executing the scripts.
>
>     You probably need to use the engine.SetSearchPaths method passing
>     in an array with  the path to the directory containing the top
>     level Python file (adding any other directories that you need to
>     be able to import from).
>
>     HTH
>
>     Michael
>
>
>         I compile the main-file with 'CreateScriptSourceFromFile'.
>
>
>         May be I am using the wrong approach in general. What I have
>         is a bunch of python files that I used with CPython directly
>         so far. Now I want to extend my program with C# (since
>         database handling is much more convenient there) and I want to
>         use the python files that I already have.
>
>         What is the best approach for this scenario?
>
>         Thanks for any kind of hint.
>
>         Severin
>
>         On Tue, Jan 20, 2009 at 5:37 PM, Michael Foord
>         <fuzzyman at voidspace.org.uk <mailto:fuzzyman at voidspace.org.uk>
>         <mailto:fuzzyman at voidspace.org.uk
>         <mailto:fuzzyman at voidspace.org.uk>>> wrote:
>
>            Renaud Durand wrote:
>
>                Ok, Thank you.
>
>                Does anyone know where could I find an IronPython Assembly
>                documentation and/or up to date tutorials ?
>
>
>            These tutorials are up to date - except for the one
>         specified as
>            being for IP1:
>
>            http://www.voidspace.org.uk/ironpython/embedding.shtml
>
>            Michael
>
>
>                Thank you again.
>
>                2009/1/20 Curt Hagenlocher <curt at hagenlocher.org
>         <mailto:curt at hagenlocher.org>
>                <mailto:curt at hagenlocher.org
>         <mailto:curt at hagenlocher.org>> <mailto:curt at hagenlocher.org
>         <mailto:curt at hagenlocher.org>
>
>                <mailto:curt at hagenlocher.org
>         <mailto:curt at hagenlocher.org>>>>
>
>
>                   I believe this was valid for a long-ago alpha.  For
>         the 2.0
>                   release, you'll want to create an engine by saying
>                   engine = Python.CreateEngine()
>
>                   On Tue, Jan 20, 2009 at 7:32 AM, Renaud Durand
>                <renaud.durand.it <http://renaud.durand.it>
>         <http://renaud.durand.it>
>                   <http://renaud.durand.it>@gmail.com
>         <http://gmail.com> <http://gmail.com>
>                <http://gmail.com>> wrote:
>
>                       Hi,
>
>                       I want to use IronPython function through C#. To
>         do It, I
>                       found a tutorial at
>                      
>         http://www.ironpython.info/index.php/Hosting_IronPython_2.
>                       But the code does not seem to be updated for
>         IronPython
>                2.0.
>
>                       the code is :
>
>                       using System;
>                       using IronPython.Hosting;
>                       using IronPython.Runtime;
>                       using Microsoft.Scripting;
>                       using Microsoft.Scripting.Hosting;
>
>                       namespace EmbeddedCalculator
>                       {
>                           public class Engine
>                           {
>                               private ScriptEngine engine;
>
>                               public Engine()
>                               {
>                                   engine = PythonEngine.CurrentEngine;
>                               }
>
>                               public string calculate(string input)
>                               {
>                                   try
>                                   {
>                                       ScriptSource source =
>                       engine.CreateScriptSourceFromString(input, "py");
>                                       return source.Execute().ToString();
>                                   }
>                                   catch
>                                   {
>                                       return "Error";
>                                   }
>                               }
>
>                           }
>                       }
>                       // End of code
>
>                       When I try to compile it in visual studio, the
>         compiler
>                could
>                       not find the name "PythonEngine". I have added
>         all the
>                       references to
>                       the needed libraries. So, what am I missing ?
>
>
>                       Thank you :-).
>
>                       --        Renaud Durand
>
>                       _______________________________________________
>                       Users mailing list
>                       Users at lists.ironpython.com
>         <mailto:Users at lists.ironpython.com>
>                <mailto:Users at lists.ironpython.com
>         <mailto:Users at lists.ironpython.com>>
>                <mailto:Users at lists.ironpython.com
>         <mailto:Users at lists.ironpython.com>
>                <mailto:Users at lists.ironpython.com
>         <mailto:Users at lists.ironpython.com>>>
>
>                            
>         http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
>
>
>                   _______________________________________________
>                   Users mailing list
>                   Users at lists.ironpython.com
>         <mailto:Users at lists.ironpython.com>
>                <mailto:Users at lists.ironpython.com
>         <mailto:Users at lists.ironpython.com>>
>                <mailto:Users at lists.ironpython.com
>         <mailto:Users at lists.ironpython.com>
>                <mailto:Users at lists.ironpython.com
>         <mailto:Users at lists.ironpython.com>>>
>
>                  
>         http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
>
>
>
>                --        Renaud Durand
>              
>          ------------------------------------------------------------------------
>
>
>
>                _______________________________________________
>                Users mailing list
>                Users at lists.ironpython.com
>         <mailto:Users at lists.ironpython.com>
>         <mailto:Users at lists.ironpython.com
>         <mailto:Users at lists.ironpython.com>>
>              
>          http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>                
>
>
>            --    http://www.ironpythoninaction.com/
>            http://www.voidspace.org.uk/blog
>
>
>
>            _______________________________________________
>            Users mailing list
>            Users at lists.ironpython.com
>         <mailto:Users at lists.ironpython.com>
>         <mailto:Users at lists.ironpython.com
>         <mailto:Users at lists.ironpython.com>>
>            http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
>
>         ------------------------------------------------------------------------
>
>         _______________________________________________
>         Users mailing list
>         Users at lists.ironpython.com <mailto:Users at lists.ironpython.com>
>         http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>          
>
>
>     _______________________________________________
>     Users mailing list
>     Users at lists.ironpython.com <mailto:Users at lists.ironpython.com>
>     http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>   




More information about the Ironpython-users mailing list