B4J Question B4J and Python

Tyrion

New Member
Hi. I can run simple Python scripts when I don't use any import external modules (like import requests, import numpy, ...)
Is it possible to run a "main" script using import "external python's libraries"

Is it possible using shell (sh.initialize)... or does I need to add a Jython or JPython library.
I do love and appreciate a small sample ! :cool:

Thank a lot.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Your best option is to use jShell and run it with the python command.

Something like:
B4X:
Dim shl As Shell
shl.Initialize("shl", "python", Array("path to script"))
shl.Run(30000)
Wait For shl_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
If Success Then
    Log(ExitCode)
    Log(StdErr)
    Log(StdOut)
End If

If it is a non-ui app then you need to make sure that there is a message loop running.
 
Last edited:
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Please, use the Code menu icon:

1618209068093.png


?
 
Upvote 0

Tyrion

New Member
Thanks for the thoughts Erel... always appreciate help. :)
As this is a Non-UI app, I try this code with StartMessageLoop inside :
B4X:
'Non-UI application (console / server application)
#Region Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True
#End Region

Sub Process_Globals
    
End Sub

Sub AppStart (Args() As String)
    Log("Hello")
    Dim shl As Shell
    shl.Initialize("shl", "C:\Users\Tyrion\AppData\Local\Continuum\anaconda3\python", Array("C:\Users\Tyrion\Desktop\Maths\truc.py"))
    shl.Run(30000)
    Wait For shl_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
    If Success=True Then 
        Log("Success")
    Else
        Log("Failure")
    End If
    If Success Then
        Log(StdOut)
        Log(ExitCode)
        Log(StdErr)
    End If
    StartMessageLoop
End Sub

Where truc.py is :
Python:
# -*- coding: utf-8 -*-
"""
Created on Mon Apr 12 21:34:10 2021

@author: Tyrion
"""
print('truc')

And I get no error but no return from StdOut (just log("Hello") at the begining on the console and nothing else !
The message from the console is "StartMessageLoop was not called" and all the lines after "Wait For shl_ProcessCompleted(..." seems to be ignored.

Tyrion... (humble) !
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
The answer above is correct.

In this case it should look like this:
B4X:
Sub AppStart (Args() As String)
 DoSomething
 StartMessageLoop
End Sub

Sub DoSomething
   Dim shl As Shell
    shl.Initialize("shl", "C:\Users\Tyrion\AppData\Local\Continuum\anaconda3\python", Array("C:\Users\Tyrion\Desktop\Maths\truc.py"))
    shl.Run(30000)
    Wait For shl_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
    If Success=True Then 
        Log("Success")
    Else
        Log("Failure")
    End If
    If Success Then
        Log(StdOut)
        Log(ExitCode)
        Log(StdErr)
    End If
End Sub
 
Upvote 0
Top