0

I had made a text editor, and added a run command to it. So recently I am using exec() to execute commands but with big code it shows error. I want to make a run command like a build command in sublime text for my text editor. So how can I make a function to perform this program execution.

It will be better if the function tell me the error too , like an any modern compiler does.

2

1 Answer 1

1

You could store the code inside a file and then call execute it with Popen.
This allows you to get the output of stdout and stderr.

Error output goes into stderr.
And normal output e.g. print goes into stdout

from subprocess import (
    Popen,
    PIPE,
)

python_cmd = Popen(('python3', 'test.py'), stderr=PIPE, stdout=PIPE)
output = python_cmd.communicate()
stdout = output[0].decode()
stderr = output[1].decode()
Sign up to request clarification or add additional context in comments.

6 Comments

I am getting error that file not found default_compiler_location=open('defaultCompilerDist.py','w+') default_compiler_location.write(str(txt.get("1.0", "end-1c"))) default_compiler_location.close() default_compile_chr=py_compile.compile('defaultCompilerDist.py') executedScript=Popen(['python3','defaultCompilerDist.py'], stderr=PIPE,stdout=subprocess.PIPE).communicate() output = executedScript.communicate() stdout = output[0].decode() stderr = output[1].decode() run_shell_text.insert(END,str(stdout))
The file ("defaultCompilerDist.py") is in the same folder of the main program.
As far as I can see it should find the file. You write the file. I dont know the purpose of using py_compile on it before using Popen. Also remove the .comunicate() after Popen because you are doing that already one line under it
i used py_compile.compile so that i can find any error in the code.
how can I get the error line number or the line number which is showing error show that I can add a tag and highlight it?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.