B4J Library [PyBridge] Console (non-ui) project template

Simple template for console apps. There is an assumption that the global Python will be used.
Put in the additional libraries folder.

1741772187759.png


Right click inside the libraries tag and choose refresh.
 

Attachments

  • PyBridge (console).b4xtemplate
    1.2 KB · Views: 17
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
Important point - when you run the program from the IDE, the IDE sets an environment variable with the global python folder, based on the path set under Tools - Configure Paths (or the installation folder if not set).

If you want to run the jar from the command line, with the global Python, then you need to set the environment variable:
B4X:
set B4J_PYTHON=D:\python\python
D:\java\jdk-19.0.2\bin\java -jar your_jar.jar
 

Daestrum

Expert
Licensed User
Longtime User
Where does version.Print2(...) output to? - its not in the logs and not in cmd window. (Using just the basic template)
 
Last edited:

Daestrum

Expert
Licensed User
Longtime User
Ok, sorted had to change code to
B4X:
    End If
    wait for (PrintPythonVersion) Completed
   
    'When possible, kill the python process before exiting:
    CloseProgram

also
B4X:
java -jar -DB4J_PYTHON=your/python/path your_jar.jar
works too
 
Last edited:

Daestrum

Expert
Licensed User
Longtime User
I set it to 2000 and still never got an output.

Edit: Just tried 10000 and still no output.

Waiting for debugger to connect...
Program started.
Server is listening on port: 58770
Python path: d:/python.3.12.8/python.exe
connected
starting PyBridge v0.70
watchdog set to 30 seconds
Connecting to port: 58770
PyBridge disconnected
Process completed. ExitCode: 1
Program terminated (StartMessageLoop was not called).
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
With this code:
B4X:
Private Sub Start
    Py.Initialize(Me, "Py")
    Dim opt As PyOptions = Py.CreateOptions("Python/python/python.exe")
    Py.Start(opt)
    Wait For Py_Connected (Success As Boolean)
    If Success = False Then
        LogError("Failed to start Python process.")
        Return
    End If
    PrintPythonVersion
    
    'When possible, kill the python process before exiting:
    Sleep(500)
    CloseProgram
End Sub
?
 

Daestrum

Expert
Licensed User
Longtime User
yes , its the Py.KillProcess - it kills python before it returns the value.

If I comment out Py.KillProcess it works fine.
 

Daestrum

Expert
Licensed User
Longtime User
Or this code works fine too
B4X:
Private Sub CloseProgram
    Sleep(100)
    Py.KillProcess
    Sleep(100)
    StopMessageLoop
End Sub
 

Daestrum

Expert
Licensed User
Longtime User
I apologise Erel I miss read post #7 and saw the sleep and assumed it was in the closeprogram sub.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
The important thing is that the template was updated and now includes the correct code.

For completeness, the Python process logs are read by B4J, using jShell library. They are not sent together with the program responses. This is important for performance reasons.
The process outputs are polled (every 50ms) so if the process is killed right after the commands were sent, they will not appear.
 
Top