Refer to this ,I 've coded as following William's example which is attached file. I have still error codea after clicked the button.
My the lastest I have edited,but I have still error code.
B4X:
Dim opt As PyOptions = Py.CreateOptions(File.Combine(File.DirApp, "C:\Python/python310/python.exe"))
Private Sub Fibonacci(n As Int) As PyWrapper
Dim Code As String = $"
#python code here
#Fibonacci numbers module
def Fibonacci(n):
result=[]
a,b=0,1
while a<n:
result.append(a)
a,b=b,a+b
return result
"$
Return Py.RunCode("Fibonacci", Array(n), Code)
End Sub
Server is listening on port: 61177
Python executable not found!
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
Failed to start Python process.
org.apache.commons.exec.ExecuteException: Execution failed (Exit value: -559038737. Caused by java.io.IOException: Cannot run program "C:\_2_B4J~1\PYFIBO~1\B4J\Objects\Python\python310\python.exe" (in directory "."): CreateProcess error=2, The system cannot find the file specified)
Process completed. ExitCode: -559038737
b4xset._contains (java line: 61)
java.lang.NullPointerException: Cannot invoke "b4j.example.b4xorderedmap._containskey(Object)" because "this._map" is null
at b4j.example.b4xset._contains(b4xset.java:61)
at b4j.example.pybridge._registermember(pybridge.java:515)
at b4j.example.pybridge._runcode(pybridge.java:527)
at b4j.example.b4xmainpage._fibonacci(b4xmainpage.java:183)
at b4j.example.b4xmainpage$ResumableSub_Button1_Click.resume(b4xmainpage.java:143)
at b4j.example.b4xmainpage._button1_click(b4xmainpage.java:119)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
at java.base/java.lang.reflect.Method.invoke(Method.java:578)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:111)
at anywheresoftware.b4a.BA$1.run(BA.java:236)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
at java.base/java.lang.Thread.run(Thread.java:1589)
Server is listening on port: 61427
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
connected
starting PyBridge v0.50
watchdog set to 30 seconds
Connecting to port: 61427
Error occurred on line: 61 (B4XMainPage)
(b4xmainpage._fibonacci) - Python Error (TabError) - Method: module.exec: inconsistent use of tabs and spaces in indentation (<string>, line 7)
pywrapper.raiseError (java line: 607)
java.lang.RuntimeException: (b4xmainpage._fibonacci) - Python Error (TabError) - Method: module.exec: inconsistent use of tabs and spaces in indentation (<string>, line 7)
at b4j.example.pywrapper.raiseError(pywrapper.java:607)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
at java.base/java.lang.reflect.Method.invoke(Method.java:578)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:132)
at b4j.example.pywrapper._getvalue(pywrapper.java:239)
at b4j.example.b4xmainpage$ResumableSub_Button1_Click.resume(b4xmainpage.java:152)
at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:156)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:105)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:98)
at anywheresoftware.b4a.keywords.Common$3.run(Common.java:1172)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
at java.base/java.lang.Thread.run(Thread.java:1589)
Private Sub Fibonacci(n As Int) As PyWrapper
Dim Code As String = $"
#python code here
#Fibonacci numbers module
def Fibonacci(n):
result=[]
a,b=0,1
while a<n:
result.append(a)
a,b=b,a+b
return result
"$
Return Py.RunCode("Fibonacci", Array(n), Code)
End Sub
PyBridge is a framework that allows accessing Python libraries from within B4J. It works by starting a Python process that connects to the B4J process. The Python process receives commands from the B4J process and executes them. From the developer perspective it is very similar to accessing...
The IDE will use tabs if you add a line of code. Python code expects either tabs or spaces but not both. You can ensure it only uses spaces by doing this sort of thing.
B4X:
Dim Code As String = $"
#python code here
#Fibonacci numbers module
def Fibonacci(n):
result=[]
a,b=0,1
while a<n:
result.append(a)
a,b=b,a+b
return result
"$.Replace(TAB," ") ' replace tabs with 4 spaces.
Ok, I know all of my problems
1. It is about configured path to Python directory
(My Python is C:\Python\python310\python.exe, but others may be different from mine)
B4X:
Dim opt As PyOptions = Py.CreateOptions(File.Combine("", "C:\Python\python310\python.exe"))
2. It is about the indentation each line which is different from other languages programming.