Can I embed Windows Python in C# or VC++?

Discussion in 'Python' started by grbgooglefan, Dec 7, 2007.

  1. grbgooglefan

    grbgooglefan Guest

    I want to use Python's Windows (python25.dll) version to embed in my
    C# (or atleast VC++) program for performing syntax checks on the
    Python expressions which are later supposed to be evaluated at runtime
    by another C++ program

    For this, I would like to use CPython API functions such as
    Py_Initialize, PyModule_New, PyModule_GetDict, PyRun_String,
    PyObject_GetAttrString, PyObject_CallObject, PyTuple_SetItem & other
    similar functions from my C#/ VC++ program on Windows.

    I have installed Python251.msi on my Desktop.
    Can I start doing the development using the include, lib & the
    python25.dll files availale after installing this MSI?
     
    grbgooglefan, Dec 7, 2007
    #1
    1. Advertising

  2. En Thu, 06 Dec 2007 23:27:15 -0300, grbgooglefan <>
    escribió:

    > I want to use Python's Windows (python25.dll) version to embed in my
    > C# (or atleast VC++) program for performing syntax checks on the
    > Python expressions which are later supposed to be evaluated at runtime
    > by another C++ program
    >
    > For this, I would like to use CPython API functions such as
    > Py_Initialize, PyModule_New, PyModule_GetDict, PyRun_String,
    > PyObject_GetAttrString, PyObject_CallObject, PyTuple_SetItem & other
    > similar functions from my C#/ VC++ program on Windows.
    >
    > I have installed Python251.msi on my Desktop.
    > Can I start doing the development using the include, lib & the
    > python25.dll files availale after installing this MSI?


    Yes. You don't require the source package to embed Python and use the API
    in your programs.

    --
    Gabriel Genellina
     
    Gabriel Genellina, Dec 7, 2007
    #2
    1. Advertising

  3. grbgooglefan

    grbgooglefan Guest

    On Dec 7, 12:17 pm, "Gabriel Genellina" <>
    wrote:
    > En Thu, 06 Dec 2007 23:27:15 -0300, grbgooglefan <>
    > escribió:
    >
    > > I want to use Python's Windows (python25.dll) version to embed in my
    > > C# (or atleast VC++) program for performing syntax checks on the
    > > Python expressions which are later supposed to be evaluated at runtime
    > > by another C++ program

    >
    > > For this, I would like to use CPython API functions such as
    > > Py_Initialize, PyModule_New, PyModule_GetDict, PyRun_String,
    > > PyObject_GetAttrString, PyObject_CallObject, PyTuple_SetItem & other
    > > similar functions from my C#/ VC++ program on Windows.

    >
    > > I have installed Python251.msi on my Desktop.
    > > Can I start doing the development using the include, lib & the
    > > python25.dll files availale after installing this MSI?

    >
    > Yes. You don't require the source package to embed Python and use the API
    > in your programs.
    >
    > --
    > Gabriel Genellina


    Does it mean, I can embed Python in C# as well with the same APIs?
     
    grbgooglefan, Dec 7, 2007
    #3
  4. En Fri, 07 Dec 2007 01:24:57 -0300, grbgooglefan <>
    escribió:

    > On Dec 7, 12:17 pm, "Gabriel Genellina" <>
    > wrote:
    >> En Thu, 06 Dec 2007 23:27:15 -0300, grbgooglefan <>
    >> escribió:
    >>
    >> > I want to use Python's Windows (python25.dll) version to embed in my
    >> > C# (or atleast VC++) program for performing syntax checks on the
    >> > Python expressions which are later supposed to be evaluated at runtime
    >> > by another C++ program [...]> Can I start doing the development using

    >> the include, lib & the
    >> > python25.dll files availale after installing this MSI?

    >>
    >> Yes. You don't require the source package to embed Python and use the
    >> API in your programs.

    >
    > Does it mean, I can embed Python in C# as well with the same APIs?


    No; you can use the Python API in a native C++ application (the Python
    code is plain C, but all the include files have the 'extern "C" {}'
    declarations). For .NET there are IronPython and PythonNet, but I cannot
    comment on them, surely someone else may help. See
    http://www.python.org/about/

    --
    Gabriel Genellina
     
    Gabriel Genellina, Dec 7, 2007
    #4
  5. grbgooglefan

    grbgooglefan Guest

    On Dec 7, 3:07 pm, "Gabriel Genellina" <> wrote:
    > En Fri, 07 Dec 2007 01:24:57 -0300, grbgooglefan <>
    > escribió:
    >
    >
    >
    >
    >
    > > On Dec 7, 12:17 pm, "Gabriel Genellina" <>
    > > wrote:
    > >> En Thu, 06 Dec 2007 23:27:15 -0300, grbgooglefan <>
    > >> escribió:

    >
    > >> > I want to use Python's Windows (python25.dll) version to embed in my
    > >> > C# (or atleast VC++) program for performing syntax checks on the
    > >> > Python expressions which are later supposed to be evaluated at runtime
    > >> > by another C++ program [...]> Can I start doing the development using
    > >> the include, lib & the
    > >> > python25.dll files availale after installing this MSI?

    >
    > >> Yes. You don't require the source package to embed Python and use the
    > >> API in your programs.

    >
    > > Does it mean, I can embed Python in C# as well with the same APIs?

    >
    > No; you can use the Python API in a native C++ application (the Python
    > code is plain C, but all the include files have the 'extern "C" {}'
    > declarations). For .NET there are IronPython and PythonNet, but I cannot
    > comment on them, surely someone else may help. See http://www.python.org/about/
    >
    > --
    > Gabriel Genellina- Hide quoted text -
    >
    > - Show quoted text -


    Hello, Anybody else out there has used Python from C#?
     
    grbgooglefan, Dec 7, 2007
    #5
  6. On Dec 7, 9:03 am, grbgooglefan <> wrote:
    > On Dec 7, 3:07 pm, "Gabriel Genellina" <> wrote:
    >
    >
    >
    > > En Fri, 07 Dec 2007 01:24:57 -0300, grbgooglefan <>
    > > escribió:

    >
    > > > On Dec 7, 12:17 pm, "Gabriel Genellina" <>
    > > > wrote:
    > > >> En Thu, 06 Dec 2007 23:27:15 -0300, grbgooglefan <>
    > > >> escribió:

    >
    > > >> > I want to use Python's Windows (python25.dll) version to embed in my
    > > >> > C# (or atleast VC++) program for performing syntax checks on the
    > > >> > Python expressions which are later supposed to be evaluated at runtime
    > > >> > by another C++ program [...]> Can I start doing the development using
    > > >> the include, lib & the
    > > >> > python25.dll files availale after installing this MSI?

    >
    > > >> Yes. You don't require the source package to embed Python and use the
    > > >> API in your programs.

    >
    > > > Does it mean, I can embed Python in C# as well with the same APIs?

    >
    > > No; you can use the Python API in a native C++ application (the Python
    > > code is plain C, but all the include files have the 'extern "C" {}'
    > > declarations). For .NET there are IronPython and PythonNet, but I cannot
    > > comment on them, surely someone else may help. See http://www.python.org/about/

    >
    > > --
    > > Gabriel Genellina- Hide quoted text -

    >
    > > - Show quoted text -

    >
    > Hello, Anybody else out there has used Python from C#?


    Yes. I use Python for .Net to embed python code in my applications,
    and it works pretty well.
     
    Virgil Dupras, Dec 7, 2007
    #6
  7. grbgooglefan wrote:
    > I want to use Python's Windows (python25.dll) version to embed in my
    > C# (or atleast VC++) program for performing syntax checks on the
    > Python expressions which are later supposed to be evaluated at runtime
    > by another C++ program
    >
    > For this, I would like to use CPython API functions such as
    > Py_Initialize, PyModule_New, PyModule_GetDict, PyRun_String,
    > PyObject_GetAttrString, PyObject_CallObject, PyTuple_SetItem & other
    > similar functions from my C#/ VC++ program on Windows.


    It's not a matter of C++ or C## but a matter of managed or unmanaged
    language. Nowadays VS C++ can create old style binaries and CLR. For C++
    you can use the MSI but I recommend a source installation. It makes
    debugging easier. You may want to port my PCbuild9 directory from svn
    trunk (Python 2.6) to Python 2.5 if you like to use VS 2008.

    For CLR/.NET (Managed C++, C#, VB, ...) you need the PythonDotNET
    bindings from http://pythonnet.sf.net/. Brian (the creator of
    PythonDotNET) and I are very busy in the last weeks and months. The last
    preview release is outdated. Please check out the latest version from
    PythonDotNET's SVN.

    The support for unmanaged C++ is better than the support for managed
    ..NET code.

    Christian
     
    Christian Heimes, Dec 7, 2007
    #7
  8. grbgooglefan wrote:
    > I want to use Python's Windows (python25.dll) version to embed in my
    > C# (or atleast VC++) program for performing syntax checks on the
    > Python expressions which are later supposed to be evaluated at runtime
    > by another C++ program
    >
    > For this, I would like to use CPython API functions such as
    > Py_Initialize, PyModule_New, PyModule_GetDict, PyRun_String,
    > PyObject_GetAttrString, PyObject_CallObject, PyTuple_SetItem & other
    > similar functions from my C#/ VC++ program on Windows.


    It's not a matter of C++ or C## but a matter of managed or unmanaged
    language. Nowadays VS C++ can create old style binaries and CLR. For C++
    you can use the MSI but I recommend a source installation. It makes
    debugging easier. You may want to port my PCbuild9 directory from svn
    trunk (Python 2.6) to Python 2.5 if you like to use VS 2008.

    For CLR/.NET (Managed C++, C#, VB, ...) you need the PythonDotNET
    bindings from http://pythonnet.sf.net/. Brian (the creator of
    PythonDotNET) and I are very busy in the last weeks and months. The last
    preview release is outdated. Please check out the latest version from
    PythonDotNET's SVN.

    The support for unmanaged C++ is better than the support for managed
    ..NET code.

    Christian
     
    Christian Heimes, Dec 7, 2007
    #8
  9. grbgooglefan

    sturlamolden Guest

    On 7 Des, 08:07, "Gabriel Genellina" <> wrote:

    > > Does it mean, I can embed Python in C# as well with the same APIs?

    >
    > No; you can use the Python API in a native C++ application (the Python
    > code is plain C, but all the include files have the 'extern "C" {}'
    > declarations). For .NET there are IronPython and PythonNet, but I cannot
    > comment on them, surely someone else may help. See http://www.python.org/about/


    The answer is YES. C# can access C functions exported by any DLL with
    platform invoke. Since the Python C API is plain C and not C++ you can
    gain access to it from C#. Import System.Runtime.InteropServices and
    write wrappers like

    [DllImport("Python25.dll"), CallingConvention=CallingConvention.Cdecl]
    public static void Py_Initialize();

    etc.


    You can also combine C++ and C# in a project in MS Visual Studio, if
    you prefer to access Python from C++. That way you don't have to write
    platform invoke wrappers for the Python C API.
     
    sturlamolden, Dec 7, 2007
    #9
  10. sturlamolden wrote:
    > The answer is YES. C# can access C functions exported by any DLL with
    > platform invoke. Since the Python C API is plain C and not C++ you can
    > gain access to it from C#. Import System.Runtime.InteropServices and
    > write wrappers like
    >
    > [DllImport("Python25.dll"), CallingConvention=CallingConvention.Cdecl]
    > public static void Py_Initialize();


    There is no need for that. PythonDotNET wraps the P/Invokes and internal
    C API of CPython in a nice .NET API.

    Christian
     
    Christian Heimes, Dec 7, 2007
    #10
  11. sturlamolden wrote:
    > The answer is YES. C# can access C functions exported by any DLL with
    > platform invoke. Since the Python C API is plain C and not C++ you can
    > gain access to it from C#. Import System.Runtime.InteropServices and
    > write wrappers like
    >
    > [DllImport("Python25.dll"), CallingConvention=CallingConvention.Cdecl]
    > public static void Py_Initialize();


    There is no need for that. PythonDotNET wraps the P/Invokes and internal
    C API of CPython in a nice .NET API.

    Christian
     
    Christian Heimes, Dec 7, 2007
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mehr H
    Replies:
    1
    Views:
    620
    Chris Jackson
    Nov 6, 2003
  2. Replies:
    2
    Views:
    7,879
  3. Peter

    windows app embed java app

    Peter, Feb 22, 2005, in forum: Java
    Replies:
    0
    Views:
    381
    Peter
    Feb 22, 2005
  4. Arjun Narayanan
    Replies:
    4
    Views:
    555
    sturlamolden
    May 20, 2007
  5. richard
    Replies:
    8
    Views:
    405
    Bergamot
    Nov 13, 2008
Loading...

Share This Page