Jump to content
JGMS

Programs with embedded P4D only run on my own PC, not on any other

Recommended Posts

My Delphi programs with the P4D-component PyEmbeddedResEnvironment310 run like a charm, as long as I keep them on my own machine (W11 + Delphi 11.3 Enterprise).

On the target machines (W10), everything looks good: the embedded "3.10" environment is exactly the same, and contains all the required python libraries.

Besides, I have the registry key "Computer\HKEY_CURRENT_USER\Software\Embarcadero\BDS\22.0\Environment Variables" adjusted through the batch file: "reg_env.bat", from P4D github.

In there, "PYTHONENVIRONMENTDIR" reads "C:\Program Files\MyProgram", where the folder "3.10" stands aside the program executable (I also tried "C:\Program Files\MyProgram\3.10" by the way).

Note that the programs work as normal, except for that the Python scripts don't work. No errors occur. Just no effects.

 

What is it what I do wrong?

 

Many thanks ahead.

Jan

 

Edited by JGMS
Make clear that it only concerns Python scripts inside the running programs.

Share this post


Link to post

I'm not sure if this is your case, but a typical headache is multiple Python installations on the target machine (environment variables may differ and the application may try to use "another" python).

If no errors are displayed, it may be related to the execution rights.

Try to play around to determine if python script is trying execute at all and at least get an error message. Maybe create and run simplest demo app - say, from Python4Delphi samples?

Hope this can helps

Share this post


Link to post

Thanks for your reply @KoRif,

I found out that my (VCL) programs use the installed version on the target machine, rather than the embedded version of Python in the program. The debug-version of the executable showed errors that libraries were missing. These libraries were included in the side-packages subfolder associated with the executable, but were absent in the Python folder of the machine. Apparently, there is no embedding!

Thus the question remains: what do I wrong? I use the following code for the creation of the Python form:

 

procedure TPyForm.FormCreate(Sender: TObject);
begin
  MaskFPUExceptions(True);
  PythonEngine1              := TPythonEngine.Create(PyForm);
  PythonEngine1.RegVersion   := '3.10';
  PythonEngine1.DllName      := 'python310.dll';
  PythonEngine1.AutoLoad     := false;
  PythonEngine1.AutoFinalize := true;
  PythonEngine1.AutoUnload   := true;
  PythonEngine1.UseLastKnownVersion := false;
  PythonEngine1.RedirectIO   := false;
  PythonEngine1.loadDLL; 
  PyEmbeddedResEnvironment3101.pythonEngine := PythonEngine1;
  PyEmbeddedResEnvironment3101.Autoload     := True;
end;

When the project is compiled the folder 3.10 is created or updated, with all required libraries in it.

An installation program copies everything that is needed to the target machine. So far so good, I'd presume.

What more is needed? Anything specific in the registry settings, like "Environment Variables"?

 

Share this post


Link to post

@SwiftExpatThat was the perfect answer! Thank you very much, indeed.

Before starting the program I removed the Environment path referencing the installed Python version.

I was happy to see that the program runs independently. Just like I want it.

 

I added "PythonEngine1.DllPath      :=  IncludeTrailingPathDelimiter(ExtractFileDir(Application.ExeName)) + PythonEngine1.RegVersion;" to the code.

Great.

Edited by JGMS

Share this post


Link to post
17 hours ago, JGMS said:

Thanks for your reply @KoRif,

I found out that my (VCL) programs use the installed version on the target machine, rather than the embedded version of Python in the program. The debug-version of the executable showed errors that libraries were missing. These libraries were included in the side-packages subfolder associated with the executable, but were absent in the Python folder of the machine. Apparently, there is no embedding!

Thus the question remains: what do I wrong? I use the following code for the creation of the Python form:

 


procedure TPyForm.FormCreate(Sender: TObject);
begin
  MaskFPUExceptions(True);
  PythonEngine1              := TPythonEngine.Create(PyForm);
  PythonEngine1.RegVersion   := '3.10';
  PythonEngine1.DllName      := 'python310.dll';
  PythonEngine1.AutoLoad     := false;
  PythonEngine1.AutoFinalize := true;
  PythonEngine1.AutoUnload   := true;
  PythonEngine1.UseLastKnownVersion := false;
  PythonEngine1.RedirectIO   := false;
  PythonEngine1.loadDLL; 
  PyEmbeddedResEnvironment3101.pythonEngine := PythonEngine1;
  PyEmbeddedResEnvironment3101.Autoload     := True;
end;

When the project is compiled the folder 3.10 is created or updated, with all required libraries in it.

An installation program copies everything that is needed to the target machine. So far so good, I'd presume.

What more is needed? Anything specific in the registry settings, like "Environment Variables"?

 

  I don't know about Python, but if I understand correctly : is it possible to make an isolated installation of Python including libraries like matplotlib, pandas, etc and using it from Delphi?. Is it needed any user installation?. Thanks

Share this post


Link to post

@KoRiFIndeed, that is exactly the case. Python and all required libraries come together with the executable in an associated folder "3.10". In my understanding this folder "3.10" (in my case) must be copied to the same directory as of the Delphi executable in the target machine, although "PyEmbedded" hints me to having it all inside the executable. Might still be possible, I hope.

Anyway, there is no need to install Python on the target machine. Moreover, it works like a charm!

  • Like 1

Share this post


Link to post

Think of it as embedded = portable ( no need to install, just make the binaries available ).

Remember as well you are loading python into Delphi which does not have c runtime. C runtime might be required by some python modules.

python -m pip install msvc_runtime

This is the layout I use and it works correctly. My python install procedure copies c runtime to the EXE folder so that it loads it on the path.

 

image.png.2ecb3e97e90afc628e3bac17345b3135.png

Edited by SwiftExpat
clarify python instll
  • Like 1

Share this post


Link to post

@SwiftExpat
After compiling, each of the "P4D-embedded" applications get their own python subfolder (like "3.10" in my case) within the directory of the executable. Considering disk space, using the same "PythonEngine1.DllPath" for all should be more efficient. How does this affect the installation of the program on other machines? I presume that the DLLPath should also be made available on the target machine, which may look a bit of weird.
 

Share this post


Link to post

Application lifecycle and updates should drive the decision, not disk space.

 

Write out a timeline of 3-4 update cycles for your applications and determine if you want them to both depend on the same python embedded.  Can you manage that inter-dependency through the update cycle?

 

How it looks on the target machine is a question for your customer. As long as you are not modifying the machine path the answer is likely that they will not care.

 

If the products mature, you can always consolidate them later.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×