473,480 Members | 2,592 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

can't find win32api from embedded pyrun call

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

Jun 20 '06 #1
2 2784
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/

Jun 20 '06 #2
Jim
Miki wrote:
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

Jun 20 '06 #3

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

Similar topics

3
48171
by: Matt Smith | last post by:
hi all, again.... having another problem with python COMs, I run a python script on a win2000 machine, that uses win32com.client and it runs perfectly, whereas when I come to run it on a win98...
3
5687
by: Werner Merkl | last post by:
Hi, Python is really great, for small to big programs. For my colleagues and some circumstances I sometimes need to "compile" a script using py2exe. Cause I use Windows, I like to use the...
2
4093
by: Josh | last post by:
Hi All, I am trying to print a pdf file from python using the Win32api shellexecute method and am having a problem, or perhaps just a misunderstanding, with the bshow parameter. Am I correct to...
9
3530
by: Mayer | last post by:
Hello: I'm running python under cygwin and need to find the drive letter. Cygwin has a way of calling my drives by a name relative to the Cygwin directory, so I get things like /home/user rather...
5
3092
by: Frank Borell | last post by:
I'm having a problem retrieving the ShortName from a file that was written by a MAC and only from a Windows 2003 server. I realize the file has a space after it, but I can retrieve the short...
8
2815
by: Nikhil Patel | last post by:
Hi, I have following line in my c# code. string strDDEExpression="[Replace(" + strWorkArea + ","; When I run this and check the value in strDDEExpression, I find that the comma is missing. ...
10
2960
by: I. Myself | last post by:
Suppose we spawn a child process with Popen. I'm thinking of an executable file, like a compiled C program. Suppose it is supposed to run for one minute, but it just keeps going and going. Does...
3
2044
by: morris.slutsky | last post by:
So every now and then I like to mess around with hobby projects - I often end up trying to write an OpenGL video game. My last attempt aborted due to the difficulty of automating game elements and...
1
1765
by: Vertilka | last post by:
What i need from my C application to do ? 1) To execute a python script from file. 2) The python script will call functions in my C application. According to the answer from Ravi Teja (topic "C...
0
7055
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6920
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...
0
7060
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,...
1
6760
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5365
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4799
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...
0
3013
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...
1
572
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
206
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.