Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to run Python script in terminal

Getting Started

Before we dive into the deep end of the pool, it's important to warm up a little. If you're here, it means you've been introduced to Python, the high-level, interpreted programming language. Now, you want to learn how to run your Python scripts from the terminal. That's great! Let's get started.

What is a Terminal?

A terminal, sometimes referred to as the command line or console, is a tool that allows you to control your computer using text commands, instead of graphical interfaces. Think of it as a text-based remote control for your computer.

Creating a Python Script

To run a Python script in a terminal, we first need a Python script. Let's create a simple script that prints "Hello, World!" to the console. Open your text editor, type the following code and save it as hello_world.py.

print("Hello, World!")

Running the Python Script in Terminal

Now that we have our Python script, let's run it from the terminal. Open your terminal and navigate to the directory where you saved your hello_world.py script. To run the script, type python hello_world.py and press Enter. You should see Hello, World! printed in the terminal. Congrats, you've just run your first Python script from the terminal!

Understanding the Command

In the command python hello_world.py, python is the command to run the Python interpreter. The Python interpreter reads and executes Python code. The hello_world.py is the Python script we want the interpreter to execute.

Imagine you're a chef and the Python interpreter is your assistant. The script is like a recipe. You (the chef) give the recipe (the script) to your assistant (the Python interpreter) to prepare (execute).

The Magic of Shebang

There is a trick in Python that allows you to run the script just like any other command in the terminal, without having to type python before it. This trick is called a shebang.

A shebang is a line at the top of the script that tells the system what interpreter to use to execute the script. For Python scripts, the shebang line is #!/usr/bin/env python3.

Let's add the shebang line to our hello_world.py script.

#!/usr/bin/env python3
print("Hello, World!")

Now, before we can run our script like a command, we need to make it executable. In the terminal, type chmod +x hello_world.py and press Enter. This will give the script permission to be executed.

Now, you can run your script by typing ./hello_world.py in the terminal. You'll see that Hello, World! is printed, just like before!

Handling Errors

Sometimes, things don't go as planned and you get errors when running your script. Don't panic. The error message is there to help you fix the problem.

For example, if you type python helloworld.py instead of python hello_world.py, you'll get an error like this:

python: can't open file 'helloworld.py': [Errno 2] No such file or directory

This error message tells you that Python can't find a file named helloworld.py. Check the name of your script and the command you typed. They should match. In this case, the correct command is python hello_world.py.

Conclusion

Running Python scripts from the terminal is an essential skill for every Python programmer. It might seem intimidating at first, especially if you're new to programming, but don't worry. With practice, you'll become comfortable with it in no time.

Remember, the terminal is just a text-based remote control for your computer. The Python interpreter is your assistant, and the script is the recipe you give to your assistant to execute. Errors are your friends, they help you find and fix problems in your scripts.

Keep exploring, keep learning, and keep coding. You're doing great!