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
2
votes
1 answer

How to change __builtins__ module variable?

If you run this code: src = "import os" d = dict(__builtins__={}) exec src in d Python says: ImportError: __import__ not found That's what I like to do, but when creating (or maybe loading) a new module: import imp mod = imp.new_module("foo") src…
1
vote
1 answer

Command exec in a function, name error - python3

I'm begginer in Python and I'm in front of a problem that I can't understand. I tried to just define a variable with a exec() and then just print it. And it's working well. BUT when I do the same code in a function, it's not working... Example : def…
LeoZ
  • 13
  • 5
1
vote
0 answers

Why local variables go missing in Python exec while running bytecode?

I've built a function called foo to alter a function's code at bytecode level and execute it before returning to regular function execution flow. import sys from types import CodeType def foo(): frame = sys._getframe(1) # get main's frame …
Pedro
  • 1,121
  • 7
  • 16
1
vote
1 answer

Python NameError in a function body

I have two blocks of python code, one works, and the other doesn't. Working block: env = {'user':'xyz'} for key, value in env.items(): exec("{} = value".format(key)) print(user) output: xyz The block, which doesn't work: def test(): …
Anil Bisht
  • 55
  • 1
  • 6
1
vote
0 answers

Getting pytest to understand exec built-in function

I have a simple test below that I run with pytest. It works via the built-in function exec: def test_exec() -> None: foo = "" # Causes test to fail if not updated by exec exec("foo = 5") assert isinstance(foo, int) When run outside of…
Intrastellar Explorer
  • 3,005
  • 9
  • 52
  • 119
1
vote
3 answers

Python 3 - How to exec a string as if it were substituted directly?

Problem Description I am curious if it is possible to exec a string within a function as if the string were substituted for exec directly (with appropriate indentation). I understand that in 99.9% of cases, you shouldn't be using exec but I'm more…
nj3
  • 43
  • 3
1
vote
1 answer

Git : few exes not properly checkout

Am using git version 2.21.0.windows.1 with Git Extensions v 2.48.05 on Windows 10. In our project there are few python executable. After I clone our repository, there are many exe which are not getting checked-out properly. I separately copied the…
Sayan Bera
  • 135
  • 2
  • 16
1
vote
1 answer

Pyinstaller adddata query

When I am trying to converting my python file into executable and binding with pdf with using command add-data. My pdf file is store no where due to this I cannot open my pdf file while opening executable.(yes, but command will create executable…
1
vote
1 answer

Pros and cons of using exec for importing a specific module?

I would like to find out disadvantages of using exec for imports. One of the files serves as interface towards real implementations of specific functionalities depending on chosen project (framework is intended to work on several projects). First…
Sir DrinksCoffeeALot
  • 593
  • 2
  • 11
  • 20
1
vote
0 answers

What are the scoping rules for exec?

I have (as it seems to me) some basic understanding of Python scoping rules (based on studying of this answer). I have also learned from the documentation on exec, that it takes globals and locals as optional arguments, and that: In all cases, if…
user8554766
1
vote
1 answer

cx_freeze Matplotlib issue

There seems to be a few people who are struggling to build Python executables that use the Matplotlib library. I am using CX_Freeze and can create a executable for my GUI without the graphs, but when the graphs with the matplolib library are added,…
DonikuY
  • 31
  • 8
0
votes
0 answers

After comfile and installing with pyinstaller and nsis, the following error occurs (ImportError: PiFrozenImporter cannot handle module 'inspect'_

When I tried to run it, something turned off while running it, so I went to the installation folder and ran it, and I got an error as below. This is the full text of the error. When I tried to run it, something turned off while running it, so I…
sukja
  • 1
  • 1
0
votes
0 answers

Creating a debugable function with exec

Is it possible to debug the code of a function that was defined via exec in a convenient way? foo_source = """\ def foo(): print("start") breakpoint() print("end") """ exec(foo_source) foo() The breakpoint in the code works, and I can…
Jakube
  • 3,353
  • 3
  • 23
  • 40
0
votes
1 answer

Executing modules specified by strings

I am developing a Django backend for an online course platform. The backend runs the code submitted by the student. Here is a working example for running a student code consisting of three modules: import importlib import sys util_a = """ def…
AlwaysLearning
  • 7,257
  • 4
  • 33
  • 68
0
votes
0 answers

embed list in a string and run it with exec on python

I was Trying to Make an python line as String and run it using exec(), that string was supposed to invoke a module and pass parameters ,here the parameter is a list and when i inject in string and call exec on it .. i'm getting - TypeError: can…