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

44 views
Skip to first unread message

grbgooglefan

unread,
Dec 6, 2007, 9:27:15 PM12/6/07
to
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?

Gabriel Genellina

unread,
Dec 6, 2007, 11:17:15 PM12/6/07
to pytho...@python.org
En Thu, 06 Dec 2007 23:27:15 -0300, grbgooglefan <ganes...@gmail.com>
escribió:

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

--
Gabriel Genellina

grbgooglefan

unread,
Dec 6, 2007, 11:24:57 PM12/6/07
to
On Dec 7, 12:17 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
> En Thu, 06 Dec 2007 23:27:15 -0300, grbgooglefan <ganeshbo...@gmail.com>

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

Gabriel Genellina

unread,
Dec 7, 2007, 2:07:41 AM12/7/07
to pytho...@python.org
En Fri, 07 Dec 2007 01:24:57 -0300, grbgooglefan <ganes...@gmail.com>
escribió:

> On Dec 7, 12:17 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
> wrote:
>> En Thu, 06 Dec 2007 23:27:15 -0300, grbgooglefan <ganeshbo...@gmail.com>
>> 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

grbgooglefan

unread,
Dec 7, 2007, 3:03:11 AM12/7/07
to
On Dec 7, 3:07 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar> wrote:
> En Fri, 07 Dec 2007 01:24:57 -0300, grbgooglefan <ganeshbo...@gmail.com>
> Gabriel Genellina- Hide quoted text -
>
> - Show quoted text -

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

Virgil Dupras

unread,
Dec 7, 2007, 3:34:10 AM12/7/07
to

Yes. I use Python for .Net to embed python code in my applications,
and it works pretty well.

Christian Heimes

unread,
Dec 7, 2007, 4:12:23 AM12/7/07
to grbgooglefan, pytho...@python.org
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

unread,
Dec 7, 2007, 4:12:23 AM12/7/07
to pytho...@python.org, pytho...@python.org
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

sturlamolden

unread,
Dec 7, 2007, 5:27:51 AM12/7/07
to
On 7 Des, 08:07, "Gabriel Genellina" <gagsl-...@yahoo.com.ar> 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.


Christian Heimes

unread,
Dec 7, 2007, 6:07:07 AM12/7/07
to sturlamolden, pytho...@python.org
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

unread,
Dec 7, 2007, 6:07:07 AM12/7/07
to pytho...@python.org, pytho...@python.org
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

Reply all
Reply to author
Forward
0 new messages