473,699 Members | 2,223 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Embedding Python in Windows tutorial?

With some googling I have found these resources:

http://docs.python.org/ext/win-dlls.html
http://www.python.org/doc/faq/windows.html

I have a large Win32/MFC/C/C++ application that has an embedded scripting
language (a very limited one). I would like to rip it out and replace it
with Python. I am thinking that this would be relatively simple since the
scripting language is a very small interface between the UI and the
engine -- there would only be a handful functions I would need to wrap.

However in googling I haven't been able to come up with a completely
straightforward way to do it. I see all these caveats mentioned casually,
and not explained, which kind of scares me.

Examples:

1. I heard Python23.dll is built with MS VC++ 6. I am using .NET 2003 (VC++
7.1). Is this a problem? I wouldn't think so since I am not statically
linking... but someone said the C runtime is different between the two?
When would that be a problem?

2. Any sample VS.NET projects / boilerplate DLL code that loads
Python23.dll into the? Sure this is probably pretty simple -- add an
include paths, add a DLL dependency... but I'm sure there is at least one
gotcha and if I can avoid repeating other's mistakes, I'd prefer that.

3. How does C++ change the story? I assume at the least I have to extern
"C" any functions that are called by Python, right? What else?

4. The program in question has no installer -- it is an internal tool, and
people simply download the binaries .exe/.dll's/etc. from a source control
system. Does that present any problems? Is all they need is Python23.dll
in their system dir, which is installed if you install Python? But I am
going to need to write some Python wrappers for functions... does this mean
I need to use distutils and all that? One thing I didn't find clear is that
distutils will "install" a module on your own machine -- but what do you
need to do to get it onto other's machines? (e.g. non-engineers who do not
have compilers)

It seems like what I am doing is pretty much the "canonical" embedding
task -- scripting an engine of C/C++ code and also needing to access this
scripting through the GUI (i.e. so it is BOTH extending and embedding), so I
would like to know if there are some more complete Windows-specific
resources out there.

Also, any general advice on the process would be interesting. Out of all
the people who have done some embedding -- was it worth it? Were there a
lot of headaches? One of my concerns now is that it will decrease the
debuggability of the application. In a pure C/C++ app in VS.NET, it is
pretty simple to debug. But now I will have a layer of Python in between,
which could make it a big pain. In general this is kind of an "extra"
project at work that I don't want to spend a whole lot of time on. (It will
help "sell" python to a large group of very capable engineers, if that
motivates anyone to help me. : ) )

Thanks in advance for any help.

Roose
Jul 18 '05 #1
2 2866
Roose wrote:
1. I heard Python23.dll is built with MS VC++ 6. I am using .NET 2003 (VC++
7.1). Is this a problem? I wouldn't think so since I am not statically
linking... but someone said the C runtime is different between the two?
When would that be a problem?
- extensions compiled with .Net 2003 seem to run fine with Python 2.3
- I've heard that Python 2.4 will use .Net 2003 anyway
2. Any sample VS.NET projects / boilerplate DLL code that loads
Python23.dll into the? Sure this is probably pretty simple -- add an
include paths, add a DLL dependency... but I'm sure there is at least one
gotcha and if I can avoid repeating other's mistakes, I'd prefer that.
If you use the normal Python API, you link with the import lib
python23.lib, which will load python23.dll. If you put python23.dll into
the same directory as your application, everything should be fine.
4. The program in question has no installer -- it is an internal tool, and
people simply download the binaries .exe/.dll's/etc. from a source control
system. Does that present any problems? Is all they need is Python23.dll
in their system dir, which is installed if you install Python? But I am
I suggest that you distribute everything Python together with you
application. I found that switching to a new Python version is a hassle
when you have to tell everyone to do the upgrade.

- distribute python23.dll in the same directory as your application
- distribute the Python standard lib as a .zip archive
-- use the script compileall.py to generate the .pyc files
for the Python lib
-- use the -d and -f options to set the source directory
in the .pyc files. It is irritating if you get a Python traceback
and the filenames look plausible, but don't exist. Something like
"PYTHONROOT/os.py" makes it clear that the python files comes from
the distribution.
-- zip all the .pyc files into an archive (.py are optional)
-- add that .zip file to your distribution
-- add <python>/DLLs/zlib.pyd to your distribution (needed to import
modules from .zip files)
-- in your application: add that .zip file to the module search path,
either by changing the environment variable PYTHONPATH or by
manipulation sys.path through the Python API
-- you can start simple by packing only the .py files. This will work,
but it will be a bit slower as Python has to compile the files
for every run of your application

If you distribute Python together with your application, you should
occasionally test that you have everything included:
in the registry, rename
HKEY_LOCAL_MACH INE/Software/Python/PythonCore/2.3. That way, your
embedded Python won't be able to fall back on your installed Python.
going to need to write some Python wrappers for functions... does this mean
I need to use distutils and all that? One thing I didn't find clear is that
distutils will "install" a module on your own machine -- but what do you
need to do to get it onto other's machines? (e.g. non-engineers who do not
have compilers)
Your wrapper functions are part of your application. You make them known
to the Python runtime during your application, so there is no need to
install them via distutils.
One of my concerns now is that it will decrease the
debuggability of the application. In a pure C/C++ app in VS.NET, it is
pretty simple to debug. But now I will have a layer of Python in between,
which could make it a big pain. In general this is kind of an "extra"


You certainly shouldn't try to debug through the Python code. Set
breakpoints on all your extension functions. Once you reached Python,
use 'go' to go right through it.

You should also have the ability to report all exceptions in scripts
complete with backtraces. This will help you now with debugging and
later your customers with their own scripts.

Daniel
Jul 18 '05 #2
Thanks a lot for the answers -- they were quite helpful. Anyone else have
any links?

I still haven't gotten around to doing this, I might put it off since it
seems like more work than I would think... as I said I thought this was the
"canonical" thing to do and it would be pretty much a cut and paste job from
examples.

Does anyone else work with Python and C/C++ regularly? I thought there
would be more experts here... or are people not using Windows?

Not to be rude, but it is a little disheartening that everyone jumps all
over that "warehouse" thread rather than something a little more mundane but
utilitarian... : )

Roose

"Daniel Dittmar" <da************ @sap.corp> wrote in message
news:cl******** **@news.sap-ag.de...
Roose wrote:
1. I heard Python23.dll is built with MS VC++ 6. I am using .NET 2003 (VC++ 7.1). Is this a problem? I wouldn't think so since I am not statically
linking... but someone said the C runtime is different between the two?
When would that be a problem?


- extensions compiled with .Net 2003 seem to run fine with Python 2.3
- I've heard that Python 2.4 will use .Net 2003 anyway
2. Any sample VS.NET projects / boilerplate DLL code that loads
Python23.dll into the? Sure this is probably pretty simple -- add an
include paths, add a DLL dependency... but I'm sure there is at least one gotcha and if I can avoid repeating other's mistakes, I'd prefer that.


If you use the normal Python API, you link with the import lib
python23.lib, which will load python23.dll. If you put python23.dll into
the same directory as your application, everything should be fine.
4. The program in question has no installer -- it is an internal tool, and people simply download the binaries .exe/.dll's/etc. from a source control system. Does that present any problems? Is all they need is Python23.dll in their system dir, which is installed if you install Python? But I am


I suggest that you distribute everything Python together with you
application. I found that switching to a new Python version is a hassle
when you have to tell everyone to do the upgrade.

- distribute python23.dll in the same directory as your application
- distribute the Python standard lib as a .zip archive
-- use the script compileall.py to generate the .pyc files
for the Python lib
-- use the -d and -f options to set the source directory
in the .pyc files. It is irritating if you get a Python traceback
and the filenames look plausible, but don't exist. Something like
"PYTHONROOT/os.py" makes it clear that the python files comes from
the distribution.
-- zip all the .pyc files into an archive (.py are optional)
-- add that .zip file to your distribution
-- add <python>/DLLs/zlib.pyd to your distribution (needed to import
modules from .zip files)
-- in your application: add that .zip file to the module search path,
either by changing the environment variable PYTHONPATH or by
manipulation sys.path through the Python API
-- you can start simple by packing only the .py files. This will work,
but it will be a bit slower as Python has to compile the files
for every run of your application

If you distribute Python together with your application, you should
occasionally test that you have everything included:
in the registry, rename
HKEY_LOCAL_MACH INE/Software/Python/PythonCore/2.3. That way, your
embedded Python won't be able to fall back on your installed Python.
going to need to write some Python wrappers for functions... does this mean I need to use distutils and all that? One thing I didn't find clear is that distutils will "install" a module on your own machine -- but what do you
need to do to get it onto other's machines? (e.g. non-engineers who do not have compilers)


Your wrapper functions are part of your application. You make them known
to the Python runtime during your application, so there is no need to
install them via distutils.
> One of my concerns now is that it will decrease the
debuggability of the application. In a pure C/C++ app in VS.NET, it is
pretty simple to debug. But now I will have a layer of Python in between, which could make it a big pain. In general this is kind of an "extra"


You certainly shouldn't try to debug through the Python code. Set
breakpoints on all your extension functions. Once you reached Python,
use 'go' to go right through it.

You should also have the ability to report all exceptions in scripts
complete with backtraces. This will help you now with debugging and
later your customers with their own scripts.

Daniel

Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2785
by: Alicia Haumann | last post by:
I accidentally sent this to webmaster@python.org, so this could be a duplicate if "webmaster" forwards it to this list. :{ Hi, there. Thanks for any help that can be offered. I've been working with Python for a year or more now, but only doing simple extending in C/C++. I'm now attempting some embedding and several questions have come to mind. BTW - I'm running Windows 2000 with Python23 and VisualC++ developers
1
1912
by: amit | last post by:
Hello, I am currently studying how to embedd python. I am developing a graphical C++ application. My goal is to embedd python script that will control some kind of animation. I have some questions about python embedding: 1) Is there a good text book or other resource on embedding/extending? (I find it hard to learn only by the tutorial and C/Python API from the python.org site)
8
1823
by: Thomas Bartkus | last post by:
Name: lib64python2.4-devel Summary: The libraries and header files needed for Python development Description: The Python programming language's interpreter can be extended with dynamically loaded extensions and can be embedded in other programs. This package contains the header files and libraries needed to do these types of tasks. ------------------------------------------------------
3
2050
by: Marco Meoni | last post by:
Hi all! I've a problem with a C++ class that has to be included in a python application. One way to do it is Extending and Embedding the Python Interpreter Now i have 2 questions 1) Is there a one-file version of this tutorial? 2) Is there anyone that can help me with this problem? The class is attached. Thanks all. Marco
2
1985
by: John Dean | last post by:
Hi Could somebody, please tell me where I can find information about embedding Python into a C/C++ application. The example in the docs is rather simple. I am looking for something a bit more complex and longer -- Best Regards John
1
3079
by: freesteel | last post by:
I have posted about this problem before. SInce then I found a much better article to help with embedding python in a multithreaded application: http://www.linuxjournal.com/article/3641 I found this article very good and it clarified for me what needs doing. Now I have an example application that almost works, but it is far from reliable. If I run this several times I get crashes telling me that the heap is modified after deallocation....
6
3001
by: Qun Cao | last post by:
Hi Everyone, I am a beginner on cross language development. My problem at hand is to build a python interface for a C++ application built on top of a 3D game engine. The purpose of this python interface is providing a convenient scripting toolkit for the application. One example is that a user can write a python script like: player = Player() game.loadPlayer(player) player.moveTo(location)
4
5685
by: David Abrahams | last post by:
I'm seeing highly surprising (and different!) behaviors of PyImport_ImportModule on Linux and Windows when used in a program with python embedding. On Linux, when attempting to import a module xxx that's in the current directory, I get ImportError: No module named xxx I can work around the problem by setting
0
1982
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 385 open (+21) / 3790 closed (+21) / 4175 total (+42) Bugs : 1029 open (+43) / 6744 closed (+43) / 7773 total (+86) RFE : 262 open ( +4) / 291 closed ( +4) / 553 total ( +8) New / Reopened Patches ______________________
0
8623
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9184
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9048
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8892
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6539
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5878
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4386
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3064
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2359
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.