There are two primary ways you can instruct the Python interpreter to execute code:
Execution Modes in Python
00:00 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 interactive interpreter.
We’ll use this example file, saved as
execution_methods.py, to explore how the behavior of the code changes depending on the context. In this file, there are three calls to the
print() function defined. The first two print some introductory phrases. The third
print() will first print the phrase
"The value of __name__ is:", and it will then print the representation of the
__name__ variable using Python’s built-in
repr() function. In Python, the
repr() function displays the printable representation of an object.
You’ll see the words file, module, and script used throughout this tutorial. Practically, there isn’t much difference between them. However, there are slight differences in meaning that emphasize the purpose of a piece of code. A file is typically any file that contains code. Most Python files have the extension
.py. A Python script is a file that you intend to execute from the command line to accomplish a task.
01:49 First, let’s look at executing your Python script from the command line. When you execute a script, you will not be able to interactively define the code that the Python interpreter is executing.
Let’s try this by executing
execution_methods.py from the command line. In this example, you can see that
__name__ has the value
'__main__', where the quote symbols tell you that the value has the string type.
You may also see Python scripts executed from within packages by adding the
-m argument to the command. Most often, you will see this recommended when you’re using
pip to install packages, where adding the
-m argument runs the code in the
__main__.py module of a package. In all three cases,
__name__ has the same value, and it’s the string
The Python documentation defines specifically when
__name__ will have the value
'__main__'. It states that “A module’s
__name__ is set to equal
'__main__' when read from standard input, a script, or from an interactive prompt.”
__name__ is stored in the global namespace of the module, along with the
__package__, and other attributes.
Now let’s take a look at the second way that the Python interpreter will execute your code, which is imports. When you’re developing a module or a script, you will most likely want to take advantage of modules that someone else has already built, which you can do with the
import keyword. During the import process, Python executes the statements defined in the specified module—but you should note that this happens only the first time you import a module. To demonstrate the results of importing your
execution_methods.py file, let’s start the interactive Python interpreter and then import the
execution_methods file. In this code output, you can see that the Python interpreter executes the three calls to
The first two lines of output are exactly the same as when you executed the file as a script in the command line because there are no variables in either of the first two lines. However, there is a difference in the output from the third
When the Python interpreter imports code, the value of
__name__ is set to be the same as the name of the module that is being imported. You can see this in the third line of the output.
__name__ has the value
'execution_methods', which is the name of the
.py file that Python is importing from. It’s worth noting here that if you
import the module again without quitting Python, there will be no output.
Become a Member to join the conversation.