472,787 Members | 1,471 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,787 software developers and data experts.

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 2788
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_MACHINE/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_MACHINE/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
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...
1
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...
8
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...
3
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...
2
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...
1
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...
6
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...
4
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...
0
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...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

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.