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 exec how to save variable equal to a string

I am using exec to save a variable equal to a string. I am getting a SyntaxError. I'm assuming exec is getting confused with the value as string. Is this assumption accurate? Would appreciate the learnings! If I changed each question to an…
BrianBeing
  • 431
  • 2
  • 4
  • 12
0
votes
0 answers

Defining a variable inside `exec(...)` works in the global scope, but in the local scope

Consider the following code which uses Python's built-in funciton exec. exec('a = 1') print(a) # prints just fine def foo(): exec('b = 2') print(b) # NameError: name 'b' is not defined foo() When exec is used in the global context to…
0
votes
0 answers

Can the exec() function modify code in another module?

I created a new module with importlib using the following lines of code: #loader is None, new spec created spec = importlib.util.spec_from_loader(name, loader=None, origin=self.url) #creates a new module based on spec and spec.loader.create_module …
thom0075
  • 1
  • 4
0
votes
0 answers

Launch on event with python

here I write my first question as programming beginner starting with python. I have a small script that executes a loop, which function is to download a list from the server, compare it with other lists and see if something has happened. Usually the…
0
votes
3 answers

black console using py-to-exe

i tried to make an executable file from my main_app.py using pyinstaller. All works but when im duble click the .exe files generated its popping out just a black console, not the app.. This is the command i used: pyinstaller --onefile -w…
Champs
  • 121
  • 7
0
votes
1 answer

Isolate state change of chdir within python exec scope

Running a script with exec, how does one "backup" python process global state in its entirety and restores it afterwards? code example: import os print(os.getcwd()) a = 8 print(a) exec_scope = {} exec(""" import os os.chdir('../new place') a =…
AturSams
  • 7,568
  • 18
  • 64
  • 98
0
votes
1 answer

How to get function value from txt file with exec Python

I want to execute python code from txt file and store it. In txt file I have this code: def func(a): return a In python: def random_file_input(file_name): ran_number = random.randint(0, 100) with open(file_name, 'r+') as f: data…
Zakoo Koo
  • 1
  • 1
0
votes
1 answer

Exec Function, saving Methods/Classes/Variables -> Method

I'm writing a library for creating smaller python modules. I am not sure if I should be using compile() instead of exec(), it currently execute the files(any basic code in the global scope is executed) but not stored as methods in the Function where…
0
votes
1 answer

How to extend scope to an exec'd function?

I have a module object containing several function definitions. Here is a simplified example. The source code (makes up the module "my_module") def functionA(): print("hello") def functionB(): functionA() print("world") The module is…
phantom
  • 38
  • 4
0
votes
1 answer

Azure Cognitive Services and Python Executable

I am working on creating a program that uses the Speech aspect of Azure's Cognitive Services. When I deploy the executable(.exe) with just console printing, it works as hoped. The program works perfect with the Azure Voice within the VS Code…
0
votes
2 answers

How to run currently opened file in python tkinter?

I have this program written in python tkinter, which has a text box and a menu. The menu has two options, open file and run file. The open file lets you open python files and writes the contents of the file into the text box. The run file opens up a…
Skcoder
  • 299
  • 1
  • 9
0
votes
1 answer

Handle Python reserved words in a sympy parse_expr?

If I parse an expression containing lambda, I get an error even though Symbol("lambda") is valid: >>> sympy.Symbol("lambda") lambda >>> sympy.parse_expr("1 + lambda") Traceback (most recent call last): File "", line 1, in File…
amos
  • 5,092
  • 4
  • 34
  • 43
0
votes
1 answer

Pyinstaller output .exe closes immediately

I am trying to turn a python file into an executable with Pyinstaller. The file calls onto different modules as well as different libraries. When executing the command pyinstaller --onefile instabot.py a instabot.exe file is produced and placed into…
David Wicker
  • 73
  • 1
  • 1
  • 13
0
votes
1 answer

Storing stdout from an exec process into a variable

I currently have the following code whereby I am using os.execv() to execute some sort of process. For example: process_param = [exec_path, f] pid = os.fork( try: if (pid > 0): #parent time_lim = 55 for _…
0
votes
0 answers

I don't understand how Python 3 exec works

def beat(): exec('nDns=1', locals()) print(nDns) beat() Result NameError: name 'nDns' is not defined. When I change locals() to globals() it works, but I don't really want it written to globals. It must stay in scope of the function. How?
trshmanx
  • 600
  • 7
  • 16