pyscripter / python4delphi

Free components that wrap up Python into Delphi and Lazarus (FPC)
MIT License
895 stars 307 forks source link

Error: Could not open Dll "python27.dll" #425

Closed SoyYako closed 1 year ago

SoyYako commented 1 year ago

Hello friends, We have (Win-Server 2016 Standard) that has a previous installation of Python in the destination: C:\Python27 This installation is used by other processes that have been executing Scripts (.py) as expected for a long time.

We made the configuration indicated in the "How Python for Delphi finds your Python distribution" section, under the "use a specific unregistered version" mode, hoping to get our program to run, but we only got the mentioned error message.

We clarify that the "python27.dll" file is present in the "c:\windows\system32" folder, and also in the "C:\Python" folder.

We have tried to leave the file "python27.dll" inside the local folder of our program and the same error appears.

All these tests have even been carried out from the "Administrator" user. In the future, this will be launched by each user from their own account.

Recommendations?

oscar-msp commented 1 year ago

Hello! Are you using the same architecture from Python and your app? If your app is 32-bits you have to use a Python 32-bits.

I hope this can be useful.

Regards

SoyYako commented 1 year ago

Hello! The destination computer is a (Windows-Server 2016) Our program is (x64) I deduce that "Python27.dll" previously existing on that computer is also. How to know if the installed Python is x32 or x64?

oscar-msp commented 1 year ago

You can see it in the control panel Something like this: imagen

SoyYako commented 1 year ago

You can see it in the control panel ....

  • Oh, thanks...

-I just checked it. It tells "Python 2.7.10 (64-bit)"

But, in "C:\Python27\Tools\versioncheck" folder, I found this script: "pyversioncheck.py" When run, it displays: Python 2.7.10 (default, May 23 2015, 09:44:00) [MSC v.1500 64 bit (AMD64)] on win32

"...on win32" ???? But this is a new MS-Server 2016 x64. I do not understand what is happening...

SoyYako commented 1 year ago

Updated:

We have just compiled our program on x32 and the same error continues to appear.

My component configuration

1 - First, we configure on visual component, property "AutoLoad = unchecked" 2 - Next, also we configure by code:

procedure TForm1.FormCreate(Sender: TObject);
var AlternateLib,AlternateLibPath,AlternateLibVer,AlternateEngine :string;
begin
{
TEST:
comment or uncomment the next string vars, to test the three possible configuration variants detailed in the "How Python for Delphi finds your Python distribution" section:
}
  AlternateEngine := 'c:\python27\python27.dll' ;
  AlternateLib := 'C:\Windows\SysWOW64\python27.dll' ;   //'C:\MyProgram\python27.dll'
  AlternateLibVer  := '2.7' ;

  if (AlternateLib<>'')  then begin
      AlternateLib := TPath.GetFileName( AlternateLib );
      AlternateLibPath := TPath.GetDirectoryName( AlternateLib );
    end
    else if (AlternateEngine<>'')  then begin
        AlternateLib := TPath.GetFileName( AlternateEngine );
        AlternateLibPath := TPath.GetDirectoryName( AlternateEngine );
      end;

  if (AlternateEngine = '' ) then begin
    with PythonEngine1  do begin
        if (AlternateLib<>'') then  begin
         DllName := AlternateLib;
         RegVersion := AlternateLibVer;
         UseLastKnownVersion := false;
        end;
        AutoLoad := True;
      end;
  end
  else begin
    PythonPath := trim(TPath.GetDirectoryName( AlternateEngine ));
    with PythonEngine1  do begin
         UseLastKnownVersion := false;
         DllPath := AlternateLibPath;
         DllName := AlternateLib;
         RegVersion := AlternateLibVer;
         OnBeforeLoad := PythonEngine1BeforeLoad;   //this event load a public string named "PythonPath"
    end;
  end;
//----------------init----------------
  PythonEngine1.LoadDLL; 
end;

{. . .}

procedure TForm1.PythonEngine1BeforeLoad(Sender: TObject);
begin
  if PythonPath<>''  then PythonEngine1.SetPythonHome( PythonPath );
end;
oscar-msp commented 1 year ago

I think that you can try to load the library with the property "AutoLoad = checked" (True), then leave blank the DllName , DllPath and RegVersion. Your Application must to be x64 because your Python is x64.

pyscripter commented 1 year ago

Python 2.x is no longer supported. See https://github.com/pyscripter/python4delphi/releases/tag/python2 for the last release with python 2.x support.