[python-win32] Embedding python in a Win32 application

Ajay Kanade ajay.kanade at autodesk.com
Mon Oct 6 17:53:59 CEST 2014


Zachary,

Python embedding in another application (win32) is done quite a bit by many (me too). My suggestion to you is:
-	Build both release & debug
-	After the builds, do the "Install Python" steps at https://wiki.python.org/moin/VS2012 for *both* release & debug builds. Note: I had to modify manual_install.py because I did not build tcl (something like that)
-	If above step become too much, do a proper installation of python (.msi) & replace all .pyds & dlls & exes with your built ones
-	Idea is to keep both *.pyd and *_d.pyd (& *.dll & *_d.dll) in the same folders. This way both release & debug of your app will execute
-	Link release/debug python libs with your release/debug exe respectively
-	Take a look at http://stackoverflow.com/questions/1387906/c-with-python-embedding-crash-if-python-not-installed/2970407#2970407 to see how you can package python with your exe. Also it shows how you can set PythonHome in your C code
-	If you are going to create .pyds of your own (using swig or boost), note that you must have xyz.pyd & xyz_d.pyd for release & debug respectively
-	Python27.dll, Python27_d.dll & the Python27/DLLs & Python27/Lib are the only things needed at runtime

My suggestion for you would be to make it work for a pure python installation (you just have to build the python27.dll) before building the whole python thing yourself.

Hope it helps. I may have missed something. But that should give you an idea.

Ajay

-----Original Message-----
From: python-win32 [mailto:python-win32-bounces+ajay.kanade=autodesk.com at python.org] On Behalf Of Zachary Ware
Sent: Saturday, October 04, 2014 2:46 AM
To: python-win32 at python.org
Subject: Re: [python-win32] Embedding python in a Win32 application

On Fri, Oct 3, 2014 at 5:33 PM, Zachary Turner <zturner at google.com> wrote:
> Also, to answer your the question more specifically about sys.path, 
> when I run my debug build of python_d.exe from the commandl ine and 
> print sys.path I get this:
>
>>>> sys.path
> ['', 'D:\\python_src\\Python-2.7.8\\Lib',
> 'D:\\src\\llvm\\build\\ninja\\lib\\site-packages',
> 'd:\\python_src\\Python-2.7.8\\pcbuild\\python27_d.zip',
> 'd:\\python_src\\Python-2.7.8\\DLLs',
> 'd:\\python_src\\Python-2.7.8\\lib\\plat-win',
> 'd:\\python_src\\Python-2.7.8\\lib\\lib-tk',
> 'd:\\python_src\\Python-2.7.8\\pcbuild', 
> 'd:\\python_src\\Python-2.7.8', 
> 'd:\\python_src\\Python-2.7.8\\lib\\site-packages']
> [43489 refs]
>
> When I do the same thing from my embedded interpreter, I get this:
> ['D:\\src\\llvm\x08uild\ninja\x08in',
> 'D:\\src\\llvm\x08uild\ninja\x08in\\..\\lib\\site-packages',
> 'D:\\python_src\\Python-2.7.8\\Lib',
> 'D:\\src\\llvm\\build\\ninja\\lib\\site-packages',
> 'D:\\python_src\\Python-2.7.8\\PCbuild\\python27_d.zip',
> 'C:\\Python27\\Lib', 'C:\\Python27\\DLLs', 
> 'C:\\Python27\\Lib\\lib-tk', 'D:\\src\\llvm\\build\\ninja', 
> 'D:\\src\\llvm\\build\\ninja\\bin',
> 'C:\\Python27', 'C:\\Python27\\lib\\site-packages', '.']
>
> The first two junked up entries are bugs I need to fix, but not 
> relevant really to this.  It's worth noting that 
> d:\\python_src\\Python-2.7.8\\pcbuild is not included.  That's 
> probably where the problem lies.  Both of these were run with the same 
> system environment, so there's no difference there.  Someone running 
> python_d.exe gets d:\\python_src\\Python-2.7.8\\pcbuild in sys.path, 
> but linking against the DLL doesn't.

I will note that I haven't done any embedding of Python myself, so I'm shooting in the dark a little bit here.  From what I've read trying to figure this out (and from other background information picked up over the past few years), the default setting of sys.path is an unholy mess.  Some values are defaults based on the value of sys.prefix (or sys.exec_prefix, both of which are based on sys.executable, set by Py_SetProgramName), values can come from the environment (PYTHONPATH), and on Windows, values can even come from the registry (which is where
C:\Python27\* came from in your embedded interpreter).  sys.path is manipulated by Py_Initialize, PySys_SetArgv, and site.py (usually in
that order, I think).   And the rules also change on Windows when
python(_d).exe detects that it's in a build directory rather than an installed location.

I think your best solution here might be to just set sys.path yourself
(https://docs.python.org/2/c-api/sys.html#c.PySys_SetPath) just after Py_Initialize, overwriting the default values.  This also gives you the added benefit of being able to put the Python libraries (Lib/**.py and PCbuild/*.pyd) wherever you want them (even in the same folder, or even a zipfile), rather than trying to fit them into where Python expects them to be, and you don't run the risk of running Python code that really probably shouldn't have been findable anyway.

Hope this is of some help,
--
Zach
_______________________________________________
python-win32 mailing list
python-win32 at python.org
https://mail.python.org/mailman/listinfo/python-win32


More information about the python-win32 mailing list