tomye 1 Posted October 30, 2022 if write like this , got an error : There is already one instance of TPythonEngine running the P4D can not change Python DLLs in runtime ? Share this post Link to post
David Heffernan 2345 Posted October 30, 2022 I don't think embedded Python supports this scenario Share this post Link to post
pyscripter 689 Posted October 30, 2022 Set AutoLoad to False and call LoadDLL at Runtime (e.g. in your FormCreate). Share this post Link to post
tomye 1 Posted October 31, 2022 7 hours ago, pyscripter said: Set AutoLoad to False and call LoadDLL at Runtime (e.g. in your FormCreate). Yes, i did , but does not work , so i post the question on here does the P4D not support LOADDLL and UNLOADDLL dynamic call ? for example: i launch the exe and call the LoadDLL function at first time after run one script i need reset a new DLL path and call LoadDLL again, how to do like this? Share this post Link to post
pyscripter 689 Posted October 31, 2022 18 hours ago, tomye said: Yes, i did , but does not work , so i post the question on here does the P4D not support LOADDLL and UNLOADDLL dynamic call ? It does, but it is not a good idea and should be avoided. See demo34 for how it can work. This demonstrates also AutoLoad = False Share this post Link to post
David Heffernan 2345 Posted October 31, 2022 1 hour ago, pyscripter said: It does, but it is not a good idea and should be avoided. See demo34 for how it can work. This demonstrates also AutoLoad = False Has this changed? I remember failing to do this, unload and reload the dll in my own wrapper of embedded Python. But that was very many years ago so things likely have changed. Share this post Link to post
pyscripter 689 Posted November 1, 2022 8 hours ago, David Heffernan said: Has this changed? Unloading and reloading has always been possible with P4D, but quite tricky and not foolproof. You need to manually unload the dll's loaded by imported modules and if you fail to do that it will fail. PyScripter does that and allows you to change python version without exiting. 1 Share this post Link to post
David Heffernan 2345 Posted November 1, 2022 How do you find out which DLLs to unload? Share this post Link to post
Brian Evans 105 Posted November 1, 2022 1 hour ago, David Heffernan said: How do you find out which DLLs to unload? PyScripter is opensource so you can read the code. A quick look seems to suggest it doesn't - it unloads a bunch of common .pyd that might be loaded. I could be missing something however. In file cInternalPython.pas the procedure TInternalPython.DestroyPythonComponents; from https://github.com/pyscripter/pyscripter. 1 Share this post Link to post
pyscripter 689 Posted November 2, 2022 19 hours ago, Brian Evans said: PyScripter is opensource so you can read the code. A quick look seems to suggest it doesn't - it unloads a bunch of common .pyd that might be loaded. Indeed. As I said it is not foolproof. If you use a specific set of python modules, you can see in Delphi's debug mode which DLLs are loaded when your python scripts execute. You can then write code to unload them after unloading python. Failing to unload a DLL does not necessarily mean that there will be an issue next time you load python. Share this post Link to post
David Heffernan 2345 Posted November 2, 2022 Thanks, this helps. None of this would be necessary if python modules unloaded their DLLs so I'm guessing that Python itself doesn't provide hooks to modules to let them do that. Or perhaps is it just that modules aren't in the habit of unloading their DLLs. I think it would be easy to do for extension modules. But I'm not sure how a Python module that loaded DLLs with ctypes would get a hook to unload them. Certainly it sounds like what OP is trying to do is not very viable. Especially if the Python code that is executed can be specified at runtime. Share this post Link to post
Fr0sT.Brutal 900 Posted November 3, 2022 14 hours ago, David Heffernan said: But I'm not sure how a Python module that loaded DLLs with ctypes would get a hook to unload them DllMain with DLL_PROCESS_DETACH parameter? Share this post Link to post
David Heffernan 2345 Posted November 3, 2022 26 minutes ago, Fr0sT.Brutal said: DllMain with DLL_PROCESS_DETACH parameter? For a Python extension module. But a pure Python module isn't a DLL. Share this post Link to post