René's URL Explorer Experiment


Title: Python's exec(): Execute Dynamically Generated Code – Real Python

Open Graph Title: Python's exec(): Execute Dynamically Generated Code – Real Python

Description: In this tutorial, you'll learn how to use Python's built-in exec() function to execute code that comes as either a string or a compiled code object.

Open Graph Description: In this tutorial, you'll learn how to use Python's built-in exec() function to execute code that comes as either a string or a compiled code object.

Mail addresses
?subject=Python article for you&body=Python's exec(): Execute Dynamically Generated Code on Real Python https://realpython.com/python-exec/

Opengraph URL: https://realpython.com/python-exec/

X: @realpython

direct link

Domain: realpython.com


Hey, it has json ld scripts:
{
  "@context": "http://schema.org",
  "@type": "Article",
  "headline": "Python's exec(): Execute Dynamically Generated Code",
  "image": {
    "@type": "ImageObject",
    "url": "https://files.realpython.com/media/How-to-Use-Pythons-exec-Function_Watermarked.15c3fc76b92a.jpg",
    "width": 1920,
    "height": 1080
  },
  "mainEntityOfPage": {
    "@type": "WebPage",
    "@id": "https://realpython.com/python-exec/",
    "lastReviewed": "2022-11-10",
    "author": {
      "@type": "Person",
      "name": "Leodanis Pozo Ramos",
      "image": "https://realpython.com/cdn-cgi/image/width=862,height=862,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/Perfil_final1.9f896bc212f6.jpg",
      "url": "https://realpython.com/team/lpozoramos/",
      "affiliation": {
        "@type": "Organization",
        "@id": "https://realpython.com/#organization",
        "name": "Real Python",
        "url": "https://realpython.com",
        "logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
      }
    },
    "reviewedBy": [
      {
        "@type": "Person",
        "name": "Aldren Santos",
        "image": "https://realpython.com/cdn-cgi/image/width=500,height=500,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/Aldren_Santos_Real_Python.6b0861d8b841.png",
        "url": "https://realpython.com/team/asantos/",
        "affiliation": {
          "@type": "Organization",
          "@id": "https://realpython.com/#organization",
          "name": "Real Python",
          "url": "https://realpython.com",
          "logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
        }
      },
      {
        "@type": "Person",
        "name": "Geir Arne Hjelle",
        "image": "https://realpython.com/cdn-cgi/image/width=800,height=800,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/gahjelle.470149ee709e.jpg",
        "url": "https://realpython.com/team/gahjelle/",
        "affiliation": {
          "@type": "Organization",
          "@id": "https://realpython.com/#organization",
          "name": "Real Python",
          "url": "https://realpython.com",
          "logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
        }
      },
      {
        "@type": "Person",
        "name": "Ian Currie",
        "image": "https://realpython.com/cdn-cgi/image/width=480,height=480,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/5sK42VEQ_-_Copy_-_Copy.b8a1a4a9ca1f.jpeg",
        "url": "https://realpython.com/team/icurrie/",
        "affiliation": {
          "@type": "Organization",
          "@id": "https://realpython.com/#organization",
          "name": "Real Python",
          "url": "https://realpython.com",
          "logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
        }
      },
      {
        "@type": "Person",
        "name": "Kate Finegan",
        "image": "https://realpython.com/cdn-cgi/image/width=400,height=400,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/VZxEtUor_400x400.7169c68e3950.jpg",
        "url": "https://realpython.com/team/kfinegan/",
        "affiliation": {
          "@type": "Organization",
          "@id": "https://realpython.com/#organization",
          "name": "Real Python",
          "url": "https://realpython.com",
          "logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
        }
      }
    ]
  },
  "datePublished": "2022-08-24T14:00:00+00:00",
  "dateModified": "2022-11-10T22:05:07.584497+00:00",
  "publisher": {
    "@type": "Organization",
    "@id": "https://realpython.com/#organization",
    "name": "Real Python",
    "url": "https://realpython.com",
    "logo": {
      "@type": "ImageObject",
      "url": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png",
      "width": 512,
      "height": 512
    },
    "description": "Real Python is a leading provider of online Python education and one of the largest language-specific online communities for software developers. It publishes high-quality learning resources, such as tutorials, books, and courses to an audience of millions of developers, data scientists, and machine learning engineers each month.",
    "slogan": "Become a Python Expert",
    "email": "info@realpython.com",
    "sameAs": [
      "https://github.com/realpython",
      "https://www.youtube.com/realpython",
      "https://twitter.com/realpython",
      "https://x.com/realpython",
      "https://www.linkedin.com/company/realpython-com/",
      "https://www.facebook.com/learnrealpython",
      "https://www.instagram.com/realpython",
      "https://www.tiktok.com/@realpython.com"
    ]
  },
  "author": {
    "@type": "Person",
    "name": "Leodanis Pozo Ramos",
    "image": "https://realpython.com/cdn-cgi/image/width=862,height=862,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/Perfil_final1.9f896bc212f6.jpg",
    "url": "https://realpython.com/team/lpozoramos/",
    "affiliation": {
      "@type": "Organization",
      "@id": "https://realpython.com/#organization",
      "name": "Real Python",
      "url": "https://realpython.com",
      "logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
    }
  },
  "description": "In this tutorial, you'll learn how to use Python's built-in exec() function to execute code that comes as either a string or a compiled code object."
}

authorReal Python
twitter:cardsummary_large_image
twitter:imagehttps://files.realpython.com/media/How-to-Use-Pythons-exec-Function_Watermarked.15c3fc76b92a.jpg
og:imagehttps://files.realpython.com/media/How-to-Use-Pythons-exec-Function_Watermarked.15c3fc76b92a.jpg
twitter:creator@realpython
og:typearticle

Links:

https://realpython.com/
Start Herehttps://realpython.com/start-here/
Learn Python https://realpython.com/python-exec/
Python Tutorials →In-depth articles and video courseshttps://realpython.com/search?kind=article&kind=course&order=newest
Learning Paths →Guided study plans for accelerated learninghttps://realpython.com/learning-paths/
Quizzes & Exercises →Check your learning progresshttps://realpython.com/quizzes/
Browse Topics →Focus on a specific area or skill levelhttps://realpython.com/tutorials/all/
Community Chat →Learn with other Pythonistashttps://realpython.com/community/
Office Hours →Live Q&A calls with Python expertshttps://realpython.com/office-hours/
Podcast →Hear what’s new in the world of Pythonhttps://realpython.com/podcasts/rpp/
Books →Round out your knowledge and learn offlinehttps://realpython.com/products/books/
Reference →Concise definitions for common Python termshttps://realpython.com/ref/
Code Mentor →BetaPersonalized code assistance & learning toolshttps://realpython.com/mentor/
Unlock All Content →https://realpython.com/account/join/
More https://realpython.com/python-exec/
Learner Storieshttps://realpython.com/learner-stories/
Python Newsletterhttps://realpython.com/newsletter/
Python Job Boardhttps://www.pythonjobshq.com
Meet the Teamhttps://realpython.com/team/
Become a Tutorial Writerhttps://realpython.com/write-for-us/
Become a Video Instructorhttps://realpython.com/become-an-instructor/
Searchhttps://realpython.com/search
https://realpython.com/search
Joinhttps://realpython.com/account/join/
Sign‑Inhttps://realpython.com/account/login/?next=%2Fpython-exec%2F
Browse Topicshttps://realpython.com/tutorials/all/
Guided Learning Pathshttps://realpython.com/learning-paths/
Basicshttps://realpython.com/search?level=basics
Intermediatehttps://realpython.com/search?level=intermediate
Advancedhttps://realpython.com/search?level=advanced
aihttps://realpython.com/tutorials/ai/
algorithmshttps://realpython.com/tutorials/algorithms/
apihttps://realpython.com/tutorials/api/
best-practiceshttps://realpython.com/tutorials/best-practices/
careerhttps://realpython.com/tutorials/career/
communityhttps://realpython.com/tutorials/community/
databaseshttps://realpython.com/tutorials/databases/
data-sciencehttps://realpython.com/tutorials/data-science/
data-structureshttps://realpython.com/tutorials/data-structures/
data-vizhttps://realpython.com/tutorials/data-viz/
devopshttps://realpython.com/tutorials/devops/
djangohttps://realpython.com/tutorials/django/
dockerhttps://realpython.com/tutorials/docker/
editorshttps://realpython.com/tutorials/editors/
flaskhttps://realpython.com/tutorials/flask/
front-endhttps://realpython.com/tutorials/front-end/
gamedevhttps://realpython.com/tutorials/gamedev/
guihttps://realpython.com/tutorials/gui/
machine-learninghttps://realpython.com/tutorials/machine-learning/
newshttps://realpython.com/tutorials/news/
numpyhttps://realpython.com/tutorials/numpy/
projectshttps://realpython.com/tutorials/projects/
pythonhttps://realpython.com/tutorials/python/
stdlibhttps://realpython.com/tutorials/stdlib/
testinghttps://realpython.com/tutorials/testing/
toolshttps://realpython.com/tutorials/tools/
web-devhttps://realpython.com/tutorials/web-dev/
web-scrapinghttps://realpython.com/tutorials/web-scraping/
Table of Contentshttps://realpython.com/python-exec/#toc
Getting to Know Python’s exec()https://realpython.com/python-exec/#getting-to-know-pythons-exec
Running Code From a String Inputhttps://realpython.com/python-exec/#running-code-from-a-string-input
Executing Compiled Codehttps://realpython.com/python-exec/#executing-compiled-code
Running Code From Python Source Fileshttps://realpython.com/python-exec/#running-code-from-python-source-files
Using the globals and locals Argumentshttps://realpython.com/python-exec/#using-the-globals-and-locals-arguments
Uncovering and Minimizing the Security Risks Behind exec()https://realpython.com/python-exec/#uncovering-and-minimizing-the-security-risks-behind-exec
Avoiding Input From Untrusted Sourceshttps://realpython.com/python-exec/#avoiding-input-from-untrusted-sources
Restricting globals and locals to Minimize Riskshttps://realpython.com/python-exec/#restricting-globals-and-locals-to-minimize-risks
Deciding on Allowed Built-in Nameshttps://realpython.com/python-exec/#deciding-on-allowed-built-in-names
Putting exec() Into Actionhttps://realpython.com/python-exec/#putting-exec-into-action
Running Code From External Sourceshttps://realpython.com/python-exec/#running-code-from-external-sources
Using Python for Configuration Fileshttps://realpython.com/python-exec/#using-python-for-configuration-files
Conclusionhttps://realpython.com/python-exec/#conclusion
https://realpython.com/feedback/survey/article/python-exec/liked/?from=article-sidebar
https://realpython.com/feedback/survey/article/python-exec/disliked/?from=article-sidebar
Leodanis Pozo Ramoshttps://realpython.com/python-exec/#author
https://realpython.com/python-exec/#reader-comments
intermediatehttps://realpython.com/tutorials/intermediate/
pythonhttps://realpython.com/tutorials/python/
Getting to Know Python’s exec()https://realpython.com/python-exec/#getting-to-know-pythons-exec
Running Code From a String Inputhttps://realpython.com/python-exec/#running-code-from-a-string-input
Executing Compiled Codehttps://realpython.com/python-exec/#executing-compiled-code
Running Code From Python Source Fileshttps://realpython.com/python-exec/#running-code-from-python-source-files
Using the globals and locals Argumentshttps://realpython.com/python-exec/#using-the-globals-and-locals-arguments
Uncovering and Minimizing the Security Risks Behind exec()https://realpython.com/python-exec/#uncovering-and-minimizing-the-security-risks-behind-exec
Avoiding Input From Untrusted Sourceshttps://realpython.com/python-exec/#avoiding-input-from-untrusted-sources
Restricting globals and locals to Minimize Riskshttps://realpython.com/python-exec/#restricting-globals-and-locals-to-minimize-risks
Deciding on Allowed Built-in Nameshttps://realpython.com/python-exec/#deciding-on-allowed-built-in-names
Putting exec() Into Actionhttps://realpython.com/python-exec/#putting-exec-into-action
Running Code From External Sourceshttps://realpython.com/python-exec/#running-code-from-external-sources
Using Python for Configuration Fileshttps://realpython.com/python-exec/#using-python-for-configuration-files
Conclusionhttps://realpython.com/python-exec/#conclusion
Remove adshttps://realpython.com/account/join/
namespaceshttps://realpython.com/python-namespaces-scope/
scopehttps://realpython.com/python-scope-legb-rule/
stringshttps://realpython.com/python-strings/
built-in functionshttps://docs.python.org/3/library/functions.html
Click here to download the free sample codehttps://realpython.com/bonus/python-exec-code/
https://realpython.com/python-exec/#getting-to-know-pythons-exec
exec()https://docs.python.org/3/library/functions.html#exec
eval()https://realpython.com/python-eval-function/
expressionshttps://realpython.com/python-operators-expressions/
importshttps://realpython.com/python-import/
interpretedhttps://en.wikipedia.org/wiki/Interpreter_(computing)
compiledhttps://en.wikipedia.org/wiki/Compiled_language
bytecodehttps://docs.python.org/3/glossary.html#term-bytecode
CPythonhttps://realpython.com/cpython-source-code-guide/
virtual machinehttps://docs.python.org/3/glossary.html#term-virtual-machine
stringhttps://realpython.com/python-strings/
globalhttps://realpython.com/python-scope-legb-rule/#modules-the-global-scope
localhttps://realpython.com/python-scope-legb-rule/#functions-the-local-scope
returnhttps://realpython.com/python-return-statement/
Nonehttps://realpython.com/null-in-python/
side effectshttps://en.wikipedia.org/wiki/Side_effect_(computer_science)
whilehttps://realpython.com/python-while-loop/
interpreterhttps://realpython.com/interacting-with-python/#starting-the-interpreter
REPLhttps://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop
input()https://docs.python.org/3/library/functions.html#input
security riskshttps://realpython.com/python-exec/#uncovering-and-minimizing-the-security-risks-behind-exec
Remove adshttps://realpython.com/account/join/
https://realpython.com/python-exec/#running-code-from-a-string-input
one-liner programhttps://en.wikipedia.org/wiki/One-liner_program
generator expressionhttps://realpython.com/introduction-to-python-generators/#building-generators-with-generator-expressions
sum()https://realpython.com/python-sum-function/
statementshttps://docs.python.org/3/reference/simple_stmts.html#simple-statements
printshttps://realpython.com/python-print/
invalid syntaxhttps://realpython.com/invalid-syntax-python/
Remove adshttps://realpython.com/account/join/
https://realpython.com/python-exec/#executing-compiled-code
compile()https://docs.python.org/3/library/functions.html#compile
documentationhttps://docs.python.org/3/library/functions.html?highlight=built#compile
https://realpython.com/python-exec/#running-code-from-python-source-files
filehttps://realpython.com/working-with-files-in-python/
open()https://realpython.com/working-with-files-in-python/#pythons-with-open-as-pattern
with statementhttps://realpython.com/python-with-statement/
.read()https://docs.python.org/3/library/io.html#io.TextIOBase.read
variablehttps://realpython.com/python-variables/
Dynamic Importshttps://realpython.com/python-import/#dynamic-imports
Remove adshttps://realpython.com/account/join/
https://realpython.com/python-exec/#using-the-globals-and-locals-arguments
namespaceshttps://realpython.com/python-namespaces-scope/
scopehttps://realpython.com/python-scope-legb-rule/
built-in scopehttps://realpython.com/python-scope-legb-rule/#builtins-the-built-in-scope
mappinghttps://docs.python.org/3/glossary.html#term-mapping
https://realpython.com/python-exec/#uncovering-and-minimizing-the-security-risks-behind-exec
Remove adshttps://realpython.com/account/join/
https://realpython.com/python-exec/#avoiding-input-from-untrusted-sources
https://realpython.com/python-exec/#restricting-globals-and-locals-to-minimize-risks
sectionhttps://realpython.com/python-exec/#using-the-globals-and-locals-arguments
min()https://realpython.com/python-min-and-max/
len()https://realpython.com/len-python-function/
print()https://realpython.com/python-print/
https://realpython.com/python-exec/#deciding-on-allowed-built-in-names
Remove adshttps://realpython.com/account/join/
https://realpython.com/python-exec/#putting-exec-into-action
https://realpython.com/python-exec/#running-code-from-external-sources
timeithttps://docs.python.org/3/library/timeit.html#module-timeit
Guido van Rossumhttps://twitter.com/gvanrossum
Timerhttps://docs.python.org/3/library/timeit.html#timeit.Timer
timeithttps://github.com/python/cpython/blob/main/Lib/timeit.py
initializerhttps://realpython.com/python-class-constructor/#object-initialization-with-__init__
doctesthttps://realpython.com/python-doctest/
interactivehttps://realpython.com/interacting-with-python/
docstringhttps://realpython.com/how-to-write-docstrings-in-python/
source codehttps://github.com/python/cpython/blob/main/Lib/doctest.py
Remove adshttps://realpython.com/account/join/
https://realpython.com/python-exec/#using-python-for-configuration-files
configparserhttps://docs.python.org/3/library/configparser.html#module-configparser
INI file formathttps://en.wikipedia.org/wiki/INI_file
https://realpython.com/python-exec/#conclusion
Click here to download the free sample codehttps://realpython.com/bonus/python-exec-code/
https://realpython.com/feedback/survey/article/python-exec/liked/?from=article-footer
https://realpython.com/feedback/survey/article/python-exec/disliked/?from=article-footer
https://realpython.com/team/lpozoramos/
» More about Leodanishttps://realpython.com/team/lpozoramos/
https://realpython.com/team/asantos/
Aldrenhttps://realpython.com/team/asantos/
https://realpython.com/team/gahjelle/
Geir Arnehttps://realpython.com/team/gahjelle/
https://realpython.com/team/icurrie/
Ianhttps://realpython.com/team/icurrie/
https://realpython.com/team/kfinegan/
Katehttps://realpython.com/team/kfinegan/
Level Up Your Python Skills »https://realpython.com/account/join/?utm_source=rp_article_footer&utm_content=python-exec
Level Up Your Python Skills »https://realpython.com/account/join/?utm_source=rp_article_footer&utm_content=python-exec
https://realpython.com/feedback/survey/article/python-exec/liked/?from=article-comments
https://realpython.com/feedback/survey/article/python-exec/disliked/?from=article-comments
LinkedInhttps://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Frealpython.com%2Fpython-exec%2F
Twitterhttps://twitter.com/intent/tweet/?text=Interesting%20Python%20article%20on%20%40realpython%3A%20Python%27s%20exec%28%29%3A%20Execute%20Dynamically%20Generated%20Code&url=https%3A%2F%2Frealpython.com%2Fpython-exec%2F
Blueskyhttps://bsky.app/intent/compose?text=Interesting%20Python%20article%20on%20%40realpython.com%3A%20Python%27s%20exec%28%29%3A%20Execute%20Dynamically%20Generated%20Code%20https%3A%2F%2Frealpython.com%2Fpython-exec%2F
Facebookhttps://facebook.com/sharer/sharer.php?u=https%3A%2F%2Frealpython.com%2Fpython-exec%2F
Get tips for asking good questionshttps://realpython.com/python-beginner-tips/#tip-9-ask-good-questions
get answers to common questions in our support portalhttps://support.realpython.com
Real Python Community Chathttps://realpython.com/community/
“Office Hours” Live Q&A Sessionhttps://realpython.com/office-hours/
intermediatehttps://realpython.com/tutorials/intermediate/
pythonhttps://realpython.com/tutorials/python/
Python's property(): Add Managed Attributes to Your Classeshttps://realpython.com/python-property/?utm_source=realpython&utm_medium=web&utm_campaign=related-post&utm_content=python-exec
The subprocess Module: Wrapping Programs With Pythonhttps://realpython.com/python-subprocess/?utm_source=realpython&utm_medium=web&utm_campaign=related-post&utm_content=python-exec
How to Run Your Python Scripts and Codehttps://realpython.com/run-python-scripts/?utm_source=realpython&utm_medium=web&utm_campaign=related-post&utm_content=python-exec
Python Modules and Packages – An Introductionhttps://realpython.com/python-modules-packages/?utm_source=realpython&utm_medium=web&utm_campaign=related-post&utm_content=python-exec
Python's assert: Debug and Test Your Code Like a Prohttps://realpython.com/python-assert-statement/?utm_source=realpython&utm_medium=web&utm_campaign=related-post&utm_content=python-exec
https://realpython.com/account/signup/?intent=continue_reading&utm_source=rp&utm_medium=web&utm_campaign=rwn&utm_content=v1&next=%2Fpython-exec%2F
Continue »https://realpython.com/account/signup/?intent=continue_reading&utm_source=rp&utm_medium=web&utm_campaign=rwn&utm_content=v1&next=%2Fpython-exec%2F
Sign-Inhttps://realpython.com/account/login/?next=/python-exec/
Start Herehttps://realpython.com/start-here/
Learning Resourceshttps://realpython.com/search
Code Mentorhttps://realpython.com/mentor/
Python Referencehttps://realpython.com/ref/
Python Cheat Sheethttps://realpython.com/cheatsheets/python/
Support Centerhttps://support.realpython.com/
Learning Pathshttps://realpython.com/learning-paths/
Quizzes & Exerciseshttps://realpython.com/quizzes/
Browse Topicshttps://realpython.com/tutorials/all/
Live Courseshttps://realpython.com/live/
Bookshttps://realpython.com/books/
Podcasthttps://realpython.com/podcasts/rpp/
Newsletterhttps://realpython.com/newsletter/
Community Chathttps://realpython.com/community/
Office Hourshttps://realpython.com/office-hours/
Learner Storieshttps://realpython.com/learner-stories/
Plans & Pricinghttps://realpython.com/account/join/
Team Planshttps://realpython.com/account/join-team/
For Businesshttps://realpython.com/account/join-team/inquiry/
For Schoolshttps://realpython.com/account/join-team/education-inquiry/
Reviewshttps://realpython.com/learner-stories/
About Ushttps://realpython.com/about/
Teamhttps://realpython.com/team/
Mission & Valueshttps://realpython.com/mission/
Editorial Guidelineshttps://realpython.com/editorial-guidelines/
Sponsorshipshttps://realpython.com/sponsorships/
Careershttps://realpython.workable.com
Press Kithttps://realpython.com/media-kit/
Merchhttps://realpython.com/merch
https://www.youtube.com/realpython
https://x.com/realpython
https://www.linkedin.com/company/realpython-com
https://www.facebook.com/LearnRealPython
https://github.com/realpython/
Privacy Policyhttps://realpython.com/privacy-policy/
Terms of Usehttps://realpython.com/terms/
Securityhttps://realpython.com/security/
Contacthttps://realpython.com/contact/
https://realpython.com/

Viewport: width=device-width, initial-scale=1, shrink-to-fit=no, viewport-fit=cover

Robots: max-image-preview:large


URLs of crawlers that visited me.