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

G

grbgooglefan

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?
 
G

Gabriel Genellina

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.
 
G

grbgooglefan

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?
 
G

Gabriel Genellina

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/
 
G

grbgooglefan

En Fri, 07 Dec 2007 01:24:57 -0300, grbgooglefan <[email protected]>
escribió:




En Thu, 06 Dec 2007 23:27:15 -0300, grbgooglefan <[email protected]>
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/

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

Virgil Dupras

En Fri, 07 Dec 2007 01:24:57 -0300, grbgooglefan <[email protected]>
escribió:
On Dec 7, 12:17 pm, "Gabriel Genellina" <[email protected]>
wrote:
En Thu, 06 Dec 2007 23:27:15 -0300, grbgooglefan <[email protected]>
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/
- 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.
 
C

Christian Heimes

grbgooglefan said:
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
 
C

Christian Heimes

grbgooglefan said:
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
 
S

sturlamolden

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.
 
C

Christian Heimes

sturlamolden said:
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
 
C

Christian Heimes

sturlamolden said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top