importing win32com.client.Dispatch with embedded interpreter

Gordon McMillan gmcm at hypernet.com
Wed Aug 1 18:28:27 EDT 2001


Mike Haspert wrote: 

> When I saw this in the help (1.4 Embedding Python), I assumed I'd be
> OK as long as I matched each call to Py_Initialize() with a call to
> Py_Finalize();

[elided]

Yes, that's certainly what it seems to say. This is new, however,
and since completely reclaiming Python is a difficult task, it's 
likely you've exposed a bug.
 
> Adding a call to Py_IsInitialized() before each Py_Initialize()
> verifies that it is not initialized, yet my problem persists. Other
> modules import: For example, usecom.py doesn't throw an exception
> doing
>     import traceback,string,sys
> which is what I used to report the exception from trying to import
> win32com.  So we're back to my lack of understanding regarding the
> message:
> 
> Traceback (most recent call last):
>   File "c:\scripts\usecom.py", line 4, in f1
>     from win32com.client import Dispatch
>   File "C:\Python21\win32com\__init__.py", line 95, in ?
>     SetupEnvironment()
> TypeError: this constructor takes no arguments

The error makes no sense that I can see, either.

It would probably have to be debugged at the C level,
and that's not an easy task.
 
> I was hoping to embed Python as a scripting language in a significant
> automated-test-equipment app, but without the ability to stop/restart
> the interpreter at will, I'm out of luck.

Unless a module does something stupid, like hold 
transient state in module globals, you don't need
to stop/restart the interpreter. I'm 99.9% sure that
win32com.* doesn't do that.

If some module *does* do that, you can use

import module
reload(module)

to ensure that state is "fresh" (just make sure
you haven't used "from module import ....", or
you'll have to pull the names out of module yourself
after the reload).

So I don't think you should regard this as a 
showstopper. People have been embedding Python in
long running apps for ages, long before the rule of
"only one Py_Initialize / Py_Finalize per run" was
changed.
 
- Gordon



More information about the Python-list mailing list