B4J Question Can I check the library is used?

Theera

Expert
Licensed User
Longtime User
Assume I need to select PyBridge library,so I check the PyBridge library in the Libraries list.
I would like to know how I check that I have selected the PyBridge library yet via my code.
My taught is
B4X:
If IsUsed("PyBridge" ) then
          #Region PythonConnected
                             Py.Initialize(Me, "Py")
                            'โปรแกรมจะมองหา Python.exe Local Python ก่อน ถ้าหาไม่เจอ จะไปหาที่ global Python ซึ่งได้กำหนดเส้นทางไดเรคทอรี่Tools-Configure Paths ก่อนหน้า
                             Dim opt As PyOptions = Py.CreateOptions("Python/python/python.exe")
 
                             'แต่กรณีไม่ได้กำหนดเส้นทางไดเรคทอรี่Tools-Configure Paths และ ไม่สามารถค้นหา Local Python เจอ เราต้องเขียนคำสั่งต่อไปนี้แทน
                             'Dim opt As PyOptions = Py.CreateOptions(File.Combine("", "C:\Python\python313\python.exe")) '<--  ถ้าเราไม่ใช้คำสั่งนี้จะ Error เกิดขึ้น
                             Py.Start(opt)
                             Wait For Py_Connected (Success As Boolean)
                             If Success = False Then
                                      LogError("Failed to start Python process.")
                                      Return
                             End If
        #End Region
End If
If IsUsed("PyBridge" ) then
     Private Sub B4XPage_Background
          Py.KillProcess
     End Sub
End If

If IsUsed("PyBridge" ) then
    Private Sub Py_Disconnected
         Log("PyBridge disconnected")
    End Sub
End If
 
Last edited:

Daestrum

Expert
Licensed User
Longtime User
I think you would need to use tags like we do for B4J,B4A,B4I #If B4J ... #End If

If you just use 'normal' if statements the code will still get included in the compile.

You would need to create a PYTHON tag and use that : #If PYTHON ... #End If to surround the code that uses PyBridge.
 
Upvote 0

Theera

Expert
Licensed User
Longtime User
I think you would need to use tags like we do for B4J,B4A,B4I #If B4J ... #End If

If you just use 'normal' if statements the code will still get included in the compile.

You would need to create a PYTHON tag and use that : #If PYTHON ... #End If to surround the code that uses PyBridge.
If # Python... #End If, I think we would only use a limited band, but If we have IsUsed() then.. End If , it is more useful better. At least it can be included in the same B4J template.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
If you didn't add the library, you will get an error in the IDE and you can't initialize an object from the class or module.
Something like:
Unknown type: PyBridge
Are you missing a library reference?
 
Upvote 0

Theera

Expert
Licensed User
Longtime User
If you didn't add the library, you will get an error in the IDE
That is the answer is isUsed("PyBridge") be not used , The app can skip the conditions to do anything.
 
Upvote 0

Theera

Expert
Licensed User
Longtime User
I think you don't get my point.
My target is why we don't do in the only one template. I think PyBridgeis one of the part B4J Template. I 'm sorry that my terribled English.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
I haven't tried PyBridge.
My point is,
If you add a library and don't use it, you get a warning in the Logs.
If you don't add a library and try to use it, you get an error unknown type in the Logs.
 
Upvote 0
Top