Python is one of the most popular programming languages in the world. It is extremely powerful, has easy syntax and lots of cool libraries that you can use for various tasks.
If you’re starting to learn Python, then knowing how to run your Python scripts is an important skill to learn.
If you don’t know how to run your scripts, you can’t verify whether they actually work. It is the only way to really test your programs.
In this step-by-step guide, you’ll learn everything you need to know about the basics of running Python scripts. We’ll discuss how the techniques used for running scripts differs based on the kind of environment, your operating system, and even your skills as a developer.
Table of Contents
You can skip to a specific section of this Python tutorial using the table of contents below:
There are two main types of files in Python: scripts and modules.
Before proceeding further, it is important to highlight the difference between the two. The next section will explore this dichotomy in detail.
Difference Between Scripts and Modules
When you create a new Python file to write a simple program that outputs a “Hello World” statement, then what you’re actually writing is a Python script.
This is a Python file that can be executed directly by the user using any of the techniques mentioned in the preceding section.
The script is also known as a top-level program file. It is processed by an interpreter, which parses and executes the file line-by-line in a sequential manner.
A module, on the other hand, is a Python file that is meant to be imported and used by Python scripts.
You can indded run a module by itself. However, that beats the purpose of creating module files.
The main difference between the two is that Python scripts are meant to be directly executed, while modules are imported and used by other files (namely scripts).
Knowing the difference between the two will allow you to understand how to effectively organize your Python programs.
Scripts are useful when you’re trying to build a short program. However, if you’re trying to better organize your program and want to write reusable code, then writing modules is advisable.
A Python script or module relies on an interpreter for its successful execution. The next section contains a brief discussion on Python interpreter.
What’s the Role of a Python Interpreter?
The interpreter basically executes the program from the start to finish and follows the instructions contained in the codebase. Before running scripts and modules, you need to install an interpreter on your system.
Downloading and installing Python from the official website installs the Python interpreter on your system. You can also install the interpreter using Anaconda, which helps in package management.
You can include a Python program in an application written in a completely different language. This is possible with the help of Python interpreters written in different programming languages.
All the interpreters perform the same task. They help in the execution of the Python code. Therefore, it is imperative to ensure that the interpreter is installed correctly on your system.
In the next sections, you’ll learn how you can run your Python code using different approaches. I’ll start by showing you how to run Python code in interactive sessions.
Run Python Interactively
The quickest way to run Python commands is by creating an interactive session on your terminal. Simply open the command-prompt or terminal on your system and type Python or Python3, depending on the type of Python you’ve installed.
Here’s how you can creating an interactive session on windows and run a small program that adds two variable and prints their out and also prints out hello world:
Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:37:02)[MSC v.1924 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license"for more information.
>>> x = 10
>>> y = 15
>>> print("Hello World")
If you see »> on your terminal, you know you’re in the interactive mode now.
In this mode, you can write and run Python without having to configure anything else.
The downside of using interacting mode is that the terminal or command prompts aren’t the best text editors. More importantly, all your code is lost when you close the session.
The interactive session should be used solely for testing small examples and basic logic. When using the interactive session, each command is evaluated immediately. This characteristic is really helpful if you’re a beginner who needs to verify your understanding of syntax and commands.
Using the interactive session, you can run scripts and modules as well by using the power of import. However, it will not be discussed in this tutorial.
To close the interactive session, use the quit() command and the session will end. You can also use the following key combinations to quit: ctrl + Z and then Enter for windows and ctrl + D and then Enter for linux based systems.
Run Python Using The Command Line
When you write programs in Python, you’re creating scripts and modules. Modern developers generally do not use interactive sessions because they do not store the code and logic after the interactive session has been terminated.
Python files can be created using any plain text editor. You can use the operating system’s native text editor or you can try out VS Code, which is one of my preferred text editors. The benefit of using a text editor like VS Code is that these editors have optional plugins that can help boost your productivity.
Another great feature provided by VS Code is intellisense. Although a controversial feature and many believe that it hampers the learning of beginners, intellisense is imperative for any serious projects. It autocompletes code and allows you to check methods.
Consider the following test script for the rest of the tutorial:
For Windows based systems, the first line, which is known as a shebang, is not required.
For Linux based systems, the shebang helps in identifying the program that will be used for running the file. In this case, this is Python3.
The script simply outputs Hello World. The script’s name is helloworld.py. We’ll use this as an example for the rest of this tutorial.
Using Python Command
You can run our helloworld Python script with the following command:
It’s that simple to run the script. The command will execute your script and all the associated modules and write the output in the terminal.
On windows, if it gives an error such as Python is not recognized as an internal or external command, operable program, or batch file then check your environment variables. This stackoverflow answer has more details.
If you want to store the output of your script for later use, you can use the angular bracket >. The command becomes:
This will store all the output in the output.txt file. You can view it later on at your leisure.
Run Python Using VS Code
As suggested earlier, when you’re doing serious programming then advanced text editors like VS Code and Sublime Text become a necessity.
Alongside the benefits of intellisense and plugins, another advantage of these text editors is that they allow you to run the code using native terminals built inside of them.
The default integrated development environment (IDE) that comes with Python is the IDLE. It provides all of the basic functionalities and is fast and reliable. However, the IDLE lacks an aesthicaly pleasing look. It’s also missing intellisense and other plugins that might boost your productivity.
Using the built-in terminals available with modern text editors is convenient when you’re coding and want to quickly test your program’s output. You don’t have to switch to a command prompt or terminal and can quickly type in your command to start the program.
Here’s the terminal of VS Code, running the script that was written earlier:
To learn more about how the advanced text editors work and how you can enjoy their in-built features, read their documentation.
Run Python From a File Manager
This is by far the simplest way to run a Python script. In Windows, you simply double-click the icon of the Python file on the file manager. Since Windows associates file types with their corresponding programs, it will automatically run it using Python’s interpreter.
The same trick can be performed on Linux, however, you will have to include the shebang and give the file execution privileges using chmod.
A quick warning is warranted here. Pay attention if your script is as simple as the one written for this tutorial. For short programs like this, you will probably just see a flash of the command prompt or terminal before the program concludes and the terminal is closed.
A work around this situation is to add a statement in the code that requires a user input. For example, input(“enter variable x”). This will stop the execution of the program and allow you to see the progress until that command. The program remains paused until you press Enter.
This approach is not advisable for production applications as it may fail in case of bugs and errors in your program.
Python is a fun language to learn. It is easy, powerful and has tons of support on the internet. These characteristics make Python a very programmer friendly language, which even a beginner can learn quickly.
After reading this tutorial, you should now feel comfortable in writing simple Python scripts and running them. You know the different ways to run Python and the role of interpreter in executing your Python program.
This will allow you to improve your development process and test different strategies quickly.