freelanceprogrammers.org Forum Index » Delphi

Converting 3.24-->3.29


View user's profile Post To page top
rg_becker Posted: Thu Oct 06, 2005 8:17 pm


Joined: 06 Oct 2005

Posts: 7
Converting 3.24-->3.29
I am trying to bring an application up to date with Python2.4. I found the new
3.29 version of P4D supports 2.4 so I am trying to use it.

The application used to use a fairly hackish way of isolating itself from any
built in Python by handling its own engine initialization and it now seems that
I will have to alter this to make things happy.


The form initializer starts

procedure TReregForm.initialize;
begin
initializePython;
....

where the initializePython method is

procedure TReregForm.initializePython;
begin
PythonGUIInputOutput1:= TPythonGUIInputOutput.Create(self);
with PythonGUIInputOutput1 do begin
Output:= memConvertLog
end;
Python:= TMyAtomPythonEngine.Create(self);
with Python do begin
dllname := `Python24.dll`;
IO:= PythonGUIInputOutput1;
end;
pyApp:= TPythonDelphiVar.Create(self);
with pyApp do begin
Engine:= Python;
Module:= `__main__`;
VarName:= `pyApp`;
end;
pdvPercentComplete:= TPythonDelphiVar.Create(self);
with pdvPercentComplete do begin
Engine:= Python;
Module:= `__main__`;
VarName:= `progressBarPercent`;
OnChange:= pdvPercentCompleteChange;
end;
with Python do begin
Loaded;
ExecString(`import sys, os`); <---- import error on os here
ExecString(`from reregprepop import reregapp`);
ExecString(`os.chdir(os.path.dirname(reregapp.__file__))`);
homepath := EvalStringAsStr(`os.path.dirname(reregapp.__file__)`);
ExecString(`pyApp.value = reregapp.ReregApp()`);
end;
end;

I get an ImportError: `no module named os` at the first ExecString.
Through tracing I checked that the Loaded call does initialize the engine.

What am I doing wrong here? I used to be able to do the import with 3.24 &
python 2.3.
--
Robin Becker
Reply with quote
Send private message
View user's profile Post To page top
morgan_martinet Posted: Fri Oct 07, 2005 7:58 am


Joined: 29 Apr 2005

Posts: 103
Converting 3.24-->3.29
Hi Robin,

> I am trying to bring an application up to date with Python2.4. I found the
> new
> 3.29 version of P4D supports 2.4 so I am trying to use it.
>
> The application used to use a fairly hackish way of isolating itself from
> any
> built in Python by handling its own engine initialization and it now seems
> that
> I will have to alter this to make things happy.
>
>
> The form initializer starts
>
> procedure TReregForm.initialize;
> begin
> initializePython;
> ....
>
> where the initializePython method is
>
> procedure TReregForm.initializePython;
> begin
> PythonGUIInputOutput1:= TPythonGUIInputOutput.Create(self);
> with PythonGUIInputOutput1 do begin
> Output:= memConvertLog
> end;
> Python:= TMyAtomPythonEngine.Create(self);
> with Python do begin
> dllname := `Python24.dll`;
> IO:= PythonGUIInputOutput1;
> end;
> pyApp:= TPythonDelphiVar.Create(self);
> with pyApp do begin
> Engine:= Python;
> Module:= `__main__`;
> VarName:= `pyApp`;
> end;
> pdvPercentComplete:= TPythonDelphiVar.Create(self);
> with pdvPercentComplete do begin
> Engine:= Python;
> Module:= `__main__`;
> VarName:= `progressBarPercent`;
> OnChange:= pdvPercentCompleteChange;
> end;
> with Python do begin
> Loaded;
> ExecString(`import sys, os`); <---- import error on os here
> ExecString(`from reregprepop import reregapp`);
> ExecString(`os.chdir(os.path.dirname(reregapp.__file__))`);
> homepath := EvalStringAsStr(`os.path.dirname(reregapp.__file__)`);
> ExecString(`pyApp.value = reregapp.ReregApp()`);
> end;
> end;
>
> I get an ImportError: `no module named os` at the first ExecString.
> Through tracing I checked that the Loaded call does initialize the engine.
>
> What am I doing wrong here? I used to be able to do the import with 3.24 &
> python 2.3.

I see nothing wrong here, but when you setup TPythonEngine you should always
define the 3 following properties:
DllName: `python24.dll`; RegVersion: `2.4`; APIVersion: 1012

If Python could not import os, it means that it could not find it from its
path. So, try this:

ExecString(`import sys`#13`print sys.path`);

Which version of Delphi are you using? If Delphi6 or later, don`t forget
that you can get advantage of the VarPyth.pas unit which will makes things a
lot easier when try to access/invoke Python objects...

Bye,

Morgan
Reply with quote
Send private message
View user's profile Post To page top
rg_becker Posted: Fri Oct 07, 2005 12:45 pm


Joined: 06 Oct 2005

Posts: 7
Converting 3.24-->3.29
Morgan Martinet wrote:
> Hi Robin,
.......
> >
> > I get an ImportError: `no module named os` at the first ExecString.
> > Through tracing I checked that the Loaded call does initialize the engine.
> >
> > What am I doing wrong here? I used to be able to do the import with 3.24 &
> > python 2.3.
>
> I see nothing wrong here, but when you setup TPythonEngine you should always
> define the 3 following properties:
> DllName: `python24.dll`; RegVersion: `2.4`; APIVersion: 1012
>

OK that`s useful; I was thinking maybe the no site flag or something
might also play a part.

> If Python could not import os, it means that it could not find it from its
> path. So, try this:
>
> ExecString(`import sys`#13`print sys.path`);

I tried this, but got no output when using the debugger. I assume that
this is because this is being done in the form setup so my form is not
yet properly initialized & gui output is not yet available. Perhaps I`ll
get more info running the exe directly rather than trhough the debugger.

> Which version of Delphi are you using? If Delphi6 or later, don`t forget
> that you can get advantage of the VarPyth.pas unit which will makes things a
> lot easier when try to access/invoke Python objects...
>

I`m using 7, but don`t know anything about VarPython.
......

--
Robin Becker
Reply with quote
Send private message
View user's profile Post To page top
rg_becker Posted: Fri Oct 07, 2005 4:11 pm


Joined: 06 Oct 2005

Posts: 7
Converting 3.24-->3.29
....
> path. So, try this:
>
> ExecString(`import sys`#13`print sys.path`);
>

I added your API and RegVersion values and then I get this from the above

[`C:\code\fidelity\reregprepop`, `C:\code\fidelity\reregprepop`,
`C:\WINDOWS\system32\python24.zip`, ``,
`C:\code\fidelity\reregprepop\DLLs`,
`C:\code\fidelity\reregprepop\lib`,
`C:\code\fidelity\reregprepop\lib\plat-win`, `C:\code\f
idelity\reregprepop\lib\lib-tk`, `C:\code\fidelity\reregprepop`]
Traceback (most recent call last):
File "<string>", line 1, in ?
ImportError: No module named os

So I seem to be missing all of the standard Python24 libraries.
I guess that`s an improvement for packaging as that was hard to get
right before. I just need to know how to turn this path handling stuff
on and off. In deployed mode I need the above sort of behaviour ie
based on the start exe path, but when testing I need the original
Python locations; in my case that`s c:PythonDLLs etc etc.

After seeing the above I noticed that I had the following in my dpr

setEnvironmentVariable(`PYTHONPATH`, pChar(homepath));
setEnvironmentVariable(`PYTHONHOME`, pChar(homepath));
SetEnvironmentVariable(pChar(`PATH`),pChar(homepath+`;`+GetEnvironmentVariable(`
PATH`)));

ie we were trying to influence the way that PYTHONPATH was being acted
on before starting up python. I guess there`s a better way to do that
now. Anyhow removing the above in test mode makes things work as expected.

I notice that the Atom stuff is now deprecated. What should I be using?

Robin
Reply with quote
Send private message
View user's profile Post To page top
morgan_martinet Posted: Fri Oct 07, 2005 6:38 pm


Joined: 29 Apr 2005

Posts: 103
Converting 3.24-->3.29
> I added your API and RegVersion values and then I get this from the above
>
> [`C:\code\fidelity\reregprepop`, `C:\code\fidelity\reregprepop`,
> `C:\WINDOWS\system32\python24.zip`, ``,
> `C:\code\fidelity\reregprepop\DLLs`,
> `C:\code\fidelity\reregprepop\lib`,
> `C:\code\fidelity\reregprepop\lib\plat-win`, `C:\code\f
> idelity\reregprepop\lib\lib-tk`, `C:\code\fidelity\reregprepop`]
> Traceback (most recent call last):
> File "<string>", line 1, in ?
> ImportError: No module named os
>
> So I seem to be missing all of the standard Python24 libraries.
Right.

> I guess that`s an improvement for packaging as that was hard to get
> right before. I just need to know how to turn this path handling stuff
> on and off. In deployed mode I need the above sort of behaviour ie
> based on the start exe path, but when testing I need the original
> Python locations; in my case that`s c:PythonDLLs etc etc.
Don`t forget to read the PDF document "Deploying P4D" that tries to give
hints about this...

> After seeing the above I noticed that I had the following in my dpr
>
> setEnvironmentVariable(`PYTHONPATH`, pChar(homepath));
> setEnvironmentVariable(`PYTHONHOME`, pChar(homepath));
> SetEnvironmentVariable(pChar(`PATH`),pChar(homepath+`;`+GetEnvironmentVari
> able(`PATH`)));
>
> ie we were trying to influence the way that PYTHONPATH was being acted
> on before starting up python. I guess there`s a better way to do that
> now. Anyhow removing the above in test mode makes things work as expected.
Great! Indeed, setting environment variables changed the behavior of Python.
There are also other ways to influence it (see pdf doc).

> I notice that the Atom stuff is now deprecated. What should I be using?
That`s because you should use the VarPyth.pas unit from now on.
You have demo25 that unit tests it and shows all the things you can do with
it... and there are a couple of other demos using it (search for VarPyth).

In your case, you initialization code would become:
Old style:
with Python do begin
Loaded;
ExecString(`import sys, os`); <---- import error on os here
ExecString(`from reregprepop import reregapp`);
ExecString(`os.chdir(os.path.dirname(reregapp.__file__))`);
homepath := EvalStringAsStr(`os.path.dirname(reregapp.__file__)`);
ExecString(`pyApp.value = reregapp.ReregApp()`);
end;

VarPyth style:

Uses ... VarPyth;
Var
_os : Variant;
_reregapp : Variant;
_homepath : String;
Begin
_os := ImportModule(`os`);
_reregapp := Import(`reregprepop`).reregapp;
_homepath := _os.path.dirname(_reregapp.__file__);
_os.chdir(_homepath);
MainModule.pyApp.value := _reregapp.ReregApp();
// or you can add a new global to the main module (or any other module)
// without relying on a TPythonDelphiVar:
MainModule.myApp := _reregapp.ReregApp();
End;

Note that I did not compile this code and don`t guarantee that it will work
from scratch, but this should be something that you can do with VarPyth.

Bye,

Morgan
Reply with quote
Send private message
Post new topic Reply to topic
Display posts from previous:   
 

All times are GMT
Page 1 of 1
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Freelace Website Designer - Customer web design and software building.
Booking Calendar - reservation calendar script
Land Surveying - total station instruments and equipments
China Wholesale - Electronics Products
Character Studio - Tutorials and Help