How to Run a Python Script

In this tutorial, we will go through the steps on how you can run a Python script on your computer.

How to Run a Python Script

Executing a Python script is very easy within the terminal or IDE (Integrated Development Environment). You can identify a Python script by the .py file extension. This tutorial will touch on how to run the script within the terminal or in an IDE like Visual Studio Code (VSC).

For this tutorial to work correctly, you will need to install Python to your chosen operating system. We have tutorials on installing Python to Windows, macOS, and Linux. These are all straightforward and will not take long to complete.

The steps below will take you through all you need to know to run a Python script. If you encounter any issues, your system may not have Python configured correctly.

Run a Python Script in the Terminal

Running a Python script in the terminal is very straightforward but requires you have Python already installed. Below are a few short steps that you will need to complete to run a script successfully.

1. Load the terminal application on your chosen operating system. For example, on Windows, this is the command prompt. On macOS and Linux, it is known as terminal.

To open the command prompt on Windows, press the Windows Key + R, type in cmd, and press Enter.

Load the terminal application on your Mac. You can find it by going to launchpad and searching terminal. Click on Terminal.

Linux will vary depending on the operating system. For example, you can open the terminal on Ubuntu by pressing CTRL + ALT + T.

2. In the terminal, navigate to the directory where your Python script is located by using the cd command.

For example, the command below will change my directory to the dev directory.

3. Inside this folder, I have several Python scripts.

To run a Python script, I will need to write python3, followed by the script’s name. It is important to note that the python command may differ. For example, it might be just python or python3.1.

4. If you want to run a Python script without preceding the script with python, you will need to specify a shebang line pointing to the Python environment. The shebang line needs to be at the top of the file.

Below is an example of a shebang line pointing to the location of Python 3.

After adding the shebang line, if you try and run the script without the python command, the script should still run correctly. However, you may need to use chmod to make the file executable.

Run a Python Script in an IDE

Most modern integrated development environments (IDE) will have an inbuilt method of testing code, such as Python scripts. However, the way you run and test code will vary from IDE to IDE, so you may need to look up instructions on how you can do it.

In this example, we will use Visual Studio Code, an IDE that is available on almost every operating system, including Ubuntu.

There are two ways you can run a Python script within VS code. The first method involves using the built-in run and debug mode. The second method will use the built-in terminal.

1. Load VS code on your computer and open the Python file you wish to test.

In the sidebar, click on the icon that has the play icon and a bug. Next, click on “Run and Debug”.

VS Code will now open the inbuilt terminal and run the file, as seen in the image below.

Run a Python Script Visual Studio Code Debug Mode

2. To use the integrated terminal, right-click on your file in the explorer tab on the right-hand side. Then, click on the option “Open in Integrated Terminal“.

Visual Studio Code Open Integrated Terminal

This option will open a terminal at the bottom of the editor. VS code will set the directory to the location of your script. Here you can run your Python script from within the IDE.

For example, in the screenshot below, we write python dataypes.py to run our file.

Run a Python Script Visual Studio Code using the inbuilt terminal

Conclusion

I hope by now you have a good understanding of how you can run a Python script on your operating system of choice. Python is an excellent choice for running scripts that handle various tasks, as it is a versatile programming language.

If you are new to Python, we recommend checking out some tutorials to learn more about Python and its characteristics. Learning the Python syntax or the variables you can use is a great place to start.

Please let us know if you notice a mistake or an important topic is missing from this guide.

Print Friendly, PDF & Email
Spread the love

Server Status

Aradippou Chat 1 Chat 2 Larnaca Limassol Nicosia

Chat Links

Official Links.

1. Chat 1.
2. Chat 2.

Alternative Mirror Links.

1. KiwiIRC 1.
2. KiwiIRC 2.

Other Web Clients.

1. IrcCloud.

Recent Posts

Related Posts:

Archives

Super Club Radio

CyIRC

CyIRC Tweets

Scroll Up