• Running Python code from GPRBuild (Calling from GNAT Studio)

    From Rego, P.@21:1/5 to All on Thu Nov 10 11:25:42 2022
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Dmitry A. Kazakov@21:1/5 to All on Thu Nov 10 22:57:59 2022
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Dmitry A. Kazakov@21:1/5 to All on Fri Nov 11 12:04:43 2022
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Rego, P.@21:1/5 to All on Fri Nov 11 02:25:57 2022
    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!

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Rego, P.@21:1/5 to but it on Fri Nov 11 03:28:49 2022
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Dmitry A. Kazakov@21:1/5 to All on Fri Nov 11 14:30:22 2022
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Dmitry A. Kazakov@21:1/5 to Dmitry A. Kazakov on Fri Nov 11 14:40:14 2022
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Egil H H@21:1/5 to All on Fri Nov 11 07:20:50 2022
    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";

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Rego, P.@21:1/5 to All on Fri Nov 11 10:08:13 2022
    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)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Rego, P.@21:1/5 to All on Fri Nov 11 10:14:43 2022
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Rego, P.@21:1/5 to All on Fri Nov 11 10:12:32 2022
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Egil H H@21:1/5 to All on Fri Nov 11 12:05:12 2022
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Dmitry A. Kazakov@21:1/5 to All on Fri Nov 11 20:50:10 2022
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Rego, P.@21:1/5 to All on Fri Nov 11 13:24:59 2022
    Project level, not in the Linker package
    Yep, as I did.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Rego, P.@21:1/5 to All on Fri Nov 11 13:34:01 2022
    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!

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stephen Leake@21:1/5 to P." on Sat Nov 12 09:40:37 2022
    "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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)