Linked Questions
94 questions linked to/from How do I execute a string containing Python code in Python?
33
votes
3
answers
46k
views
eval SyntaxError: invalid syntax in python [duplicate]
I want to assign :
x0='123'
x1='123'
x2='123'
x3='123'
x4='123'
x5='123'
x6='123'
x7='123'
x8='123'
x9='123'
I write the code to express that i can get the ...
26
votes
4
answers
13k
views
Can I use 'eval' to define a function in Python? [duplicate]
I want to define a Python function using eval:
func_obj = eval('def foo(a, b): return a + b')
But it return invalid syntax error?
How can I make it?
Btw, how can I transform a function obj to a ...
0
votes
1
answer
8k
views
Convert string to variable in python [duplicate]
How to convert a=b=2 string into a=2, b=2 (actual assignments) in python?
There are some python parsers which give output for expressions like 2+3+4.
But how to use a=b=2 string into variable as a=2 b=...
0
votes
2
answers
2k
views
How to convert a string to python code in python? [duplicate]
If I have a string containing some code in a python file. I want to run the code in the string, for example:
program = "for i in range(100):\n\tprint(i)"
eval(program)
However, I'm pretty sure eval ...
-1
votes
4
answers
4k
views
How to turn a string expression into a normal expression in python [duplicate]
i just want to know how to turn a string expression into a normal expression, where I can solve it.
Example:
"1 + 1"
i don't want the above in a string, I want it to be
1 + 1
Another ...
2
votes
1
answer
3k
views
Possible to turn an input string into a callable function object in Python? [duplicate]
I want to be able to take a string which describes a Python function and turn it into a function object which I can then call. For example,
myString = "def add5(x):
return x + 5"
myFunc = ...
0
votes
1
answer
879
views
Python REPL in Django [duplicate]
I want to execute python code in my browser. Now I enter code in text-field in template, took it to view, where doing following:
source = request.POST.get('source', '').replace('"', r'\"')
result = ...
0
votes
4
answers
487
views
Running a python code within a python script [duplicate]
I want to do following. Script has some python code as a string (saved in a variable) and is it possible to run that code ?
Well, I know one way, writing that string to a file & running it, but ...
0
votes
1
answer
91
views
Execute Python String inside a Python code using Python3 [duplicate]
If I have a srting inside my Python code that looks like this:
x = "print('Hello World')"
I want to execute it as if it is like a seperate .py file. Is there something like
execute(x)
1
vote
1
answer
315
views
How to convert a string, with functions inside, into a executable code in Python? [duplicate]
I have a .txt file that has the following text:
"np.sqrt(2)**2"
I can't get the answer to this mathematical equation because is a string, does anyone know how to convert that text to code (...
1
vote
1
answer
195
views
How to execute command line by line in python [duplicate]
I have a text file with 2 lines
fyers.place_orders(token = token, data = {"symbol" : "NSE:SBIN-EQ","qty" : 1,"type" : 2,"side" : 1,"productType" : "INTRADAY","limitPrice" : 0,"stopPrice" : 0,"...
0
votes
1
answer
203
views
Parse a matrix in a textfile into an actual (array-based) matrix in python? [duplicate]
I am trying to parse a textfile where a matrix is saved in the string format:
"[[0,1],[1,0]]"
To goal is to parse the .txt file and turn it into an actual matrix represented by an arrays of ...
-1
votes
1
answer
141
views
Execute instructions written in a string [duplicate]
I have a little problem, I have written a for loop as a string.
In PHP, with the help of function exec(), we can run the string which will eventually run the for loop defined inside the string.
Can ...
-2
votes
2
answers
70
views
Check logical value inside a string in python [duplicate]
I mean I have this string var:
mystr1 = "1==1 or 1==2"
mystr2 = "1==1 and 1==2"
if_logical_string(mystr1) must be True
if_logical_string(mystr2) must be False
How can I achieve this? Is there any lib ...
-1
votes
2
answers
81
views
Any way to input a command and execute it? [duplicate]
a = input('Enter something:') #e.g. input is print('Hello World')
# some function
print(a) #and then we get only `Hello World` out as we use print
I already researched executing shell commands via ...