comp.lang.ada
 help / color / mirror / Atom feed
* Running Python code from GPRBuild (Calling from GNAT Studio)
@ 2022-11-10 19:25 Rego, P.
  2022-11-10 21:57 ` Dmitry A. Kazakov
  2022-11-12 17:40 ` Stephen Leake
  0 siblings, 2 replies; 16+ messages in thread
From: Rego, P. @ 2022-11-10 19:25 UTC (permalink / raw)


Hello everyone, long time no see some of you, I hope everyone is fine.

I'm trying to compile some Python files from an Ada project (in an integration which will consist of Ada, Python and Rust). But let's simplify the things, so I created a Python-only project using this :

-- testpy.gpr
project Testpy is
   for Source_Dirs use ("src");
   for Object_Dir use "obj";
   for Main use ("Analysis.py");
   for Languages use ("Python");
   for Source_Files use ("Analysis.py");
end Testpy;

and as I tried to compile `Analysis.py` I got the messages
> gprbuild -ws -c -f -u -PP:\Gnat\testpy.gpr Analysis.py
> testpy.gpr:7:26: language unknown for "analysis.py"
> gprbuild: "P:\Gnat\testpy.gpr" processing failed
> [2022-11-10 16:23:10] process exited with status 4, elapsed time: 03.78s

So, obviously I am missing something (and yeah, I took the last 2h searching the docs...), so, please, how would I fix that?

Thanks
Rego.

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Running Python code from GPRBuild (Calling from GNAT Studio)
  2022-11-10 19:25 Running Python code from GPRBuild (Calling from GNAT Studio) Rego, P.
@ 2022-11-10 21:57 ` Dmitry A. Kazakov
  2022-11-11 10:25   ` Rego, P.
  2022-11-12 17:40 ` Stephen Leake
  1 sibling, 1 reply; 16+ messages in thread
From: Dmitry A. Kazakov @ 2022-11-10 21:57 UTC (permalink / raw)


On 2022-11-10 20:25, Rego, P. wrote:

> I'm trying to compile some Python files from an Ada project (in an integration which will consist of Ada, Python and Rust).

Python is not a compiled language.

1. If you want to create a Python module in a form of a dynamically 
linked library, you must use C or Ada for that.

2. If you rather meant pre-compiled binary code (*.pyc). Python creates 
them by py_compile command. See

     https://docs.python.org/3/library/py_compile.html#py_compile.compile

There is also command-line interface:

     https://docs.python.org/3/library/compileall.html

You possibly could configure gpr to call compileall on *.py files, 
though it would make no sense to me.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Running Python code from GPRBuild (Calling from GNAT Studio)
  2022-11-10 21:57 ` Dmitry A. Kazakov
@ 2022-11-11 10:25   ` Rego, P.
  2022-11-11 11:04     ` Dmitry A. Kazakov
  0 siblings, 1 reply; 16+ messages in thread
From: Rego, P. @ 2022-11-11 10:25 UTC (permalink / raw)


Hi Dmitry,

> Python is not a compiled language. 
Indeed. It will not generate an object like any compiled language.
But Since Python is included in Languages list from Project properties, I expected 
that the IDE were capable of statically checking the syntax and pointed the 
"compilation" errors. Also, I expected that we could run the script from GPS, and 
be able to debug it.
 
> 1. If you want to create a Python module in a form of a dynamically 
> linked library, you must use C or Ada for that. 

Not the case, I also don't see advantages of it (so I agree, I'd go directly to Ada or C).

> 2. If you rather meant pre-compiled binary code (*.pyc). Python creates 
> them by py_compile command. See 
> https://docs.python.org/3/library/py_compile.html#py_compile.compile 
> There is also command-line interface: 
> https://docs.python.org/3/library/compileall.html 
> You possibly could configure gpr to call compileall on *.py files, 
> though it would make no sense to me. 

I think this would be the closer of what I was trying to do.
The idea is to help in the development. Instead of having to develop the 
Python part from a separate IDE, being able to code in the same would
speed up things. 

Would you have some example of it? (how to configure gpr to call 
compileall on *.py files). 

Thanks!

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Running Python code from GPRBuild (Calling from GNAT Studio)
  2022-11-11 10:25   ` Rego, P.
@ 2022-11-11 11:04     ` Dmitry A. Kazakov
  2022-11-11 11:28       ` Rego, P.
  0 siblings, 1 reply; 16+ messages in thread
From: Dmitry A. Kazakov @ 2022-11-11 11:04 UTC (permalink / raw)


On 2022-11-11 11:25, Rego, P. wrote:

>> 1. If you want to create a Python module in a form of a dynamically
>> linked library, you must use C or Ada for that.
> 
> Not the case, I also don't see advantages of it (so I agree, I'd go directly to Ada or C).

Actually this is the way of doing scripting from Ada, like GPS IDE does. 
You write a module, e.g. in Ada, and then you can import it in your 
Python script. The module must be a DLL.

>> 2. If you rather meant pre-compiled binary code (*.pyc). Python creates
>> them by py_compile command. See
>> https://docs.python.org/3/library/py_compile.html#py_compile.compile
>> There is also command-line interface:
>> https://docs.python.org/3/library/compileall.html
>> You possibly could configure gpr to call compileall on *.py files,
>> though it would make no sense to me.
> 
> I think this would be the closer of what I was trying to do.
> The idea is to help in the development. Instead of having to develop the
> Python part from a separate IDE, being able to code in the same would
> speed up things.
> 
> Would you have some example of it? (how to configure gpr to call
> compileall on *.py files).

You could start with something like this:

project Python is
    for Languages use ("Python");
    package Compiler is
       for Driver ("Python") use
 
"C:/GNAT/2021/libexec/gnatstudio/gnathub/share/gnathub/python/python.exe";
       for Required_Switches ("Python") use ("-m", "compileall");
    end Compiler;
    package Naming is
       for Body_Suffix ("Python") use ".py";
    end Naming;
end Python;

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Running Python code from GPRBuild (Calling from GNAT Studio)
  2022-11-11 11:04     ` Dmitry A. Kazakov
@ 2022-11-11 11:28       ` Rego, P.
  2022-11-11 13:30         ` Dmitry A. Kazakov
  2022-11-11 15:20         ` Egil H H
  0 siblings, 2 replies; 16+ messages in thread
From: Rego, P. @ 2022-11-11 11:28 UTC (permalink / raw)


> You could start with something like this: 

Thanks Dmitry!

> project Python is 
> for Languages use ("Python"); 
> package Compiler is 
> for Driver ("Python") use 
> "C:/GNAT/2021/libexec/gnatstudio/gnathub/share/gnathub/python/python.exe"; 
> for Required_Switches ("Python") use ("-m", "compileall"); 
> end Compiler; 
> package Naming is 
> for Body_Suffix ("Python") use ".py"; 
> end Naming; 
> end Python;

Got it. I didn't include naming and compiler options.
Now it runs, but it asks for a linker 
> gprbuild: no linker specified and no default linker in the configuration
(where idk if it makes sense since we wont be linking to an object)

For a no error py it "compiles" clean (ok I'll call compilation, but we understand 
it's not "the compilation"), but in compilation log it doesn't show the warnings 
that are included in the edit window with the opened file. If I include 
an error, it shows in the log where the error happened, but doesn't show in
the locations. GPS will be not very useful this way. 

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Running Python code from GPRBuild (Calling from GNAT Studio)
  2022-11-11 11:28       ` Rego, P.
@ 2022-11-11 13:30         ` Dmitry A. Kazakov
  2022-11-11 13:40           ` Dmitry A. Kazakov
  2022-11-11 18:08           ` Rego, P.
  2022-11-11 15:20         ` Egil H H
  1 sibling, 2 replies; 16+ messages in thread
From: Dmitry A. Kazakov @ 2022-11-11 13:30 UTC (permalink / raw)


On 2022-11-11 12:28, Rego, P. wrote:

> Got it. I didn't include naming and compiler options.
> Now it runs, but it asks for a linker
>> gprbuild: no linker specified and no default linker in the configuration
> (where idk if it makes sense since we wont be linking to an object)
> 
> For a no error py it "compiles" clean (ok I'll call compilation, but we understand
> it's not "the compilation"), but in compilation log it doesn't show the warnings
> that are included in the edit window with the opened file. If I include
> an error, it shows in the log where the error happened, but doesn't show in
> the locations. GPS will be not very useful this way.

This "compiles" only:

project Python is
    for Languages use ("Python");
    package Compiler is
       for Driver ("Python") use 
"C:/GNAT/2021/libexec/gnatstudio/gnathub/share/gnathub/python/python.exe";
       for Required_Switches ("Python") use ("-m", "py_compile");
    end Compiler;
    package Naming is
       for Body_Suffix ("Python") use ".py";
    end Naming;
end Python;

It should show syntax errors. If you remove -m py_compile switches, then 
it will "compile" and execute.

(Python is not the thing you would wish to use in an IDE or anywhere, 
pretty much consistently so... (:-))

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Running Python code from GPRBuild (Calling from GNAT Studio)
  2022-11-11 13:30         ` Dmitry A. Kazakov
@ 2022-11-11 13:40           ` Dmitry A. Kazakov
  2022-11-11 18:12             ` Rego, P.
  2022-11-11 18:08           ` Rego, P.
  1 sibling, 1 reply; 16+ messages in thread
From: Dmitry A. Kazakov @ 2022-11-11 13:40 UTC (permalink / raw)


On 2022-11-11 14:30, Dmitry A. Kazakov wrote:
> On 2022-11-11 12:28, Rego, P. wrote:
> 
>> Got it. I didn't include naming and compiler options.
>> Now it runs, but it asks for a linker
>>> gprbuild: no linker specified and no default linker in the configuration
>> (where idk if it makes sense since we wont be linking to an object)
>>
>> For a no error py it "compiles" clean (ok I'll call compilation, but 
>> we understand
>> it's not "the compilation"), but in compilation log it doesn't show 
>> the warnings
>> that are included in the edit window with the opened file. If I include
>> an error, it shows in the log where the error happened, but doesn't 
>> show in
>> the locations. GPS will be not very useful this way.
> 
> This "compiles" only:
> 
> project Python is
>     for Languages use ("Python");
>     package Compiler is
>        for Driver ("Python") use 
> "C:/GNAT/2021/libexec/gnatstudio/gnathub/share/gnathub/python/python.exe";
>        for Required_Switches ("Python") use ("-m", "py_compile");
>     end Compiler;
>     package Naming is
>        for Body_Suffix ("Python") use ".py";
>     end Naming;
> end Python;
> 
> It should show syntax errors. If you remove -m py_compile switches, then 
> it will "compile" and execute.
> 
> (Python is not the thing you would wish to use in an IDE or anywhere, 
> pretty much consistently so... (:-))

Forgot to mention. If you want to create an executable. You can try to 
configure the package Linker using PyInstaller as the driver:

    https://pyinstaller.org/en/stable/operating-mode.html

Note that Linker does not take language e.g. ("Python") you just write:

    package Linker is
       for Driver use "pyinstaller.exe";

You have to tinker a bit since there is no object files to feed the 
Linker. Maybe you could sell it *.py as an "archive" (static library). 
Whatever... (:-))

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Running Python code from GPRBuild (Calling from GNAT Studio)
  2022-11-11 11:28       ` Rego, P.
  2022-11-11 13:30         ` Dmitry A. Kazakov
@ 2022-11-11 15:20         ` Egil H H
  2022-11-11 18:14           ` Rego, P.
  1 sibling, 1 reply; 16+ messages in thread
From: Egil H H @ 2022-11-11 15:20 UTC (permalink / raw)


On Friday, November 11, 2022 at 12:28:50 PM UTC+1, Rego, P. wrote:
> > You could start with something like this:
> Thanks Dmitry!
> > project Python is 
> > for Languages use ("Python"); 
> > package Compiler is 
> > for Driver ("Python") use 
> > "C:/GNAT/2021/libexec/gnatstudio/gnathub/share/gnathub/python/python.exe"; 
> > for Required_Switches ("Python") use ("-m", "compileall"); 
> > end Compiler; 
> > package Naming is 
> > for Body_Suffix ("Python") use ".py"; 
> > end Naming; 
> > end Python;
> Got it. I didn't include naming and compiler options. 
> Now it runs, but it asks for a linker 
> > gprbuild: no linker specified and no default linker in the configuration 
> (where idk if it makes sense since we wont be linking to an object) 
> 
> For a no error py it "compiles" clean (ok I'll call compilation, but we understand 
> it's not "the compilation"), but in compilation log it doesn't show the warnings 
> that are included in the edit window with the opened file. If I include 
> an error, it shows in the log where the error happened, but doesn't show in 
> the locations. GPS will be not very useful this way.

There are a couple of project level attributes you can play around with:
 
for Object_Generated("Python") use "False";

and

for Objects_Linked("Python") use "False";



^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Running Python code from GPRBuild (Calling from GNAT Studio)
  2022-11-11 13:30         ` Dmitry A. Kazakov
  2022-11-11 13:40           ` Dmitry A. Kazakov
@ 2022-11-11 18:08           ` Rego, P.
  2022-11-11 19:50             ` Dmitry A. Kazakov
  1 sibling, 1 reply; 16+ messages in thread
From: Rego, P. @ 2022-11-11 18:08 UTC (permalink / raw)


> It should show syntax errors. If you remove -m py_compile switches, then 
> it will "compile" and execute. 

Got some error when removed -m switch
C:\GNAT\2021\libexec\gnatstudio\gnathub\share\gnathub\python\python.exe: can't open file 'compileall': [Errno 2] No such file or directory

> (Python is not the thing you would wish to use in an IDE or anywhere, 
> pretty much consistently so... (:-))

LOL. I got lazy :-) IDEs make life easier, debugging, autocomplete, syntax highlight, 
pretty printing, analysers rsrsrs
even for Py stuff (can't run away from it)

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Running Python code from GPRBuild (Calling from GNAT Studio)
  2022-11-11 13:40           ` Dmitry A. Kazakov
@ 2022-11-11 18:12             ` Rego, P.
  0 siblings, 0 replies; 16+ messages in thread
From: Rego, P. @ 2022-11-11 18:12 UTC (permalink / raw)


> Note that Linker does not take language e.g. ("Python") you just write: 
> package Linker is 
> for Driver use "pyinstaller.exe"; 

did that... got some more ugly messages 

gprbuild -d -PP:\Gnat\testpy.gpr P:\Gnat\src\Analysis.py
Compile
   [python]       analysis.py
gprbuild: raised ADA.ASSERTIONS.ASSERTION_ERROR : Invalid Id 0
[C:\GNAT\2021\bin\gprbuild.exe]
0x8db0d6
0x648431
0x648671
0x4832bc
0x492553
0x49260e
0x4932d4
0x413bd5
0x948929
0x401423
0x40113b
[C:\WINDOWS\System32\KERNEL32.DLL]
0x7ffc052b7032
[C:\WINDOWS\SYSTEM32\ntdll.dll]
0x7ffc05e6269f
[2022-11-11 15:10:07] process exited with status 4, elapsed time: 04.03s

and now I oversimplified the Analysis.py to 
import os
import sys
print('test')

maybe I'm pushing harder than should (trying to compile/run/whatever Python from GPS),
but still curious.
 
> You have to tinker a bit since there is no object files to feed the 
> Linker. Maybe you could sell it *.py as an "archive" (static library). 
> Whatever... (:-))

Who knows :p

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Running Python code from GPRBuild (Calling from GNAT Studio)
  2022-11-11 15:20         ` Egil H H
@ 2022-11-11 18:14           ` Rego, P.
  2022-11-11 20:05             ` Egil H H
  0 siblings, 1 reply; 16+ messages in thread
From: Rego, P. @ 2022-11-11 18:14 UTC (permalink / raw)


> There are a couple of project level attributes you can play around with: 
> for Object_Generated("Python") use "False"; 
> and 
> for Objects_Linked("Python") use "False";

I had tried those before, removed because it didn't change anything (or it seemed)
Now after including Linker package I put them back, still no effect.

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Running Python code from GPRBuild (Calling from GNAT Studio)
  2022-11-11 18:08           ` Rego, P.
@ 2022-11-11 19:50             ` Dmitry A. Kazakov
  2022-11-11 21:34               ` Rego, P.
  0 siblings, 1 reply; 16+ messages in thread
From: Dmitry A. Kazakov @ 2022-11-11 19:50 UTC (permalink / raw)


On 2022-11-11 19:08, Rego, P. wrote:
>> It should show syntax errors. If you remove -m py_compile switches, then
>> it will "compile" and execute.
> 
> Got some error when removed -m switch
> C:\GNAT\2021\libexec\gnatstudio\gnathub\share\gnathub\python\python.exe: can't open file 'compileall': [Errno 2] No such file or directory

Because it is one of these three:

1. for Required_Switches ("Python") use ("-m", "py_compile");
2. for Required_Switches ("Python") use ("-m", "compileall");
3. nothing

#1 results in

    python.exe -m py_compile <file>

= compile only. py_compile is the module name that takes <file> as an 
argument.

#2 results in

    python.exe -m compileall <file>

= compile, generate *.pyc files, store them at the Python library 
location. compileall is the module name.

#3 results in

    python.exe <file>

= compile/run <file>.

When you remove -m but leave compileall you get

    python.exe compileall <file>

and compileall is treated as a file.

Command line-arguments are here:

    https://docs.python.org/3/using/cmdline.html

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Running Python code from GPRBuild (Calling from GNAT Studio)
  2022-11-11 18:14           ` Rego, P.
@ 2022-11-11 20:05             ` Egil H H
  2022-11-11 21:24               ` Rego, P.
  0 siblings, 1 reply; 16+ messages in thread
From: Egil H H @ 2022-11-11 20:05 UTC (permalink / raw)


On Friday, November 11, 2022 at 7:14:45 PM UTC+1, Rego, P. wrote:
> > There are a couple of project level attributes you can play around with: 
> > for Object_Generated("Python") use "False"; 
> > and 
> > for Objects_Linked("Python") use "False";
> I had tried those before, removed because it didn't change anything (or it seemed) 
> Now after including Linker package I put them back, still no effect.

Project level, not in the Linker package

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Running Python code from GPRBuild (Calling from GNAT Studio)
  2022-11-11 20:05             ` Egil H H
@ 2022-11-11 21:24               ` Rego, P.
  0 siblings, 0 replies; 16+ messages in thread
From: Rego, P. @ 2022-11-11 21:24 UTC (permalink / raw)


> Project level, not in the Linker package
Yep, as I did.

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Running Python code from GPRBuild (Calling from GNAT Studio)
  2022-11-11 19:50             ` Dmitry A. Kazakov
@ 2022-11-11 21:34               ` Rego, P.
  0 siblings, 0 replies; 16+ messages in thread
From: Rego, P. @ 2022-11-11 21:34 UTC (permalink / raw)


> 1. for Required_Switches ("Python") use ("-m", "py_compile"); 
> 2. for Required_Switches ("Python") use ("-m", "compileall"); 
> 3. nothing 

Tried all the options, varing w/wo "-m". Not much exciting.

Current version is 
--
project Testpy is
   for Source_Dirs use ("src");
   for Object_Dir use "obj";
   for Languages use ("python");
   for Objects_Linked ("python") use "False";
   for Object_Generated ("python") use "False";

   for Source_Files use ("Analysis.py");
   for Main use ("Analysis.py");

   package Naming is
      for Body_Suffix ("python") use ".py";
   end Naming;

   package Compiler is
      for Driver ("python") use "C:\GNAT\2021\libexec\gnatstudio\gnathub\share\gnathub\python\python.exe";
      --  for Required_Switches ("python") use ("-m", "compileall");
      --  for Required_Switches ("Python") use ("-m", "py_compile");
      for Required_Switches ("Python") use ("-m","py_compile");
   end Compiler;

   package Linker is
      for Driver use "C:\GNAT\2021\libexec\gnatstudio\gnathub\share\gnathub\python\python.exe";
   end Linker;

   for Object_Generated("Python") use "False";
   for Objects_Linked("Python") use "False";

end Testpy;
--

Curiosily, compiling Analysis.py with 
# Analysis.py
import os
import sys

print('test')
blabla

(so obviously it should not compile), returns no error

gprbuild -ws -c -f -u -PP:\Gnat\testpy.gpr Analysis.py
Compile
   [python]       analysis.py
[2022-11-11 18:27:03] process terminated successfully, elapsed time: 03.58s

But no problem. I'm convinced that Python (as external code, not GPS internal 
script) should be kept very far from GPS. 

Thanks!

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Running Python code from GPRBuild (Calling from GNAT Studio)
  2022-11-10 19:25 Running Python code from GPRBuild (Calling from GNAT Studio) Rego, P.
  2022-11-10 21:57 ` Dmitry A. Kazakov
@ 2022-11-12 17:40 ` Stephen Leake
  1 sibling, 0 replies; 16+ messages in thread
From: Stephen Leake @ 2022-11-12 17:40 UTC (permalink / raw)


"Rego, P." <pvrego@gmail.com> writes:

> Hello everyone, long time no see some of you, I hope everyone is fine.
>
> I'm trying to compile some Python files from an Ada project (in an
> integration which will consist of Ada, Python and Rust). But let's
> simplify the things, so I created a Python-only project using this :
>
> -- testpy.gpr
> project Testpy is
>    for Source_Dirs use ("src");
>    for Object_Dir use "obj";
>    for Main use ("Analysis.py");
>    for Languages use ("Python");
>    for Source_Files use ("Analysis.py");
> end Testpy;
>
> and as I tried to compile `Analysis.py` I got the messages
>> gprbuild -ws -c -f -u -PP:\Gnat\testpy.gpr Analysis.py
>> testpy.gpr:7:26: language unknown for "analysis.py"

So it does not associate ".py" with "Python". Try setting Spec_Suffix,
Body_Suffix in package Naming.

-- 
-- Stephe

^ permalink raw reply	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2022-11-12 17:40 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-10 19:25 Running Python code from GPRBuild (Calling from GNAT Studio) Rego, P.
2022-11-10 21:57 ` Dmitry A. Kazakov
2022-11-11 10:25   ` Rego, P.
2022-11-11 11:04     ` Dmitry A. Kazakov
2022-11-11 11:28       ` Rego, P.
2022-11-11 13:30         ` Dmitry A. Kazakov
2022-11-11 13:40           ` Dmitry A. Kazakov
2022-11-11 18:12             ` Rego, P.
2022-11-11 18:08           ` Rego, P.
2022-11-11 19:50             ` Dmitry A. Kazakov
2022-11-11 21:34               ` Rego, P.
2022-11-11 15:20         ` Egil H H
2022-11-11 18:14           ` Rego, P.
2022-11-11 20:05             ` Egil H H
2022-11-11 21:24               ` Rego, P.
2022-11-12 17:40 ` Stephen Leake

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox