importing win32com.client.Dispatch with embedded interpreter

Mike Haspert mike_haspert at credence.com
Wed Aug 1 12:37:35 EDT 2001


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();

"Sometimes, it is desirable to ``uninitialize'' Python. For
instance, the application may want to start over (make another
call to Py_Initialize()) or the application is simply done with
its use of Python and wants to free all memory allocated by
Python. This can be accomplished by calling Py_Finalize(). "
 
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

WAG: Perhaps I have to do something to shut down/finalize the win32com
module before it will correctly import a second time?? If so, I've
found no clue in the doc,yet.
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.

Gordon McMillan <gmcm at hypernet.com> wrote in message news:<Xns90EFBFDF131D1gmcmhypernetcom at 199.171.54.154>...
> [posted and mailed]
> 
> Mike Haspert wrote:
> 
> > The minimal function f1(), below in usecom.py, runs fine from the
> > command line, but when I use the C API for the interpreter, it runs
> > the first time and fails thereafter. Can anyone see what I'm doing
> > wrong? The error message "TypeError: this constructor takes no
> > arguments" has me stumped.
> 
> [snip]
> 
> > int minimal(char* modname)
> > {
> >     bool success = false;
> >     PyObject *mod,*func,*args,*rvals;
> >     Py_Initialize();
> 
> [snip]
> 
> It's got nothing to do with f1.
> 
> You can't Py_Initialize() twice.
> 
> - Gordon



More information about the Python-list mailing list