can't find win32api from embedded pyrun call

J

Jim

I am trying to figure out how to embed Python in a little C++ Windows
console app. Here's the code:

// Py_Initialize();
Py_InitializeEx(0);
PyRun_SimpleString("from win32com.client import *");

Here's what it does on the last line:

File "D:\Python\Lib\site-packages\win32com\__init__.py", line 5, in ?
import win32api, sys, ok
ImportError: No module named win32api

The same line runs fine in IDLE or at the command prompt. It also runs
without complaint if I run a release version of the app. To build the
debug version I had to build python42_d.lib/dll myself. The reason I
can't call PyInitialize is that it crashes with an "invalid signal"
error.

I don't think it's a path issue -- I've checked system path, sys.path,
pythonpath env var, and the pythonpath entry in the registry, all of
them loaded with directories including the one with win32api.pyd.
Besides, if it were an install or path problem, why would it work at
the command prompt?

Could it be a problem with the debug lib I built? Any suggestions are
welcome.

-- Jim
 
M

Miki

Hello Jim,
// Py_Initialize();
Py_InitializeEx(0);
PyRun_SimpleString("from win32com.client import *");

Here's what it does on the last line:

File "D:\Python\Lib\site-packages\win32com\__init__.py", line 5, in ?
import win32api, sys, ok
ImportError: No module named win32api

The same line runs fine in IDLE or at the command prompt. It also runs
without complaint if I run a release version of the app. To build the
debug version I had to build python42_d.lib/dll myself. The reason I
can't call PyInitialize is that it crashes with an "invalid signal"
error.

I don't think it's a path issue -- I've checked system path, sys.path,
pythonpath env var, and the pythonpath entry in the registry, all of
them loaded with directories including the one with win32api.pyd.
Besides, if it were an install or path problem, why would it work at
the command prompt?
IIRC you need to set the path explicitly in an embedded interpreter.
See the code in "site.py" (which again IMO is *not* imported when the
interpreter starts).

HTH,
Miki
http://pythonwise.blogspot.com/
 
J

Jim

Miki said:
IIRC you need to set the path explicitly in an embedded interpreter.
See the code in "site.py" (which again IMO is *not* imported when the
interpreter starts).

Thanks Miki. Actually that doesn't turn out to be the problem. If I
display sys.path within the embedded script, it shows the complete list
of paths, there is no need to pre-load.

The problem turned out to be a mismatch of parts. For one thing,
building the debug lib from Python sources using VS2005 leads to the
crash in PyInitialize (I don't know why others haven't run into this);
building with VS2003 did away with that. But that then led to the
well-known problem with a FILE* where the app is using one msvcrt and
the lib another. At some point in fixing these issues, the "cannot
find win32api" went away.

I now use this trick from a colleague which allows me to build a debug
version of my app but load the release version of the lib: bracket the
"#include python.h" statement with undef/redef of _DEBUG.

And this trick to get around the FILE* problem (since my app is VS2005
and the lib is 2003): instead of PyRun_SimpleFile, use
PyRun_SimpleString ("execfile(fname)"). I got this from the win32 FAQ.

I hope someone benefits from this, it cost me plenty to figure out.

-- Jim
 
D

David M. Cotter

I find i'm having this problem, but the solution you found isn't quite specific enough for me to be able to follow it.

I'm embedding Python27 in my app. I have users install ActivePython27 in order to take advantage of python in my app, so the python installation can't be touched as it's on a user's machine.

When I attempt to do:
import win32api

i get this:
Traceback (most recent call last):
File "startup.py", line 5, in <module>
ImportError: DLL load failed: The specified module could not be found.

I someone suggested i manually load the dependent libraries in the correct order, like this:
import pywintypes
import pythoncom
import win32api

but then i get this:
Traceback (most recent call last):
File "startup.py", line 3, in <module>
File "C:\Python27\lib\site-packages\win32\lib\pywintypes.py", line 124, in <module>
__import_pywin32_system_module__("pywintypes", globals())
File "C:\Python27\lib\site-packages\win32\lib\pywintypes.py", line 64, in __import_pywin32_system_module__
import _win32sysloader
ImportError: DLL load failed: The specified module could not be found.

the ultimate goal here is actually to do this:
from win32com.client.gencache import EnsureDispatch

which currently yields:
Traceback (most recent call last):
File "startup.py", line 3, in <module>
File "C:\Python27\lib\site-packages\win32com\__init__.py", line 5, in <module>
import win32api, sys, os
ImportError: DLL load failed: The specified module could not be found.

So, if anyone has any idea, that would be super duper great. thanks so much!

notes: my paths are definitely set correctly
 
D

David M. Cotter

note that when the script is called, i DO see this in the output window:
 
T

Tim Golden

I find i'm having this problem, but the solution you found isn't
quite specific enough for me to be able to follow it.

I'm embedding Python27 in my app. I have users install
ActivePython27 in order to take advantage of python in my app, so the
python installation can't be touched as it's on a user's machine.

When I attempt to do:

i get this:

You'll likely get more input from the guys on the python-win32 list.

Most times I've come across this issue it's been because the pywin32
package (or the whole Python distribution if you're using ActiveState)
was not installed as an administrator / elevated. I'm not sure I've ever
fathomed why, and the investigation isn't helped by the import dance
which the pywin32 code does. I don't remember solving it without a
reinstall on the user's machine.

TJG
 
D

David M. Cotter

the problem was: ActivePython does not install debug libraries, so you must link with release libraries in your project. but if you run the debug version, you're linking against debug libraries which conflict with the ones linked to by python.

"fixed" by running the release version. basically, it's not possible to debug with ActivePython due to ActiveState not including debug libs. grr
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top