PythonEnvironments icon indicating copy to clipboard operation
PythonEnvironments copied to clipboard

Python doesn't install into a named directory

Open peardox opened this issue 3 years ago • 8 comments

Put a PythonEngine and PythonEnvironment39 on a form with a memo + button

Set PythonEnvironment to e.g. c:\temp\python (making sure you create it first)

procedure TForm3.Button1Click(Sender: TObject);
begin
  if PyEmbeddedResEnvironment391.Setup('3.9') then
    begin
      Memo1.Lines.Add('Setup returned True');
      if PyEmbeddedResEnvironment391.Activate('3.9') then
        Memo1.Lines.Add('Activate returned True')
      else
        Memo1.Lines.Add('Activate returned False');
    end
  else
      Memo1.Lines.Add('Setup returned False');
end;

Output...

BeforeSetup AfterSetup Setup returned True BeforeActivate Activate returned False

Extra output is from event handlers on install, activate and ready and the target directory is still empty after the run

If I already have a Python in the directory then it's ignored - SharedLibrary and Executable are both empty causing InternalActivate to fail

peardox avatar Nov 04 '22 16:11 peardox

Which environment component and which property should I set?

lmbelo avatar Nov 04 '22 20:11 lmbelo

PyEmbeddedResEnvironment391.EnvironmentPath

If it's blank this (stupidly simple example) works otherwise it fails

PyTest.zip

Added some OnCreate code to the form - if the property is blank it's actually '$(ENVIRONMENT_PATH)\$(PYTHON_VER)' ???

I actually want to set it to something like System.IOUtils.TPath.GetHomePath + '\SomeDir'

peardox avatar Nov 05 '22 02:11 peardox

Let me try it out.

lmbelo avatar Nov 05 '22 19:11 lmbelo

The reason you'd want to set EnvironmentPath to something else is so that you can share a PythonEmbedded installation between multiple apps - something I can see being frequently desirable to save on disk space.

If you're using Torch GPU (like me) then each app would have about 2.5 GB of Python if not more - very wasteful

peardox avatar Nov 05 '22 20:11 peardox

Think about venv: we can create different environment or share a single one. This is exactly what we want for PythonEnvironments, to work like venv.

lmbelo avatar Nov 05 '22 22:11 lmbelo

Yep, exactly - that's why I'm so keen to get this working on everything.

A really interesting possibility is having a Delphi front end to a Linux WSL running on the same machine :) There are quite a few ML/AI libs that definitely prefer Linux (I'm sure a few demand it). NVidia's latest drivers can even virtualize your GPU into WSL - not played with this much though...

peardox avatar Nov 05 '22 22:11 peardox

This is the problem...

//Let's first look for an existing environment LEnvironmentPath := TPyEnvironmentPath.ResolvePath( EnvironmentPath, PythonVersion);

TPyEnvironmentPath.ResolvePath merely checks for the existance of a path - it doesn't check whether the passed path has anything in it. More importantly if you pass it an empty directory it'll decide you've got Python in it :)

If you pass it a non-existant path then it'll create it and install Python in it.

Proper thing to do would be to at least install it in an empty dir - check if the passed path is empty or not...

class function TPyEnvironmentPath.ResolvePath(const APath, APythonVersion: string): string; begin Result := APath.Replace(PYTHON_VER, APythonVersion); Result := ResolvePath(Result); end;

What's that replace doing? Ahh - replacing "$(PYTHON_VER)" with 3.9 - errm - string don't contain "$(PYTHON_VER)" in the first place - could check for that too...

Might be nice to actually check if the path you're checking has a valid Python - does python lib exist yet? Actually you've got a cancel thing now - could have valid lib but cancelled half way through. Maybe drop some JSON in the directory - e.g. PythonEmbedded.json with some installation stuff?

Lastly - this ain't backwards compatible with the previous version - if you already have a Python then the new version don't work as it's got completely different files in it so nothing is where is should be as far as the new one is concerned

peardox avatar Nov 07 '22 17:11 peardox

It always checks for an existing Python installation in the desired folder.

lmbelo avatar Nov 28 '22 15:11 lmbelo