4) Set Up Your Machine Lesson

How to Run Python Scripts

8 min to complete · By Martin Breuss

Another way you can run Python code is by executing a script. Python scripts start execution at the top of the file and proceed line by line until the end of the file is reached, or the program is terminated in another way.

This way of programming is called procedural programming and it's what you'll do throughout the first module of this Python course. Python is a very versatile language, and you'll later learn different ways of programming while sticking with the same language.

Create a Python Script

In order to run a Python script, you need to save your code with a .py file extension. This tells your computer that the instructions in that file should be executed with Python. You already saved a file containing Python code with that file extension when you built the guess-the-number game. It's time to give this process another go, this time using VS Code.

Start by creating a new file by going to File / New File or using your operating system's keyboard shortcut. Save this new blank file as first_script.py. You can save the file on your Desktop or in any other location. Just make sure you know where it is and how to find it.

Inside this .py file, write some Python code. Keep it short and concise by printing out some text to the console:

print("hello world")

After you type this code into your empty file, make sure to save it!

Colorful illustration of a light bulb

Info: To make your life a little easier, turn on VS Code's auto-save option by checking File / Auto Save in the VS Code context menu.

Run Scripts with the Terminal

Now switch to the terminal part of your VS Code window. You can run your new Python script using the python3 command:

python3 first_script.py

The above command will only work if your terminal points to the folder where you saved the first_script.py file. If you saved it on your Desktop, you can make sure that your terminal points to your Desktop by typing:

cd ~/Desktop

This command will change the directory to the Desktop of your current user.

Colorful illustration of a light bulb

Info: The cd command on UNIX systems stands for change directory and allows you to switch to different locations with your terminal. The tilde symbol (~) is a shortcut reference for your user's home directory. Therefore, this command means change directory to the Desktop of my current user. Check out the Bash Basics course if you want to learn more terminal commands.

The tilde shortcut should also work on Windows PowerShell. Alternatively, you can navigate to your home directory by using the $HOME variable.

After you make sure that you are in the directory where you saved the Python script, you can execute the file with the python3 first_script.py command mentioned further up.

However, sometimes you won't exactly know where you just saved a file, or it might get too tiring to type out long paths in your terminal just to run your script. VS Code has a solution to this!

Run Scripts with a Button

VS Code makes running your script even more user-friendly. You won't have to remember any paths or type commands into your terminal. All you need to do is press Run:

VS Code text window with Python code and the Play button to execute the script highlighted

The green triangle button in the top right of your VS Code window runs the current file and shows you the output in your terminal. It's really just a shortcut to doing what you did manually before. When you hover over the button, you'll see that it's called Run Python File in Terminal, which is exactly what it does.

It's an extremely common action to run your script while you're in the process of developing it. You want to know what its current outputs are, whether it works as expected, and if it doesn't, what might be going on.

Therefore, it's very helpful to be able to run your script quickly with just the click of a button.

Summary: How to Run Python Scripts

  • A Python script is Python code saved to a file that has the file extension .py. Scripts are useful when writing more complex programming logic and when you also want to keep your code for later use.

  • Saving your code in a script allows you to run the code multiple times, make updates, and continue this iteration until you have a functional program. Most of the code you will write throughout this course will be saved as a Python script.

  • As you have seen, there are three steps to running your Python code from a script:

    1. Write your Python code
    2. Save it in a file with the .py extension
    3. Execute the script using CLI commands on your terminal or the Run button in VS Code