Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Execution Modes in Python

There are two primary ways you can instruct the Python interpreter to execute code:

  1. You can execute the Python file as a script using the command line.
  2. You can import the Python code from one file into another file or into the interactive interpreter.

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.

00:16 No matter which way of running your code you’re using, Python defines a special variable called __name__ that contains a string whose value depends on how the code is being used.

00:27 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.

01:01 This example uses repr() to emphasize that the value of __name__ is a string. You can read more about repr() in the Python documentation.

01:11 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:37 A Python module is a file that you intend to import from within another module or a script, or from the interactive interpreter. You can read more about modules in the Python documentation.

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.

02:00 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.

02:14 Remember that in Python there’s no difference between strings defined with single quotes (') and defined with double quotes (").

02:21 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 '__main__'.

02:42 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 __doc__, __package__, and other attributes.

03:04 You can read more about these attributes in the Python Data Model documentation and, specifically for modules and packages, in the Python Import documentation.

03:14 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 print().

03:55 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 print().

04:08 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.

04:35 You can find more information on how importing works in Python in the official Python documentation. Next up, you will learn some best practices for Python main() functions.

Become a Member to join the conversation.