Questions tagged [python-exec]

For questions about exec/eval/etc and their behavior within Python

exec() function supports dynamic execution of Python code. object must be either a string or a code object. If it is a string, the string is parsed as a suite of Python statements which is then executed (unless a syntax error occurs).

If it is a code object, it is simply executed. In all cases, the code that’s executed is expected to be valid as file input (see the section “File input” in the Reference Manual). Be aware that the return and yield statements may not be used outside of function definitions even within the context of code passed to the exec() function. The return value is None.

Source

71 questions
0
votes
1 answer

Using nonlocal inside exec

Why does the following code: exec(""" a = 3 def b(): nonlocal a a = a + 1 b() #error occurs even without this call print(a) """ ) ) give this error: Traceback (most recent call last): File "", line 1, in File "",…
kmindspark
  • 487
  • 1
  • 7
  • 18
0
votes
1 answer

Python exec not picking up scope when called via a function

I have a function test() that should check if a string is a valid Python file. (The string is typically extracted from a readme or so.) Running exec() on the string works well, unless in this situation: string = """ import math def f(x): return…
Nico Schlömer
  • 53,797
  • 27
  • 201
  • 249
0
votes
2 answers

Python Exec not passing full variables to exec shell - with working errors

Python "Exec" command is not passing local values in exec shell. I thought this should be a simple question but all seem stumped. Here is a repeatable working version of the problem ... it took me a bit to recreate a working problem (my files are…
Edward
  • 179
  • 2
  • 12
0
votes
2 answers

How to get the returned value of a function, called by exec() in python?

I have a function called 'somefunc' : def somefunc(): return "ok" And i wanted to run it with exec() like : exec("somefunc()") That works great. But the problem is, i can't get the returned value "ok". I've tried to do this: a =…
Mr. lindroid
  • 162
  • 1
  • 12
0
votes
1 answer

How to hide args to argparse when running script with exec?

I have 2 files, runner.py that runs target.py with subprocess or exec. They both have command line options. If runner runs target with subprocess it's ok: $ python runner.py run target.py with subprocess... target.py: running with dummy = False If…
Guglie
  • 2,121
  • 1
  • 24
  • 43
0
votes
1 answer

Execute stand alone python script inside another script

I have made a .py file (i call it acomp.py) that reads from and SQL database and after a series of calculations and exports its output to an excel file.Later I sends this .xlsx file by e-mail to various persons. I wish to put it inside another…
0
votes
1 answer

How to merge dependent globals from multiple calls to exec with independent globals dicts

This code works fine - it defines do_return as a global, it defines do_proxy as a global, and do_proxy can resolve do_return when called. g1 = {} exec("def do_return(): return 1", g1) exec("def do_proxy(): return do_return()", g1) merged =…
Daniel Wagner-Hall
  • 2,446
  • 1
  • 20
  • 18
0
votes
0 answers

Python function not successful to define variable

I have run the below code: def test(yymm): exec("test_var_" + yymm + " = 'abc'") print(test_var_1912) test('1912') My expected output is to assign the variable 'test_var_1912' and print out string "abc". However, Python prompt the below…
0
votes
0 answers

Not able to fetch info from database when trying from python through exe

I've converted my Python application to an .exe using the cx_Freeze Library. Now the GUI appears after I run the exe file, but it cannot fetch data from database, everything else is working. When this application runs in python environment…
Ankit
  • 11
  • 3
0
votes
1 answer

exec: name not defined, but works earlier on in the same code

I ran into this when creating a script that exports dirs and mails to another account with exchangelib. When I run code with "exec" it returns as not defined but earlier on in the same code it works. This part doesn't work: exec('a =…
Zeznzo
  • 648
  • 7
  • 14
0
votes
2 answers

A dictionary method instead of exec method

I have the following variable: Output=[{'name': 'AnnualIncome', 'value': 5.0}, {'name': 'DebtToIncome', 'value': 5.0}, {'name': 'Grade', 'value': 'A'}, {'name': 'Home_Ownership', 'value': 'Rent'}, {'name': 'ID', 'value': 'ID'}, {'name':…
Jin Khor
  • 21
  • 3
0
votes
1 answer

Python: dynamic initialization of class members from strings holding expressions

This is a pretty general scenario, but to give some context, let's say I am using cvxpy to do some constrained optimization. Now say I want to supply the objective function at runtime, such as func below: import cvxpy as cp class foo: def…
Ilonpilaaja
  • 1,169
  • 2
  • 15
  • 26
0
votes
2 answers

Python cannot find the executed function after exec()

I cannot find the executed function even after the excution. This is the function: # function illustrating how exec() functions. def exec_code(): LOC = """ def factorial(num): fact=1 for i in range(1,num+1): fact = fact*i …
ZHANG Juenjie
  • 501
  • 5
  • 20
0
votes
2 answers

Is it possible to access exec-provided globals dictionary from within a function?

Is it possible to access exec-provided globals dictionary from within a function, if the function was defined outside of the exec-ed code (and thus already bound to different __globals__)? In other words, is there a way to make the following example…
KT.
  • 10,815
  • 4
  • 47
  • 71
0
votes
2 answers

Defining a Method from A String in Python 3 and Referencing As Method

I have a need to allow the user to define a function that processes data in an object (the wisdom and security implications in this have been discussed at length in another question and would just be duplicate comments here.) I'd like the function…
Ray Salemi
  • 5,247
  • 4
  • 30
  • 63