How to Run a Python Script: A Practical Guide for Beginners

In this comprehensive guide, we will show you different methods to run a Python script and address common errors encountered by beginners.
by Josephine Loo · February 2024

Contents

    Python is a versatile and beginner-friendly programming language that is widely used for various purposes such as web development, data analysis, artificial intelligence, and more. With its simplicity and readability, Python has become an excellent choice for beginners. However, many Python beginners often face a common problem—how to actually run a Python script after learning how to code in the Python language.

    In this article, we’ll learn different methods for running a Python script, and walk you through some common errors that a beginner may encounter, with solutions to overcome them.

    Prerequisites

    Before we learn how to run a Python script, let's make sure you have everything set up correctly on your computer. Here's what you will need:

    • Python installed on your system
    • A code editor or IDE of your choice
      (eg. PyCharm, Spyder, Visual Studio Code)

    If you haven't installed Python yet, head over to the Python website and download the latest version suitable for your operating system.

    🐻 Bear Tips: You can verify the installation by opening a command prompt or terminal and typing python --version or python3 --version.

    Writing a Python Script

    To get started, let's create a simple script that prints “Hello World”. Open your code editor or IDE and create a new file with a .py extension (e.g. script.py). Then, add the code below to the file:

    print("Hello World")
    

    Save the file and you are ready to run your Python script!

    How to Run a Python Script

    Method 1. Run Python Command from the Command Line/Terminal

    The simplest way to run a Python script is by using the python command in the command line/terminal. First, open up your command line/terminal and navigate to the directory where your Python script is saved. Then, enter the python command followed by the script's name in the command line/terminal:

    python script.py
    

    Note: If you’re using Python 3.x, replace python with python3.

    This will invoke the Python interpreter and run the script, printing “Hello World” to the console:

    how to run a Python script from the command line:terminal - result.png

    🐻 Bear Tips: To print the output to a text file, add > “file_name.txt” following the command, eg. python script.py > ouptut.txt.

    Method 2. Make the Python Script Executable

    Another method to run a Python script is by making it executable and executing it from the command line or terminal without using the python command.

    At the beginning of your script, add a shebang line #!/usr/bin/env python to specify Python as the interpreter:

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

    Then, run the command below in the command line/terminal to change your script’s permission to executable:

    chmod +x script.py
    

    After completing the steps above, you can now execute your Python script without using the python command:

    ./script.py
    

    how to run a Python script from the command line:terminal - result using shebang.png

    🐻 Bear Tips: Make sure that Python is installed and set up in your system's PATH for this method to work correctly.

    Running a Python Script with Command-Line Arguments

    Python scripts can accept command-line arguments, which allows you to provide input to your script without modifying the code. In the previous script, Hello World will always be printed to the console when the script is executed. Let's modify it to accept a name as a command-line argument and print a personalized greeting.

    Modify your script with the code below:

    import sys
    
    name = sys.argv[1]
    print("Hello, " + name + "!")
    

    Save the script and run it with the following command, providing a name (eg. Bear) as an argument:

    python3 script.py Bear
    

    The name will be added to the greeting and printed to the console:

    how to run a Python script from the command line:terminal - command-line argument.png

    You can also add more than one argument:

    how to run a Python script from the command line:terminal - command-line argument extra.png

    🐻 Bear Tips: Make sure to enclose your argument that contains spaces with double quotes ("")!

    Common Errors and Solutions

    When getting started with Python, it is common to encounter errors, which can be frustrating. To help you avoid frustration in your learning journey, here are some common errors and their solutions:

    Running Python Commands Within the Python Interpreter

    Make sure not to run your Python command within the Python interpreter by ensuring that the command line/terminal does not start with >>>. Otherwise, type exit() and press enter in your command line/terminal to exit the Python interpreter.

    running python commnad within the Pyhon interpreter.png

    Shebang Line Issues

    If you’re using a shebang line at the top of your script to specify the interpreter and execute the script directly from the command line/terminal, make sure that the interpreter’s name and its path are correct. Otherwise, you’ll get an error.

    Here’s what happens when python3 is installed but the interpreter specified is python:

    no such file or directory screenshot.png

    🐻 Bear Tips: Also, make sure that the shebang line is at the TOP of your script (line 1).

    Indentation Error

    Python relies on correct indentation to define blocks of code. Check that your indentation is consistent and avoid mixing spaces and tabs in your code. To make it easier, use text editors or IDEs that have features or extensions that can help you manage indentation and highlight any error:

    Python indentatino error.png

    Here are some recommended IDEs for Python:

    Python Command Not Found

    If you get the error python: command not found, it means your system doesn’t know where to find the Python interpreter. This usually means Python isn’t installed, or it’s not in your system’s PATH. First, verify that Python is installed by typing python --version or python3 --version in your command line. If the version is shown, it is installed on your machine.

    If Python is installed but not in your PATH, you’ll need to add it:

    • On Windows: Search for “Environmental Variables” in the system settings. In the “User” section, click on the “New” button and add a new variable with the location path of the Python executable as the value.
    • On Linux and macOS: Edit your shell’s profile script (.bashrc, .bash_profile, or .zshrc) using Nano (eg. nano ~/.bashrc). When the script is opened in the nano editor, scroll to the end of the file and add export PATH=$PATH:/path/to/directory (replace /path/to/directory with the path to Python). Press Ctrl+x to save and exit. Lastly, type source ~/.bashrc in the terminal to apply the changes.

    What’s Next

    Python’s versatility and simplicity make it an excellent choice for not only beginners but also for those looking to automate tasks and streamline their workflows. If you’re looking to automate tasks on the web browser, here are some articles that might help you:

    About the authorJosephine Loo
    Josephine is an automation enthusiast. She loves automating stuff and helping people to increase productivity with automation.

    Automate & Scale
    Your Web Scraping

    Browserbear helps you get the data you need to run your business, with our nocode task builder and integrations

    How to Run a Python Script: A Practical Guide for Beginners
    How to Run a Python Script: A Practical Guide for Beginners