[python-win32] Embedding python in a Win32 application

Zachary Turner zturner at google.com
Fri Oct 3 22:45:17 CEST 2014


Apologies if this message appears incorrectly threaded.  Initially I didn't
subscribe to the list, so I'm replying to my own post instead of Zachary
Ware's.  Will do my best to keep the quoting intact though:

On Fri, Oct 3, 2014 at 1:02 PM, Zachary Ware
<zachary.ware+pydev at gmail.com> wrote:
>> On Fri, Oct 3, 2014 at 12:07 PM, Zachary Turner <zturner at google.com> wrote:

>> Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.
>>>> import ctypes
>> Traceback (most recent call last):
>>   File "<console>", line 1, in <module>
>>   File "d:\python_src\Python-2.7.8\Lib\ctypes\__init__.py", line 10, in
>> <module>
>>     from _ctypes import Union, Structure, Array
>> ImportError: No module named _ctypes
>>
>> Anybody have any idea what is going on here or how I might diagnose this?
>> It's obviously located the ctypes module that came with the source
>> distribution, so what error could it be running into?
>
> What do you get if you do 'import sys; print sys.path'?  Is the
> directory that holds _ctypes.pyd included in sys.path?
>
>> For reference, the way I've built this is to download Python 2.7 source
>> distribution, open up the solution in Visual Studio, and build it, then have
>> my application link against the binaries that are put into the build's
>> output directories.  Is there a different way?
>
> That ought to be about right.

I did some more digging, and I think it's because ctypes is built as an
extension module, so _ctypes_d.pyd is in my build output directory
alongside python_d.exe and python27_d.dll, and not in my libs directory.  I
managed to get it working if I add my build output directory to my
PYTHONPATH.  Is there some way to package up my build and then install it
so that the directory structure is the same as if I had downloaded a
pre-packaged installer and installed it?  I don't think PYTHONPATH is set
by a default install, so something about the way it installs itself (maybe
python.exe is hardcoded to look in the DLLs subfolder?) allows an installed
build of python to work without PYTHONPATH.  I'd like to achieve the same
thing with my custom build.


On Fri, Oct 3, 2014 at 10:07 AM, Zachary Turner <zturner at google.com> wrote:

> Hi,
>
> I'm trying to embed python 2.7 in a Win32 application and I'm running into
> a host of problems.
>
> Originally, my problem was that when compiling a debug version of my
> application and linking against the python27.lib that I installed via the
> packaged installer, I would just get strange behavior and memory corruption
> and random crashes.  I finally determined that this was basically just
> impossible, and if I want to use embedded python in a debug version of my
> application, I need to build my own debug version of python.
>
> This leads me to my first question: Was my original determination
> accurate?  I think all of my problems would be solved automatically if I
> could just embed release python in a debug version of my application.
>
>
> Fast forward a bit.  I now have a custom debug build of python.  My debug
> build of application links against it, and release build links against
> release python.  So far so good, until I try to run "import ctypes".  Then
> I get this:
>
> Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.
> >>> import ctypes
> Traceback (most recent call last):
>   File "<console>", line 1, in <module>
>   File "d:\python_src\Python-2.7.8\Lib\ctypes\__init__.py", line 10, in
> <module>
>     from _ctypes import Union, Structure, Array
> ImportError: No module named _ctypes
>
> Anybody have any idea what is going on here or how I might diagnose this?
> It's obviously located the ctypes module that came with the source
> distribution, so what error could it be running into?
>
> For reference, the way I've built this is to download Python 2.7 source
> distribution, open up the solution in Visual Studio, and build it, then
> have my application link against the binaries that are put into the build's
> output directories.  Is there a different way?
>
> Thanks
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-win32/attachments/20141003/d45739c5/attachment-0001.html>


More information about the python-win32 mailing list