How to stop Py_Initialise application crash?

From VBA and VB6 I call the dll, creates a Python interpreter. If I set the PATH environment variable to point to "C:\python27" and PYTHONPATH to "c:\python27\lib" , everything will be fine.

If I do not set PATH , then calling Py_Initialise() will crash XL or my VB6 application, even if I first call Py_SetProgramName with "c:\python27\python27.exe" .

I would like to specify the installation in VB / VBA, and not install it in the environment, since I cannot do it in XL (it works fine for VB6).

+4
source share
3 answers

The best answer I've found so far is that this is a bug in Python - http://bugs.python.org/issue6498 . It seems that intepreter calls exit () on some errors, rather than passing the code back to the caller. Not very friendly if you embed python in an application. But there you go.

+3
source

try changing the working directory before calling the dll:

In VBA code:

 chdir("c:\python27\") '- change the working-directory for python => call dll '- call the dll chdir(app.Path) '- change back to your folder (maybe you want to save your current folder bevore you change it the first time and change back to this?!) 

examines Thomas

+1
source

You can simply check if the required environment variable is set:

 dim PPath as string PPath = trim(Environ("PYTHONPATH")) if (PPath<>"") <call dll> else msgbox ("Error!") end if 

Or you can run the DLL in the nother test process: if this call works, you know that it is normal to call the DLL - it depends on the DLL call of your use, so I'm not sure about that:

 Private Declare Function CloseHandle Lib "kernel32" (ByVal _ hObject As Long) As Long Private Declare Function OpenProcess Lib "kernel32" (ByVal _ dwDesiredAccess As Long, ByVal bInheritHandle As _ Long, ByVal dwProcessId As Long) As Long Private Declare Function GetExitCodeProcess Lib "kernel32" _ (ByVal hProcess As Long, lpExitCode As Long) As Long Const STILL_ACTIVE = &H103 Const PROCESS_ALL_ACCESS = &H1F0FFF ... dim IsActive as boolean dim Handle as long Dim TaskID As Long TaskID = Shell("rundll32.exe pyton.dll Py_Initialise()", vbNormalNoFocus) <wait for some time> '- check if pyton.dll is still running Handle = OpenProcess(PROCESS_ALL_ACCESS, False, TaskID) Call GetExitCodeProcess(Handle, ExitCode) Call CloseHandle(Handle) IsActive = IIf(ExitCode = STILL_ACTIVE, True, False) if (not IsActive) then msgbox ("Error!") else <kill process> <call dll normally> end if 

Regards, Thomas

0
source

All Articles