René's URL Explorer Experiment


Title: Execution Modes in Python (Video) – Real Python

Open Graph Title: Execution Modes in Python – Real Python

Description: There are two primary ways that you can instruct the Python interpreter to execute or use code. You can execute the Python files or scripts using the command line, or you can input the code from one Python file into another file or into the…

Open Graph Description: There are two primary ways that you can instruct the Python interpreter to execute or use code. You can execute the Python files or scripts using the command line, or you can input the code from one Python file into another file or into the…

Opengraph URL: https://realpython.com/lessons/execution-modes-python/

X: @realpython

direct link

Domain: realpython.com


Hey, it has json ld scripts:
  {
    "@context": "https://schema.org",
    "@type": "VideoObject",
    "name": "Execution Modes in Python",
    "description": "There are two primary ways that you can instruct the Python interpreter to execute or use code. You can execute the Python files or scripts using the command line, or you can input the code from one Python file into another file or into the…",
    "thumbnailUrl": ["https://files.realpython.com/media/Python-Main-Function_Watermarked.5b31a1de4516.jpg"],
    "uploadDate": "2020-03-10T14:00:00+00:00",
    "duration": "PT4M48S",
    
    "potentialAction": {
      "@type": "SeekToAction",
      "target": "https://realpython.com/lessons/execution-modes-python/#t={seek_to_second_number}",
      "startOffset-input": "required name=seek_to_second_number"
    }
  }
  

authorReal Python
theme-color#18354c
color-schemelight dark
twitter:cardsummary_large_image
twitter:imagehttps://files.realpython.com/media/Python-Main-Function_Watermarked.5b31a1de4516.jpg
og:imagehttps://files.realpython.com/media/Python-Main-Function_Watermarked.5b31a1de4516.jpg
twitter:creator@realpython
og:typevideo.episode
og:site_nameReal Python
apple-mobile-web-app-titleReal Python

Links:

https://realpython.com/
Start Herehttps://realpython.com/start-here/
Learn Python https://realpython.com/lessons/execution-modes-python/
Your Learning Plan →Your personalized Python learning planhttps://realpython.com/account/learning-plan/
Python Tutorials →In-depth articles and video courseshttps://realpython.com/search?kind=article&kind=course&order=newest&_from=nav_library
Learning Paths →Guided study plans for accelerated learninghttps://realpython.com/learning-paths/
Quizzes & Exercises →Check your learning progresshttps://realpython.com/search?kind=exercise&kind=quiz&_from=nav_library
Browse Topics →Focus on a specific area or skill levelhttps://realpython.com/tutorials/all/
Reference →Concise definitions for common Python termshttps://realpython.com/ref/
Mentor AI →NewYour personal Python mentorhttps://realpython.com/mentor/
Office Hours →Live Q&A calls with Python expertshttps://realpython.com/office-hours/
Community Chat →Learn with other Pythonistashttps://realpython.com/community/
Live Courses →Live, instructor-led Python courseshttps://realpython.com/live/
Books →Round out your knowledge and learn offlinehttps://realpython.com/products/books/
Podcast →Hear what’s new in the world of Pythonhttps://realpython.com/podcasts/rpp/
Unlock All Content and Become a Python Expert →Get unlimited access to all Real Python has to offerhttps://realpython.com/account/join/?from=navbar_learn
Unlock All Content →Become a Python Experthttps://realpython.com/account/join/?from=navbar_learn
More https://realpython.com/lessons/execution-modes-python/
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 Contributorhttps://realpython.com/jobs/
Searchhttps://realpython.com/search
https://realpython.com/search
Joinhttps://realpython.com/account/join/
Sign‑Inhttps://realpython.com/account/login/?intent=navbar&next=%2Flessons%2Fexecution-modes-python%2F
Unlock This Lessonhttps://realpython.com/account/join/?utm_source=rp_lesson&utm_content=python-main-function
Unlock This Lessonhttps://realpython.com/account/join/?utm_source=rp_lesson&utm_content=python-main-function
https://realpython.com/courses/python-main-function/#team
Defining Main Functions in Pythonhttps://realpython.com/courses/python-main-function/
Rich Bibbyhttps://realpython.com/courses/python-main-function/#team
Recommended Tutorialhttps://realpython.com/python-main-function/
Course Slides (.pdf)https://realpython.com/courses/python-main-function/downloads/main-slides/
Sample Code (.zip)https://realpython.com/courses/python-main-function/downloads/main-code/
Ask a Questionhttps://realpython.com/lessons/execution-modes-python/#discussion
https://realpython.com/feedback/survey/course/python-main-function/liked/?from=lesson-title
https://realpython.com/feedback/survey/course/python-main-function/disliked/?from=lesson-title
Contentshttps://realpython.com/lessons/execution-modes-python/#description
Transcripthttps://realpython.com/lessons/execution-modes-python/#transcript
Discussionhttps://realpython.com/lessons/execution-modes-python/#discussion
scripthttps://realpython.com/courses/running-python-scripts/
command linehttps://realpython.com/python-command-line-arguments/
importhttps://realpython.com/courses/absolute-vs-relative-imports-python/
00:00https://realpython.com/lessons/execution-modes-python/#t=0.51
There are two primary ways that you can instruct the Python interpreter tohttps://realpython.com/lessons/execution-modes-python/#t=0.51
execute or use code.https://realpython.com/lessons/execution-modes-python/#t=3.7800000000000002
You can execute the Python files or scripts using the command line,https://realpython.com/lessons/execution-modes-python/#t=5.82
or you can input the code from one Python file into another file or into thehttps://realpython.com/lessons/execution-modes-python/#t=9.69
interactive interpreter.https://realpython.com/lessons/execution-modes-python/#t=14.43
00:16https://realpython.com/lessons/execution-modes-python/#t=16.86
No matter which way of running your code you’re using, Python defines a specialhttps://realpython.com/lessons/execution-modes-python/#t=16.86
variable called __name__ that contains a string whose value depends on how the codehttps://realpython.com/lessons/execution-modes-python/#t=20.76
is being used.https://realpython.com/lessons/execution-modes-python/#t=25.62
00:27https://realpython.com/lessons/execution-modes-python/#t=27.21
We’ll use this example file, saved as execution_methods.py, tohttps://realpython.com/lessons/execution-modes-python/#t=27.21
explore how the behavior of the code changeshttps://realpython.com/lessons/execution-modes-python/#t=32.729
depending on the context.https://realpython.com/lessons/execution-modes-python/#t=35.49
In this file,https://realpython.com/lessons/execution-modes-python/#t=37.362
there are three calls to the print() function defined.https://realpython.com/lessons/execution-modes-python/#t=38.4
00:41https://realpython.com/lessons/execution-modes-python/#t=41.778
The first two print somehttps://realpython.com/lessons/execution-modes-python/#t=41.778
introductory phrases.https://realpython.com/lessons/execution-modes-python/#t=43.02
The third print() will first print the phrasehttps://realpython.com/lessons/execution-modes-python/#t=44.193
"The value of __name__ is:",https://realpython.com/lessons/execution-modes-python/#t=47.37
and it will then print the representation of the __name__ variable using Python’shttps://realpython.com/lessons/execution-modes-python/#t=49.35
built-in repr() function.https://realpython.com/lessons/execution-modes-python/#t=53.64
00:55https://realpython.com/lessons/execution-modes-python/#t=55.48
In Python, the repr() function displays the printablehttps://realpython.com/lessons/execution-modes-python/#t=55.48
representation of an object.https://realpython.com/lessons/execution-modes-python/#t=58.74
This example uses repr() to emphasize that the value of __name__ is a string.https://realpython.com/lessons/execution-modes-python/#t=61.26
01:06https://realpython.com/lessons/execution-modes-python/#t=66.3
You can read more about repr() in the Python documentation.https://realpython.com/lessons/execution-modes-python/#t=66.3
01:11https://realpython.com/lessons/execution-modes-python/#t=71.04
You’ll see the words file, module, and script used throughout this tutorial.https://realpython.com/lessons/execution-modes-python/#t=71.04
Practically, there isn’t much difference between them.https://realpython.com/lessons/execution-modes-python/#t=75.87
However,https://realpython.com/lessons/execution-modes-python/#t=78.448
there are slight differences in meaning that emphasize the purpose of a piece ofhttps://realpython.com/lessons/execution-modes-python/#t=78.87
code.https://realpython.com/lessons/execution-modes-python/#t=82.89
01:23https://realpython.com/lessons/execution-modes-python/#t=83.32
A file is typically any file that contains code.https://realpython.com/lessons/execution-modes-python/#t=83.32
Most Python files have thehttps://realpython.com/lessons/execution-modes-python/#t=86.834
extension .py.https://realpython.com/lessons/execution-modes-python/#t=88.77
A Python script is a file that you intend to execute from thehttps://realpython.com/lessons/execution-modes-python/#t=90.562
command line to accomplish a task.https://realpython.com/lessons/execution-modes-python/#t=94.53
01:37https://realpython.com/lessons/execution-modes-python/#t=97.11
A Python module is a file that you intend to import from within anotherhttps://realpython.com/lessons/execution-modes-python/#t=97.11
module or a script, or from the interactive interpreter.https://realpython.com/lessons/execution-modes-python/#t=101.34
You can read more about modules in the Python documentation.https://realpython.com/lessons/execution-modes-python/#t=105.33
01:49https://realpython.com/lessons/execution-modes-python/#t=109.77
First, let’s look at executing your Python script from the command line.https://realpython.com/lessons/execution-modes-python/#t=109.77
When you execute a script,https://realpython.com/lessons/execution-modes-python/#t=113.85
you will not be able to interactively define the code that the Pythonhttps://realpython.com/lessons/execution-modes-python/#t=115.14
interpreter is executing.https://realpython.com/lessons/execution-modes-python/#t=118.5
02:00https://realpython.com/lessons/execution-modes-python/#t=120.51
Let’s try this by executing execution_methods.py from thehttps://realpython.com/lessons/execution-modes-python/#t=120.51
command line.https://realpython.com/lessons/execution-modes-python/#t=125.58
In this example, you can see that __name__ has the value '__main__',https://realpython.com/lessons/execution-modes-python/#t=126.216
where the quote symbols tell you that the value has the string type.https://realpython.com/lessons/execution-modes-python/#t=130.74
02:14https://realpython.com/lessons/execution-modes-python/#t=134.79
Remember that in Python there’s no difference between strings defined withhttps://realpython.com/lessons/execution-modes-python/#t=134.79
single quotes (') and defined with double quotes (").https://realpython.com/lessons/execution-modes-python/#t=138.18
02:21https://realpython.com/lessons/execution-modes-python/#t=141.42
You may also see Python scripts executed from within packages by adding the -mhttps://realpython.com/lessons/execution-modes-python/#t=141.42
argument to the command.https://realpython.com/lessons/execution-modes-python/#t=146.57
Most often, you will see this recommended when you’re using pip to installhttps://realpython.com/lessons/execution-modes-python/#t=147.81
packages,https://realpython.com/lessons/execution-modes-python/#t=151.2
where adding the -m argument runs the code in the __main__.py module of ahttps://realpython.com/lessons/execution-modes-python/#t=152.01
package.https://realpython.com/lessons/execution-modes-python/#t=157.02
02:37https://realpython.com/lessons/execution-modes-python/#t=157.523
In all three cases,https://realpython.com/lessons/execution-modes-python/#t=157.523
__name__ has the same value, and it’s the string '__main__'.https://realpython.com/lessons/execution-modes-python/#t=158.64
The Python documentation defines specifically when __name__ will have the valuehttps://realpython.com/lessons/execution-modes-python/#t=162.93
'__main__'.https://realpython.com/lessons/execution-modes-python/#t=167.22
02:48https://realpython.com/lessons/execution-modes-python/#t=168.187
It states that “A module’s __name__ is set to equal '__main__'https://realpython.com/lessons/execution-modes-python/#t=168.187
when read from standard input,https://realpython.com/lessons/execution-modes-python/#t=171.9
a script, or from an interactive prompt.” __name__ is stored in the global namespace ofhttps://realpython.com/lessons/execution-modes-python/#t=174.15
the module, along with the __doc__, __package__, and other attributes.https://realpython.com/lessons/execution-modes-python/#t=179.86
03:04https://realpython.com/lessons/execution-modes-python/#t=184.27
You can read more about these attributes in the Python Data Model documentationhttps://realpython.com/lessons/execution-modes-python/#t=184.27
and, specifically for modules and packages, in the Python Import documentation.https://realpython.com/lessons/execution-modes-python/#t=188.89
03:14https://realpython.com/lessons/execution-modes-python/#t=194.71
Now let’s take a look at the second way that the Python interpreter will executehttps://realpython.com/lessons/execution-modes-python/#t=194.71
your code, which is imports.https://realpython.com/lessons/execution-modes-python/#t=198.19
When you’re developing a module or a script,https://realpython.com/lessons/execution-modes-python/#t=199.812
you will most likely want to take advantage of modules that someone else hashttps://realpython.com/lessons/execution-modes-python/#t=202.48
already built,https://realpython.com/lessons/execution-modes-python/#t=205.72
which you can do with the import keyword.https://realpython.com/lessons/execution-modes-python/#t=206.8
03:29https://realpython.com/lessons/execution-modes-python/#t=209.134
During the import process,https://realpython.com/lessons/execution-modes-python/#t=209.134
Python executes the statements defined in the specified module—https://realpython.com/lessons/execution-modes-python/#t=210.31
but you should note that this happens only the first time you import a module.https://realpython.com/lessons/execution-modes-python/#t=213.97
03:38https://realpython.com/lessons/execution-modes-python/#t=218.132
Tohttps://realpython.com/lessons/execution-modes-python/#t=218.132
demonstrate the results of importing your execution_methods.py file,https://realpython.com/lessons/execution-modes-python/#t=218.29
let’s start the interactive Python interpreterhttps://realpython.com/lessons/execution-modes-python/#t=223.15
and then import the execution_methods file.https://realpython.com/lessons/execution-modes-python/#t=225.91
03:49https://realpython.com/lessons/execution-modes-python/#t=229.564
In this code output,https://realpython.com/lessons/execution-modes-python/#t=229.564
you can see that the Python interpreter executes the three calls to print().https://realpython.com/lessons/execution-modes-python/#t=231.25
The first two lines of output are exactly the same as when you executed the filehttps://realpython.com/lessons/execution-modes-python/#t=235.54
as a script in the command linehttps://realpython.com/lessons/execution-modes-python/#t=239.56
because there are no variables in either of the first two lines.https://realpython.com/lessons/execution-modes-python/#t=241.21
04:04https://realpython.com/lessons/execution-modes-python/#t=244.636
However,https://realpython.com/lessons/execution-modes-python/#t=244.636
there is a difference in the output from the third print().https://realpython.com/lessons/execution-modes-python/#t=245.29
When the Python interpreter imports code,https://realpython.com/lessons/execution-modes-python/#t=248.35
the value of __name__ is set to be the same as the name of the module that is beinghttps://realpython.com/lessons/execution-modes-python/#t=250.84
imported.https://realpython.com/lessons/execution-modes-python/#t=255.4
04:16https://realpython.com/lessons/execution-modes-python/#t=256.54
You can see this in the third line of the output. __name__ has the valuehttps://realpython.com/lessons/execution-modes-python/#t=256.54
'execution_methods', which is the name of the .py filehttps://realpython.com/lessons/execution-modes-python/#t=260.86
that Python is importing from.https://realpython.com/lessons/execution-modes-python/#t=265.54
04:27https://realpython.com/lessons/execution-modes-python/#t=267.018
It’s worth noting here that if you import thehttps://realpython.com/lessons/execution-modes-python/#t=267.018
module again without quitting Python, there will be no output.https://realpython.com/lessons/execution-modes-python/#t=269.83
04:35https://realpython.com/lessons/execution-modes-python/#t=275.32
You can find more information on how importing works in Python in the officialhttps://realpython.com/lessons/execution-modes-python/#t=275.32
Python documentation.https://realpython.com/lessons/execution-modes-python/#t=279.58
Next up, you will learn some best practices for Python main() functions.https://realpython.com/lessons/execution-modes-python/#t=281.83
Become a Memberhttps://realpython.com/account/join/
https://realpython.com/videos/python-main-function-overview/
Overviewhttps://realpython.com/courses/python-main-function/
https://realpython.com/lessons/best-practices-python-main-functions/
Defining Main Functions in Python: Overview 02:01 https://realpython.com/videos/python-main-function-overview/
Execution Modes in Python 04:48 https://realpython.com/lessons/execution-modes-python/
Best Practices for Python Main Functions 11:17 https://realpython.com/lessons/best-practices-python-main-functions/
Defining Main Functions in Python (Quiz) 04:00 https://realpython.com/lessons/defining-main-functions-in-python-quiz/
Defining Main Functions in Python: Summary 00:49 https://realpython.com/lessons/defining-main-functions-python-summary/
Privacy Policyhttps://realpython.com/privacy-policy/

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.