Page 1 of 1

runprogram

Posted: Thu Jul 10, 2014 5:44 am
by spacebuddy
In terminal if I type python3 I get this

Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 00:54:21)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>


I want to be able to execute this in PB, so I tried this

Compiler2 = RunProgram("python3","", "", #PB_Program_Open | #PB_Program_Read)
Output$ = ""
If Compiler2
While ProgramRunning(Compiler2)
If AvailableProgramOutput(Compiler2)
Output$ + ReadProgramString(Compiler2) + Chr(13)
OutPut$=Trim(OutPut$)
EndIf
Wend
EndIf

Debug Output$

But I get nothing back, anyone see what I am doing wrong. I want to be able to communicate with Python using PB :D

Re: runprogram

Posted: Thu Jul 10, 2014 2:20 pm
by Wolfram
Try:

Code: Select all

RunProgram("/usr/bin/python3","", "", #PB_Program_Open | #PB_Program_Read)
spacebuddy wrote:In terminal if I type python3 I get this

Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 00:54:21)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>


I want to be able to execute this in PB, so I tried this

Compiler2 = RunProgram("python3","", "", #PB_Program_Open | #PB_Program_Read)
Output$ = ""
If Compiler2
While ProgramRunning(Compiler2)
If AvailableProgramOutput(Compiler2)
Output$ + ReadProgramString(Compiler2) + Chr(13)
OutPut$=Trim(OutPut$)
EndIf
Wend
EndIf

Debug Output$

But I get nothing back, anyone see what I am doing wrong. I want to be able to communicate with Python using PB :D

Re: runprogram

Posted: Thu Jul 10, 2014 4:30 pm
by Danilo
You can start interactive mode with option "-i"

Code: Select all

EnableExplicit

Procedure Python(args.s, program$="")
    Protected python, output.s, error.s
    python = RunProgram("python","-u "+args, "", #PB_Program_Open | #PB_Program_Read | #PB_Program_Error | #PB_Program_Write)
    If python
        If program$
            WriteProgramStringN(python,program$)
        EndIf
        While ProgramRunning(python)
            If AvailableProgramOutput(python)
                output + ReadProgramString(python) + Chr(10)
                output = Trim(output)
            EndIf
            error = ReadProgramError(python)
            While error
                output + error + Chr(13)
                error = ReadProgramError(python)
            Wend
        Wend
        Debug output
    Else
        Debug "program not available"
    EndIf
EndProcedure

Procedure StartPython(args.s="")
    ProcedureReturn RunProgram("python","-u -i "+args, "", #PB_Program_Open | #PB_Program_Read | #PB_Program_Error | #PB_Program_Write)
EndProcedure

Procedure WritePython(python,string.s)
    If ProgramRunning(python)
        WriteProgramString(python,string+Chr(10))
    EndIf
EndProcedure

Procedure.s ReadPython(python)
    Protected output.s, error.s
    If ProgramRunning(python)
        While AvailableProgramOutput(python)
            output + ReadProgramString(python)+Chr(13)
        Wend
        error = ReadProgramError(python)
        While error
            output + error + Chr(13)
            error = ReadProgramError(python)
        Wend
    EndIf
    ProcedureReturn output
EndProcedure

Python("--help")

Debug "------------"

Define command$

command$ = "print 'Hello World'"
Python("-c "+#DQUOTE$+command$+#DQUOTE$)

Debug "------------"

Define program$

program$ = "quit()"+Chr(10)
Python("-i",program$)

Debug "------------"

program$ = "var = 9"+Chr(10)
program$ + "print "+#DQUOTE$+"var is:"+#DQUOTE$+", var"+Chr(10)
program$ + "quit()"+Chr(10)
Python("-i",program$)

Debug "------------"

Define py

py = StartPython()
If py
    Delay(200)
    Debug ReadPython(py)
    WritePython(py,"var = 12")
    Debug ReadPython(py)
    WritePython(py,"print "+#DQUOTE$+"var is: "+#DQUOTE$+", var")
    ;Delay(100)
    ;Debug ReadPython(py)
    WritePython(py,"print 'var * var is: ',var * var")
    Delay(100)
    Debug ReadPython(py)
    WritePython(py,"quit()")
Else
    Debug "cant start python"
EndIf

Re: runprogram

Posted: Thu Jul 10, 2014 5:04 pm
by spacebuddy
Thanks Danilo, that works :D