Linked Questions
19 questions linked to/from python: get the print output in an exec statement
1 vote
2 answers
4k views
How to get execution of python print statements as a string? [duplicate]
I am writing a python script which contains a list containing python print statements as a string. In my function, I am using a for loop to run exec function to run those statements. Here is my ...
0 votes
2 answers
872 views
How do I test what the output is of a Python Script [duplicate]
Hey I have been messing around on Python and I need to know if there is a way of testing if the output of a Python Script equals something. Heres what I have been trying: a = """ print("Hey") b = 5 ...
0 votes
0 answers
203 views
How can I get the output of a function without the 'return' statement? [duplicate]
I have recently been building a discord bot that is, apart from being a small IDE, trains and teaches you Python, by asking you questions about it. But I am stuck on the IDE part. The problem is that ...
60 votes
11 answers
72k views
How do I get the return value when using Python exec on the code object of a function?
For testing purposes I want to directly execute a function defined inside of another function. I can get to the code object of the child function, through the code (func_code) of the parent function, ...
9 votes
1 answer
6k views
Execute a block of python code with exec, capturing all its output?
What's a good way to exec a bunch of python code, like exec mycode, and capture everything it prints to stdout into a string?
5 votes
3 answers
5k views
Run a dynamic multi-line string of code in python at runtime
How can I run a multi-line string of code from inside python, additionally the string is generated at runtime and stored in either a string or an array. Background I have a function which ...
5 votes
4 answers
432 views
Retain environment of helper python script in main script
I've one helper script which I want to call from main script which is acting as a Server. This main script looks like this: Class Stuff(): def __init__(self, f): self.f = f self....
7 votes
4 answers
962 views
Python write both commands and their output to a file
Is there any way to write both commands and their output to an external file? Let's say I have a script outtest.py : import random from statistics import median, mean d = [random.random()**2 for _ ...
1 vote
1 answer
1k views
Is it possible to stop exec or eval in Python after a certain amount of time?
I want to stop the execution of the exec or eval commands if they take too long to complete. I know how to do it using multiprocessing but I was wondering if there's an easier solution. Any ideas?
1 vote
4 answers
992 views
How to display python input code along side output
I was wondering if there was away to redirect both the input and output of a python script to a file so that they were interlaced. Essentially say I have the following code: x = 1 print("hello, ...
0 votes
1 answer
829 views
Is there any way I can print the output of a code to a tkinter text box?
I am making a python IDE and I wanted to print the output of a file's code to the output text box. My current code doesnt work, it executes in the shell and the output is to the box is "None"...
1 vote
1 answer
1k views
Mounting Virtual Directory, or Running Code in Memory for python 3
I'm making a program that generates code randomly in batches, and given an input, tests to see if that code generates the desired output. I'm wanting to make the batches of generated code pretty large,...
2 votes
1 answer
631 views
Send `exec()` output to another stream without redirecting stdout
I'm writing a Sublime Text plugin that provides multiple Python shells accessible via UNIX sockets. Each shell should act as a standalone REPL running on its own thread. (It is undesirable for these ...
0 votes
0 answers
298 views
exec() with StringIO() in python
I would like to change the standard output from a terminal to a string. I could use the instruction by Frédéric Hamidi in the web page: python: get the print output in an exec statement , and it works....
2 votes
2 answers
124 views
how to convert a string to the desired form?
Good day! I'm trying to implement a module for testing knowledge. The user is given the task, he wrote the decision that is being sent and executed on the server. The question in the following. There ...
0 votes
2 answers
91 views
Python 3 I'd like my exec function to resolve to a string in a list
line1 = "class A:\n\tpass" line2 = "class B:\n\tpass" line3 = "class C:\n\tpass" line4 = "print(issubclass(C,A) and issubclass(C,B))" exec(line1...4) results in "False" printed to the console, as it ...
0 votes
0 answers
106 views
Python, running external .py script and sending output to local variable [duplicate]
I have two Python scripts. The first, when run on a text file, outputs ten lines of text to the console (I cannot paste the code here, unfortunately). The second Python script successfully runs the ...
0 votes
2 answers
64 views
Edit parameters in a python file from another python file
I've got a configuration file for an application in the form of a python file containing a dict and I'm running an AWS lambda function to get this conf file from S3, change three variables in it and ...
0 votes
0 answers
48 views
How to put a variable in a list which is created inside a for loop with exec function
After creating some loop variables with exec, I dont know how to put them in a list, see below for i in range(2): exec("x%i=i"%i) #Here I created the variables x0 and x1 y.append(??????) #I ...