

{"id":29059,"date":"2018-11-24T10:22:51","date_gmt":"2018-11-24T04:52:51","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=27606"},"modified":"2026-04-14T11:12:01","modified_gmt":"2026-04-14T05:42:01","slug":"python-exec-function","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/python-exec-function\/","title":{"rendered":"Python exec Function &#8211; Example and Risk"},"content":{"rendered":"<p>Over the days, we have begun discussing a few Python built-in functions we see commonly in use. Today, we will see Python exec tutorial.<\/p>\n<p>Moreover, we will see the exact meaning of exec. Also, we will discuss the Python exec() example with syntax. At last, we will look at risk with exec in Python.<\/p>\n<p>So, let&#8217;s start the Python exec tutorial.<\/p>\n<h3>What is Python exec()?<\/h3>\n<p>To say it again, exec() is a built-in function\/ method with Python. While eval returns a value, exec() runs a whole Python statement block and gives back nothing. Feed it a string holding loops, conditionals, or multi-line function definitions, and Python will execute them as if they were part of the file.<\/p>\n<p>Like eval, exec can open security holes. Malicious code could read files, send network data, or change variables. Limit its use to testing or loading trusted scripts at runtime.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; type(exec)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>&lt;class &#8216;builtin_function_or_method&#8217;&gt;<\/p>\n<\/div>\n<p>Let\u2019s check the help for this.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; help(exec)<\/pre>\n<p>Help on built-in function exec in module builtins:<br \/>\nexec(source, globals=None, locals=None, \/)<br \/>\nExecute the given source in the context of globals and locals.<br \/>\nThe source may be a string representing one or more Python statements<br \/>\nor a code object as returned by compile().<br \/>\nThe globals must be a dictionary and locals can be any mapping,<br \/>\ndefaulting to the current globals and locals.<br \/>\nIf only globals is given, locals defaults to it.<\/p>\n<h4>1. Syntax of Python exec<\/h4>\n<p>Let\u2019s see what this means:<\/p>\n<ul>\n<li>The source can be a string that denotes one or more Python statements<\/li>\n<li>The source can be a code object that the compile() function returns<\/li>\n<li>The globals are a dictionary- the current is the default<\/li>\n<li>The locals can be any kind of mapping- the current is the default; dictionary is the common mapping type with Python<\/li>\n<li>This function executes the source in the context of globals and locals<\/li>\n<li>If we only mention globals, the locals are set by default<\/li>\n<\/ul>\n<h4>2. Using exec() in Python<\/h4>\n<p>We use exec() to dynamically execute Python code- this can be a string or some object code. When it is a string, Python parses it as a set of statements and executes it if there is no syntax error.<\/p>\n<p>When it is object code, Python executes it. But exec() doesn\u2019t return a value; it returns None. Hence, we cannot use return and yield statements outside function definitions.<\/p>\n<h3>Example of Python exec<\/h3>\n<p>Let\u2019s begin with a simple example of exec. For now, let\u2019s skip the globals and locals parameters.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; code='a=7\\nprint(\"a*17=\",a*17)'\r\n&gt;&gt;&gt; exec(code)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>a*17= 119<\/p>\n<\/div>\n<h4>1. Dynamic Execution With User Input<\/h4>\n<p>If we can get the user to provide input at the time of execution, we can dynamically execute Python code. How cool is that?<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; code=input('What would you like to do today?')<\/pre>\n<p>What would you like to do today?[print(x**2) for x in range(7)]<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; exec(code)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">0<br \/>\n1<br \/>\n4<br \/>\n9<br \/>\n16<br \/>\n25<br \/>\n36<\/div>\n<h4>2. Python exec vs eval:<\/h4>\n<p>If we try doing this to create a list with Python exec:<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; exec('[(x**2) for x in range(7)]')\r\n&gt;&gt;&gt;<\/pre>\n<p>Nothing happens! We must use Python eval instead:<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; eval('[(x**2) for x in range(7)]')<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>[0, 1, 4, 9, 16, 25, 36]<\/p>\n<\/div>\n<h3>Risks With Exec in Python<\/h3>\n<h4>1. Problem in Python exec<\/h4>\n<p>When you give your users the liberty to execute any piece of code with the Python exec() function, you give them a way to bend the rules.<\/p>\n<p>What if you have access to the os module in your session, and they borrow a command from that to run? Say you have imported os in your code.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; import os\r\n&gt;&gt;&gt; code=input('What would you like to do today?')<\/pre>\n<p>What would you like to do today?print(os.listdir())<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; exec(code)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[&#8216;DLLs&#8217;, &#8216;Doc&#8217;, &#8216;etc&#8217;, \u2018hacked.dll\u2019, &#8216;include&#8217;, &#8216;Lib&#8217;, &#8216;libs&#8217;, &#8216;LICENSE.txt&#8217;, &#8216;man&#8217;, &#8216;NEWS.txt&#8217;, &#8216;opencv_ffmpeg343.dll&#8217;, &#8216;out.log&#8217;, &#8216;python.exe&#8217;, &#8216;python3.dll&#8217;, &#8216;python37.dll&#8217;, &#8216;pythonw.exe&#8217;, &#8216;Scripts&#8217;, &#8216;share&#8217;, &#8216;tcl&#8217;, &#8216;Tools&#8217;, &#8216;vcruntime140.dll&#8217;]<\/div>\n<p>Worse, they can corrupt or even delete all your files and directories. Consider we have a directory demo with three text files:<br \/>\nIn directory four, we have another text file:<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; os.chdir('\\\\demo') #The path of your demo folder\r\n&gt;&gt;&gt; os.listdir()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[&#8216;four&#8217;, &#8216;one.txt&#8217;, &#8216;three.txt&#8217;, &#8216;two.txt&#8217;]<\/div>\n<p>Now, what if the user runs this command:<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; code=input('What would you like to do today?')<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>What would you like to do today?os.system(&#8216;rm -rf *&#8217;)<\/p>\n<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; exec(code)<\/pre>\n<p>This will delete all your files in the current location! Since exec compiles and evaluates any expression you give it, it is often more risqu\u00e9 than eval and pickle.<\/p>\n<h4>2. Solution of Python exec Problem<\/h4>\n<p>You can check which variables and methods are available to the user. Use the dir() method for this.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; dir()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>[&#8216;__annotations__&#8217;, &#8216;__builtins__&#8217;, &#8216;__doc__&#8217;, &#8216;__loader__&#8217;, &#8216;__name__&#8217;, &#8216;__package__&#8217;, &#8216;__spec__&#8217;]<\/p>\n<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; a=7\r\n&gt;&gt;&gt; def hello(): print(\"Hello\")\r\n&gt;&gt;&gt; dir()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>[&#8216;__annotations__&#8217;, &#8216;__builtins__&#8217;, &#8216;__doc__&#8217;, &#8216;__loader__&#8217;, &#8216;__name__&#8217;, &#8216;__package__&#8217;, &#8216;__spec__&#8217;, &#8216;a&#8217;, &#8216;hello&#8217;]<\/p>\n<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; from os import *\r\n&gt;&gt;&gt; dir()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>[&#8216;DirEntry&#8217;, &#8216;F_OK&#8217;, &#8216;O_APPEND&#8217;, &#8216;O_BINARY&#8217;, &#8216;O_CREAT&#8217;, &#8216;O_EXCL&#8217;, &#8216;O_NOINHERIT&#8217;, &#8216;O_RANDOM&#8217;, &#8216;O_RDONLY&#8217;, &#8216;O_RDWR&#8217;, &#8216;O_SEQUENTIAL&#8217;, &#8216;O_SHORT_LIVED&#8217;, &#8216;O_TEMPORARY&#8217;, &#8216;O_TEXT&#8217;, &#8216;O_TRUNC&#8217;, &#8216;O_WRONLY&#8217;, &#8216;P_DETACH&#8217;, &#8216;P_NOWAIT&#8217;, &#8216;P_NOWAITO&#8217;, &#8216;P_OVERLAY&#8217;, &#8216;P_WAIT&#8217;, &#8216;R_OK&#8217;, &#8216;SEEK_CUR&#8217;, &#8216;SEEK_END&#8217;, &#8216;SEEK_SET&#8217;, &#8216;TMP_MAX&#8217;, &#8216;W_OK&#8217;, &#8216;X_OK&#8217;, &#8216;__annotations__&#8217;, &#8216;__builtins__&#8217;, &#8216;__doc__&#8217;, &#8216;__loader__&#8217;, &#8216;__name__&#8217;, &#8216;__package__&#8217;, &#8216;__spec__&#8217;, &#8216;_exit&#8217;, &#8216;a&#8217;, &#8216;abort&#8217;, &#8216;access&#8217;, &#8216;altsep&#8217;, &#8216;chdir&#8217;, &#8216;chmod&#8217;, &#8216;close&#8217;, &#8216;closerange&#8217;, &#8216;cpu_count&#8217;, &#8216;curdir&#8217;, &#8216;defpath&#8217;, &#8216;device_encoding&#8217;, &#8216;devnull&#8217;, &#8216;dup&#8217;, &#8216;dup2&#8217;, &#8216;environ&#8217;, &#8216;error&#8217;, &#8216;execl&#8217;, &#8216;execle&#8217;, &#8216;execlp&#8217;, &#8216;execlpe&#8217;, &#8216;execv&#8217;, &#8216;execve&#8217;, &#8216;execvp&#8217;, &#8216;execvpe&#8217;, &#8216;extsep&#8217;, &#8216;fdopen&#8217;, &#8216;fsdecode&#8217;, &#8216;fsencode&#8217;, &#8216;fspath&#8217;, &#8216;fstat&#8217;, &#8216;fsync&#8217;, &#8216;ftruncate&#8217;, &#8216;get_exec_path&#8217;, &#8216;get_handle_inheritable&#8217;, &#8216;get_inheritable&#8217;, &#8216;get_terminal_size&#8217;, &#8216;getcwd&#8217;, &#8216;getcwdb&#8217;, &#8216;getenv&#8217;, &#8216;getlogin&#8217;, &#8216;getpid&#8217;, &#8216;getppid&#8217;, &#8216;hello&#8217;, &#8216;isatty&#8217;, &#8216;kill&#8217;, &#8216;linesep&#8217;, &#8216;link&#8217;, &#8216;listdir&#8217;, &#8216;lseek&#8217;, &#8216;lstat&#8217;, &#8216;makedirs&#8217;, &#8216;mkdir&#8217;, &#8216;name&#8217;, &#8216;open&#8217;, &#8216;os&#8217;, &#8216;pardir&#8217;, &#8216;path&#8217;, &#8216;pathsep&#8217;, &#8216;pipe&#8217;, &#8216;popen&#8217;, &#8216;putenv&#8217;, &#8216;read&#8217;, &#8216;readlink&#8217;, &#8216;remove&#8217;, &#8216;removedirs&#8217;, &#8216;rename&#8217;, &#8216;renames&#8217;, &#8216;replace&#8217;, &#8216;rmdir&#8217;, &#8216;scandir&#8217;, &#8216;sep&#8217;, &#8216;set_handle_inheritable&#8217;, &#8216;set_inheritable&#8217;, &#8216;spawnl&#8217;, &#8216;spawnle&#8217;, &#8216;spawnv&#8217;, &#8216;spawnve&#8217;, &#8216;startfile&#8217;, &#8216;stat&#8217;, &#8216;stat_result&#8217;, &#8216;statvfs_result&#8217;, &#8216;strerror&#8217;, &#8216;supports_bytes_environ&#8217;, &#8216;symlink&#8217;, &#8216;system&#8217;, &#8216;terminal_size&#8217;, &#8216;times&#8217;, &#8216;times_result&#8217;, &#8216;truncate&#8217;, &#8216;umask&#8217;, &#8216;uname_result&#8217;, &#8216;unlink&#8217;, &#8216;urandom&#8217;, &#8216;utime&#8217;, &#8216;waitpid&#8217;, &#8216;walk&#8217;, &#8216;write&#8217;]<\/p>\n<\/div>\n<h4>3. Globals and Locals Parameters<\/h4>\n<p>Using the globals and locals parameters, we can restrict what variables and methods users can access. We can either provide both or just the globals, in which case that value suffices for both globals and locals.<\/p>\n<p><strong>Things to keep in mind while using Global &amp; Local Parameters:<\/strong><\/p>\n<ul>\n<li><strong>Global Parameters:<\/strong> If too many parameters are shared regularly, it becomes difficult to debug the code as it can be changed anywhere.<\/li>\n<li><strong>Local Parameters:<\/strong> They are easy to maintain as they are controlled inside a specific function, but it can be difficult when they are shared with multiple functions.<\/li>\n<\/ul>\n<p>At the module level, globals and locals are the same dictionary.<br \/>\nThe globals parameter-<\/p>\n<p>Let\u2019s take an example of global and local parameters in Python exec.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; from math import *\r\n&gt;&gt;&gt; exec('print(dir())')<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>[&#8216;__annotations__&#8217;, &#8216;__builtins__&#8217;, &#8216;__doc__&#8217;, &#8216;__loader__&#8217;, &#8216;__name__&#8217;, &#8216;__package__&#8217;, &#8216;__spec__&#8217;, &#8216;acos&#8217;, &#8216;acosh&#8217;, &#8216;asin&#8217;, &#8216;asinh&#8217;, &#8216;atan&#8217;, &#8216;atan2&#8217;, &#8216;atanh&#8217;, &#8216;ceil&#8217;, &#8216;copysign&#8217;, &#8216;cos&#8217;, &#8216;cosh&#8217;, &#8216;degrees&#8217;, &#8216;e&#8217;, &#8216;erf&#8217;, &#8216;erfc&#8217;, &#8216;exp&#8217;, &#8216;expm1&#8217;, &#8216;fabs&#8217;, &#8216;factorial&#8217;, &#8216;floor&#8217;, &#8216;fmod&#8217;, &#8216;frexp&#8217;, &#8216;fsum&#8217;, &#8216;gamma&#8217;, &#8216;gcd&#8217;, &#8216;hypot&#8217;, &#8216;inf&#8217;, &#8216;isclose&#8217;, &#8216;isfinite&#8217;, &#8216;isinf&#8217;, &#8216;isnan&#8217;, &#8216;ldexp&#8217;, &#8216;lgamma&#8217;, &#8216;log&#8217;, &#8216;log10&#8217;, &#8216;log1p&#8217;, &#8216;log2&#8217;, &#8216;modf&#8217;, &#8216;nan&#8217;, &#8216;pi&#8217;, &#8216;pow&#8217;, &#8216;radians&#8217;, &#8216;remainder&#8217;, &#8216;sin&#8217;, &#8216;sinh&#8217;, &#8216;sqrt&#8217;, &#8216;tan&#8217;, &#8216;tanh&#8217;, &#8216;tau&#8217;, &#8216;trunc&#8217;]<\/p>\n<\/div>\n<p>And now with an empty dictionary for globals:<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; exec('print(dir())',{})<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[&#8216;__builtins__&#8217;]<\/div>\n<p>This only lets the __builtins__ be available to the object. Want to know more about __builtins__? Check the help for it:<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; help(__builtins__)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">Help on built-in module builtins:<br \/>\nNAME<br \/>\nbuiltins &#8211; Built-in functions, exceptions, and other objects.<\/div>\n<p>We can confirm the unavailability of the math functions with another example:<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; exec('print(tan(90))')<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">-1.995200412208242<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; exec('print(tan(90))',{})<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">Traceback (most recent call last):<br \/>\nFile &#8220;&lt;pyshell#4&gt;&#8221;, line 1, in<br \/>\nexec(&#8216;print(tan(90))&#8217;,{})<br \/>\nFile &#8220;&#8221;, line 1, in<br \/>\nNameError: name &#8216;tan&#8217; is not defined<\/div>\n<p>If, here, we want only the tan function to be available, we can do that too:<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; exec('print(tan(90))',{'tan':tan})<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">-1.995200412208242<\/div>\n<p>Didn\u2019t we say it was a dictionary? Anyway, we can also call it anything else.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; exec('print(tanx(90))',{'tanx':tan})<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">-1.995200412208242<\/div>\n<p>The globals and locals parameters-<br \/>\nLet\u2019s try giving it both.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; exec('print(dir())',{'built':__builtins__},{'sum':sum,'iter':iter})<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[&#8216;iter&#8217;, &#8216;sum&#8217;]<\/div>\n<p>This lets the user execute the sum() and iter() methods along with the __builtins__. We can also keep the user from availing any builtins:<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; exec('print(dir())',{'__builtins__':None},{'sum':sum,'print':print,'dir':dir})<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[&#8216;dir&#8217;, &#8216;print&#8217;, &#8216;sum&#8217;]<\/div>\n<p>You can also use the built-in globals() and locals() functions for this.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; exec('print(dir())',globals(),locals())<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[&#8216;__annotations__&#8217;, &#8216;__builtins__&#8217;, &#8216;__doc__&#8217;, &#8216;__loader__&#8217;, &#8216;__name__&#8217;, &#8216;__package__&#8217;, &#8216;__spec__&#8217;, &#8216;acos&#8217;, &#8216;acosh&#8217;, &#8216;asin&#8217;, &#8216;asinh&#8217;, &#8216;atan&#8217;, &#8216;atan2&#8217;, &#8216;atanh&#8217;, &#8216;ceil&#8217;, &#8216;copysign&#8217;, &#8216;cos&#8217;, &#8216;cosh&#8217;, &#8216;degrees&#8217;, &#8216;e&#8217;, &#8216;erf&#8217;, &#8216;erfc&#8217;, &#8216;exp&#8217;, &#8216;expm1&#8217;, &#8216;fabs&#8217;, &#8216;factorial&#8217;, &#8216;floor&#8217;, &#8216;fmod&#8217;, &#8216;frexp&#8217;, &#8216;fsum&#8217;, &#8216;gamma&#8217;, &#8216;gcd&#8217;, &#8216;hypot&#8217;, &#8216;inf&#8217;, &#8216;isclose&#8217;, &#8216;isfinite&#8217;, &#8216;isinf&#8217;, &#8216;isnan&#8217;, &#8216;ldexp&#8217;, &#8216;lgamma&#8217;, &#8216;log&#8217;, &#8216;log10&#8217;, &#8216;log1p&#8217;, &#8216;log2&#8217;, &#8216;modf&#8217;, &#8216;nan&#8217;, &#8216;pi&#8217;, &#8216;pow&#8217;, &#8216;radians&#8217;, &#8216;remainder&#8217;, &#8216;sin&#8217;, &#8216;sinh&#8217;, &#8216;sqrt&#8217;, &#8216;tan&#8217;, &#8216;tanh&#8217;, &#8216;tau&#8217;, &#8216;trunc&#8217;]<\/div>\n<p>So, this was all in Python exec Function. hope you like our explanation.<\/p>\n<h3>Python Interview Questions Exec Function<\/h3>\n<p>1. What is Python exec?<\/p>\n<p>2. How do you pass arguments to exec in Python?<\/p>\n<p>3. What is the difference between eval and exec in Python?<\/p>\n<p>4. What is the use of exec in Python?<\/p>\n<p>5. How does exec work in Python?<\/p>\n<h3>Conclusion<\/h3>\n<p>Hence, in this Python exec tutorial, we will say that exec supports the dynamic execution of Python code. Moreover, we discussed the meaning of exec in Python.<\/p>\n<p>Also, we saw the Python exec example. At last, we discussed risk with the exec in Python.<\/p>\n<p>Still, if you have any confusion, ask in the comments tab.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Over the days, we have begun discussing a few Python built-in functions we see commonly in use. Today, we will see Python exec tutorial. Moreover, we will see the exact meaning of exec. Also,&#46;&#46;&#46;<\/p>\n","protected":false},"author":5,"featured_media":42988,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[4444,10522,10523,10524,10526,10527,10815,36647],"class_list":["post-29059","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-exec-in-python","tag-python-exec","tag-python-exec-command","tag-python-exec-example","tag-python-exec-pass-arguments","tag-python-exec-vs-eval","tag-python-return-value","tag-risk-with-exec-in-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python exec Function - Example and Risk - DataFlair<\/title>\n<meta name=\"description\" content=\"Learn what the exec function is in Python with syntax and example, Exec vs eval, Risks with Python Exec - problem &amp; Solution\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/python-exec-function\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python exec Function - Example and Risk - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Learn what the exec function is in Python with syntax and example, Exec vs eval, Risks with Python Exec - problem &amp; Solution\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/python-exec-function\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-11-24T04:52:51+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-04-14T05:42:01+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/Python-exec-Function-01.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python exec Function - Example and Risk - DataFlair","description":"Learn what the exec function is in Python with syntax and example, Exec vs eval, Risks with Python Exec - problem & Solution","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/python-exec-function\/","og_locale":"en_US","og_type":"article","og_title":"Python exec Function - Example and Risk - DataFlair","og_description":"Learn what the exec function is in Python with syntax and example, Exec vs eval, Risks with Python Exec - problem & Solution","og_url":"https:\/\/data-flair.training\/blogs\/python-exec-function\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2018-11-24T04:52:51+00:00","article_modified_time":"2026-04-14T05:42:01+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/Python-exec-Function-01.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/python-exec-function\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/python-exec-function\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823"},"headline":"Python exec Function &#8211; Example and Risk","datePublished":"2018-11-24T04:52:51+00:00","dateModified":"2026-04-14T05:42:01+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-exec-function\/"},"wordCount":1444,"commentCount":4,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/python-exec-function\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/Python-exec-Function-01.jpg","keywords":["Exec in python","Python exec","Python exec command","Python exec Example","Python exec pass arguments","Python exec vs eval","python return value","risk with exec in python"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/python-exec-function\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/python-exec-function\/","url":"https:\/\/data-flair.training\/blogs\/python-exec-function\/","name":"Python exec Function - Example and Risk - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-exec-function\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/python-exec-function\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/Python-exec-Function-01.jpg","datePublished":"2018-11-24T04:52:51+00:00","dateModified":"2026-04-14T05:42:01+00:00","description":"Learn what the exec function is in Python with syntax and example, Exec vs eval, Risks with Python Exec - problem & Solution","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/python-exec-function\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/python-exec-function\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/python-exec-function\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/Python-exec-Function-01.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/Python-exec-Function-01.jpg","width":1200,"height":628,"caption":"Python exec Function - Example and Risk"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/python-exec-function\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Python Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/python\/"},{"@type":"ListItem","position":3,"name":"Python exec Function &#8211; Example and Risk"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam1\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/29059","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=29059"}],"version-history":[{"count":17,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/29059\/revisions"}],"predecessor-version":[{"id":147599,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/29059\/revisions\/147599"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/42988"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=29059"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=29059"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=29059"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}