|
| https://realpython.com/ |
| Start Here | https://realpython.com/start-here/ |
|
Learn Python
| https://realpython.com/lessons/execution-modes-python/ |
| Your Learning Plan →Your personalized Python learning plan | https://realpython.com/account/learning-plan/ |
| Python Tutorials →In-depth articles and video courses | https://realpython.com/search?kind=article&kind=course&order=newest&_from=nav_library |
| Learning Paths →Guided study plans for accelerated learning | https://realpython.com/learning-paths/ |
| Quizzes & Exercises →Check your learning progress | https://realpython.com/search?kind=exercise&kind=quiz&_from=nav_library |
| Browse Topics →Focus on a specific area or skill level | https://realpython.com/tutorials/all/ |
| Reference →Concise definitions for common Python terms | https://realpython.com/ref/ |
| Mentor AI →NewYour personal Python mentor | https://realpython.com/mentor/ |
| Office Hours →Live Q&A calls with Python experts | https://realpython.com/office-hours/ |
| Community Chat →Learn with other Pythonistas | https://realpython.com/community/ |
| Live Courses →Live, instructor-led Python courses | https://realpython.com/live/ |
| Books →Round out your knowledge and learn offline | https://realpython.com/products/books/ |
| Podcast →Hear what’s new in the world of Python | https://realpython.com/podcasts/rpp/ |
| Unlock All Content and Become a Python Expert →Get unlimited access to all Real Python has to offer | https://realpython.com/account/join/?from=navbar_learn |
| Unlock All Content →Become a Python Expert | https://realpython.com/account/join/?from=navbar_learn |
|
More
| https://realpython.com/lessons/execution-modes-python/ |
| Learner Stories | https://realpython.com/learner-stories/ |
| Python Newsletter | https://realpython.com/newsletter/ |
| Python Job Board | https://www.pythonjobshq.com |
| Meet the Team | https://realpython.com/team/ |
| Become a Contributor | https://realpython.com/jobs/ |
| Search | https://realpython.com/search |
| https://realpython.com/search |
| Join | https://realpython.com/account/join/ |
| Sign‑In | https://realpython.com/account/login/?intent=navbar&next=%2Flessons%2Fexecution-modes-python%2F |
| Unlock This Lesson | https://realpython.com/account/join/?utm_source=rp_lesson&utm_content=python-main-function |
| Unlock This Lesson | https://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 Python | https://realpython.com/courses/python-main-function/ |
| Rich Bibby | https://realpython.com/courses/python-main-function/#team |
| Recommended Tutorial | https://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 Question | https://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 |
| Contents | https://realpython.com/lessons/execution-modes-python/#description |
| Transcript | https://realpython.com/lessons/execution-modes-python/#transcript |
| Discussion | https://realpython.com/lessons/execution-modes-python/#discussion |
| script | https://realpython.com/courses/running-python-scripts/ |
| command line | https://realpython.com/python-command-line-arguments/ |
| import | https://realpython.com/courses/absolute-vs-relative-imports-python/ |
| 00:00 | https://realpython.com/lessons/execution-modes-python/#t=0.51 |
| There are two primary ways that you can instruct the Python interpreter to | https://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 the | https://realpython.com/lessons/execution-modes-python/#t=9.69 |
| interactive interpreter. | https://realpython.com/lessons/execution-modes-python/#t=14.43 |
| 00:16 | https://realpython.com/lessons/execution-modes-python/#t=16.86 |
| No matter which way of running your code you’re using, Python defines a special | https://realpython.com/lessons/execution-modes-python/#t=16.86 |
| variable called __name__ that contains a string whose value depends on how the code | https://realpython.com/lessons/execution-modes-python/#t=20.76 |
| is being used. | https://realpython.com/lessons/execution-modes-python/#t=25.62 |
| 00:27 | https://realpython.com/lessons/execution-modes-python/#t=27.21 |
| We’ll use this example file, saved as execution_methods.py, to | https://realpython.com/lessons/execution-modes-python/#t=27.21 |
| explore how the behavior of the code changes | https://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:41 | https://realpython.com/lessons/execution-modes-python/#t=41.778 |
| The first two print some | https://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 phrase | https://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’s | https://realpython.com/lessons/execution-modes-python/#t=49.35 |
| built-in repr() function. | https://realpython.com/lessons/execution-modes-python/#t=53.64 |
| 00:55 | https://realpython.com/lessons/execution-modes-python/#t=55.48 |
| In Python, the repr() function displays the printable | https://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:06 | https://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:11 | https://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 of | https://realpython.com/lessons/execution-modes-python/#t=78.87 |
| code. | https://realpython.com/lessons/execution-modes-python/#t=82.89 |
| 01:23 | https://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 the | https://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 the | https://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:37 | https://realpython.com/lessons/execution-modes-python/#t=97.11 |
| A Python module is a file that you intend to import from within another | https://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:49 | https://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 Python | https://realpython.com/lessons/execution-modes-python/#t=115.14 |
| interpreter is executing. | https://realpython.com/lessons/execution-modes-python/#t=118.5 |
| 02:00 | https://realpython.com/lessons/execution-modes-python/#t=120.51 |
| Let’s try this by executing execution_methods.py from the | https://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:14 | https://realpython.com/lessons/execution-modes-python/#t=134.79 |
| Remember that in Python there’s no difference between strings defined with | https://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:21 | https://realpython.com/lessons/execution-modes-python/#t=141.42 |
| You may also see Python scripts executed from within packages by adding the -m | https://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 install | https://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 a | https://realpython.com/lessons/execution-modes-python/#t=152.01 |
| package. | https://realpython.com/lessons/execution-modes-python/#t=157.02 |
| 02:37 | https://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 value | https://realpython.com/lessons/execution-modes-python/#t=162.93 |
| '__main__'. | https://realpython.com/lessons/execution-modes-python/#t=167.22 |
| 02:48 | https://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 of | https://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:04 | https://realpython.com/lessons/execution-modes-python/#t=184.27 |
| You can read more about these attributes in the Python Data Model documentation | https://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:14 | https://realpython.com/lessons/execution-modes-python/#t=194.71 |
| Now let’s take a look at the second way that the Python interpreter will execute | https://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 has | https://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:29 | https://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:38 | https://realpython.com/lessons/execution-modes-python/#t=218.132 |
| To | https://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 interpreter | https://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:49 | https://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 file | https://realpython.com/lessons/execution-modes-python/#t=235.54 |
| as a script in the command line | https://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:04 | https://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 being | https://realpython.com/lessons/execution-modes-python/#t=250.84 |
| imported. | https://realpython.com/lessons/execution-modes-python/#t=255.4 |
| 04:16 | https://realpython.com/lessons/execution-modes-python/#t=256.54 |
| You can see this in the third line of the output. __name__ has the value | https://realpython.com/lessons/execution-modes-python/#t=256.54 |
| 'execution_methods', which is the name of the .py file | https://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:27 | https://realpython.com/lessons/execution-modes-python/#t=267.018 |
| It’s worth noting here that if you import the | https://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:35 | https://realpython.com/lessons/execution-modes-python/#t=275.32 |
| You can find more information on how importing works in Python in the official | https://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 Member | https://realpython.com/account/join/ |
| https://realpython.com/videos/python-main-function-overview/ |
| Overview | https://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 Policy | https://realpython.com/privacy-policy/ |
Viewport: width=device-width, initial-scale=1, shrink-to-fit=no, viewport-fit=cover