Jump to content
delphivi

Problems while importing libraries with conda

Recommended Posts

Hello, I normally use P4D with a Conda environment, that is, in PythonEngine.DllPath, I put the path of the Conda environment that contains a version of python with all its libraries installed.

Then I load my script and call the functions I need. Normally this works fine but there are some libraries that fail to import.

When I call GetPythonEngine.ExecString(stMyScript);
  fails in the line from super_gradients.training import models and returns the following message:
Exception class EPyAttributeError with message 'AttributeError: 'NoneType' object has no attribute 'write''.

However, when I run the same script in the Windows console or from vs code, it works without problems with the same Conda environment that has the libraries installed.

I know this is an environment or python problem, but I don't understand why I can't load some libraries this way.

Does anyone know any solution? Is there something I'm doing wrong?

Share this post


Link to post

Hello, Yes, I have read it, normally works correctly for me. It only happens to me with some libraries right when importing.

in that case happens with: from super_gradients.training import models

if I comment this line the rest works fine, but without predictions of course.

this is my delphi code:
 

FPythonEngine := TPythonEngine.Create(nil);

    FPythonEngine.DllPath := FEnvironmentPath;
    FPythonEngine.UseLastKnownVersion := True;
    FPythonEngine.AutoLoad := False;
    FPythonEngine.AutoUnload := False;
    FPythonEngine.InitThreads := True;
    FPythonEngine.OnBeforeLoad := PythonEngineBeforeLoad;

    FPythonModule := TPythonModule.Create(nil);
    FPythonModule.Engine := FPythonEngine;
    FPythonModule.ModuleName := 'reco';
    FPythonModule.OnInitialization := PythonModuleInitialization;
    FPythonEngine.LoadDll;
 

SetExceptionMask(exAllArithmeticExceptions);


sts := TStringList.Create;
      try
        sts.LoadFromFile(AScriptPath);
        GetPythonEngine.ExecStrings(sts);
      finally
        sts.Free;
      end;


Looks like when I run it on terminal somehow loads the libraries from conda in a different way, because running the script on the terminal without p4d works well.

 

Share this post


Link to post
On 12/29/2023 at 12:11 PM, delphivi said:

Hello, Yes, I have read it

Are you calling SetPythonHome, as suggested in that page?

 
On 12/29/2023 at 12:11 PM, delphivi said:

SetExceptionMask(exAllArithmeticExceptions);

Have you tried 

MaskFPUExceptions(True)

instead?

 

Is the Conda Library\bin subdirectory in the system path?  If not try adding it.

Edited by pyscripter

Share this post


Link to post

Hello pyscripter, Thanks for your answer.

 

On 12/30/2023 at 11:20 PM, pyscripter said:

Are you calling SetPythonHome, as suggested in that page?

Yes, I'm calling it:

procedure TPythonManager.PythonEngineBeforeLoad(Sender: TObject);
begin
    FPythonEngine.SetPythonHome(FEnvironmentPath);
end;

 

With other libraries it works well for me

 

On 12/30/2023 at 11:20 PM, pyscripter said:

MaskFPUExceptions(True)

I have tried it but there are no differences.

 

 

On 12/30/2023 at 11:20 PM, pyscripter said:

Is the Conda Library\bin subdirectory in the system path?  If not try adding it.

I have added these directories and some more to the system environment variables and I have also tried with the user variables, I have uninstalled and installed conda, I have tried with several environments. but there are some libraries that I can't import.

It happens to me with YOLO v8 from ultralytics and with super-gradients from deci.ai

I have tried asking them if they know what the problem could be and they answered me this (https://github.com/Deci-AI/super-gradients/issues/1733):

Quote

I think Python 4 Delphi does some magic with overriding stdin/stdouts which conflicts with some magic SG is also performing under the hood to log console outputs to log files. Unfortunately I don't think we are able to address this issue now. Probablly it relates to this issue but the fix is not on the roadmap yet.

 

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

×