DelphiVCL4Python icon indicating copy to clipboard operation
DelphiVCL4Python copied to clipboard

Can only run DelphiVCL program once in spyder 5.x

Open hsauro opened this issue 3 years ago • 6 comments

I use spyder 5.x (https://www.spyder-ide.org/) a lot as my a Python IDE.

I can write a DelphiVCL program, eg the simple one below. When I run it the first time it shows correctly. If I try to run it again, the screen shows but then instantly closes. In order to get the form to show correctly, I have to restart the python kernel. Not a showstopper but a nuisance for those running code from spyder. Note that the DelpchVCL code runs fine from the command line with python myfile.py. Running Python 64bit 3.8.6 on Windows 10

  from delphivcl import *

  class MainForm(Form):

     def __init__(self, owner):
         self.Caption = "A VCL Form..."
         self.SetBounds(10, 10, 500, 400)
         self.Position = "poScreenCenter"
         self.OnClose = self.__on_form_close    

    def __on_form_close(self, sender, action):
         action.Value = caFree

 def main():
       Application.Initialize()
       Application.Title = "Hello Python"
       Main = MainForm(Application)
       FreeConsole()
       Application.Run()
       Main.Destroy()

  main()

hsauro avatar Jan 14 '22 23:01 hsauro

Update: I tried the same python by calling it from pyscripter instead and got the same problem. I also switched to a different python, 3.7, but that didn't make any difference.

hsauro avatar Jan 15 '22 20:01 hsauro

Looks like this problem has been observed before:

https://en.delphipraxis.net/topic/3742-delphivcl-fantastic-but-there-are-some-issues/

hsauro avatar Jan 15 '22 20:01 hsauro

I've not tried it yet, but I'm guessing this is related to the interpreter life cycle. The application can only be initialized once in the interpreter life cycle. I'm going to try it out and come again with insights.

lmbelo avatar Jan 16 '22 13:01 lmbelo

@hsauro can you try to reproduce this issue again?

lmbelo avatar Feb 24 '22 19:02 lmbelo

C:\Program Files\Streaming\IBZ2021\Module2_3>pip uninstall delphivcl Found existing installation: delphivcl 0.1.31 Uninstalling delphivcl-0.1.31: Would remove: c:\users\breitsch\appdata\local\programs\python\python37-32\lib\site-packages\delphivcl-0.1.31.dist-info* c:\users\breitsch\appdata\local\programs\python\python37-32\lib\site-packages\delphivcl* Would not remove (might be manually added): c:\users\breitsch\appdata\local\programs\python\python37-32\lib\site-packages\delphivcl\Win64\DelphiVCL.pyd c:\users\breitsch\appdata\local\programs\python\python37-32\lib\site-packages\delphivcl_init_ - Kopie.py c:\users\breitsch\appdata\local\programs\python\python37-32\lib\site-packages\delphivcl\moduledefs - Kopie.json c:\users\breitsch\appdata\local\programs\python\python37-32\lib\site-packages\delphivcl\moduledefs.json c:\users\breitsch\appdata\local\programs\python\python37-32\lib\site-packages\delphivcl\moduledefs.log Proceed (y/n)? y Successfully uninstalled delphivcl-0.1.31

C:\Program Files\Streaming\IBZ2021\Module2_3>pip install delphivcl Collecting delphivcl Downloading delphivcl-0.1.40-cp37-cp37m-win32.whl (2.2 MB) |████████████████████████████████| 2.2 MB 1.7 MB/s Installing collected packages: delphivcl Successfully installed delphivcl-0.1.40 WARNING: You are using pip version 20.1.1; however, version 22.2.2 is available. You should consider upgrading via the 'c:\users\breitsch\appdata\local\programs\python\python37-32\python.exe -m pip install --upgrade pip' command.

C:\Program Files\Streaming\IBZ2021\Module2_3>

breitsch2 avatar Aug 05 '22 07:08 breitsch2

You can unload the DelphiVCL.pyd each time: `` eg.execStr('vcl4d_filename = delphivcl.file'); println('import name path: '+eg.EvalStr('vcl4d_filename')); eg.execStr('dll = ctypes.CDLL(vcl4d_filename)');

//https://stackoverflow.com/questions/46450368/removing-loaded-pyd-files
 eg.execStr('if vcl4d_filename.endswith("pyd"):  '+LF+
           '  _ctypes.FreeLibrary(dll._handle)  '+LF+
           '  _ctypes.FreeLibrary(dll._handle)  ');
println('unload `VCL4Python');`

Tested in maXbox4

breitsch2 avatar Aug 05 '22 19:08 breitsch2