How to Run a Python Script? A Complete Step by Step Guide

Posted in

How to Run a Python Script? A Complete Step by Step Guide
vinaykhatri

Vinay Khatri
Last updated on March 28, 2024

    After writing a set of code or a script, we need to execute it so the system can perform all the instructions that are in the script. If we want to define a high-level program or a script, we can say that it is a set of instructions. Whether you are a beginner or advanced python developer , you should know how to execute and run Python scripts or code. By executing your Python script or code, you get to know if it works as intended or not. Well, this article answers how to run a Python script/program using different methods. As a Python developer, it becomes essential for you to be aware of the different ways with which you can execute a Python script or code. So, let's not waste any time and get started!

    How to Run a Python Script or Program?

    There are various methods to run a Python script. Here is the list of the most common ways to run a Python script:

    • Command-Line or Terminal
    • Python Interactive Console or Shell
    • IDE
    • Text Editors (Specific)

    We will discuss each method in detail, but before that let us get a brief overview of a script, Python interpreter, and the requirements for running Python scripts.

    What is a Script?

    In programming, we use the term script for a file that contains executable code. In simple terms, a script is a text file that includes a specific program. To execute or run a script, we generally require a translator, and it could either be a compiler or interpreter which executes each command of the script sequentially. If we talk about Python script or Python scripting, we can say that a Python script is a text file containing the Python code, and the extension of that file should be .py . To run a Python script, we require a Python interpreter which can be downloaded from the official website of Python. Check out how to download Python.

    What is a Python Interpreter?

    A Python interpreter is a translator software that allows us to execute Python code. When we download Python from its official site, we download the Python interpreter and some of its tools so that we can run Python scripts in our system. It is essential to install Python in your system before you try to run Python scripts because the system does not understand the command and instructions of high-level programming languages by default. To run or execute a high-level programming code we require translators such as compiler and interpreter , Python uses an interpreter as a translator for the execution of its code.

    Python in Terminal and Requirement of Python Script

    Once you download Python in your system, you get the Python executable shell command on your command-line interface or terminal. On the Python shell, you can write and execute the Python code at the same time. To use the Python standard console or Python shell, you need to open your command-line interface or terminal, which is Command Prompt on Windows and Terminal on Linux and macOS. Type Python or Python3 on your command line or terminal and hit the Enter button. You will see an interactive mode represented by the >>> symbol, and now you can write and execute your Python code here. Here is an example of how to use the Python Terminal Shell. By executing the python command on your command-line interface or terminal, you can access the Python executable shell.

    Output

    This Python tool comes in handy when we want to run a specific set of code to check its output. By using the Python executable shell, we can write a thousand lines of code and execute them simultaneously. But there is a problem with writing code in the Python terminal shell; once we exit the console, all the code we have written get vanished and that's why we require Python scripts. We create Python scripts so that we can save the Python code in a file with the .py extension, and we can execute it on any platform and at any time.

    Note: To exit from the Python Standard Interactive Console, press CTRL+Z and press enter. If you are using Unix Operating System, press CTRL+D.

    Various Methods to Execute Python Script

    To execute a Python script first, you need to create a Python file with extension .py, and save it on your local system. To create a Python script, you can use a text editor or IDE, such as Sublime, Notepad ++, PyCharm, etc.

    1. Run a Python Script in Terminal/Command-Line

    To run a Python script in the terminal or command-line interface, your system must have Python installed in it. To execute the Python script in the command line, you first need to navigate to the directory where the file is stored. After that, you need to write Python or Python3 command followed by the full name of your Python script, including extension and hit Enter.

    C:\Users\Public\script.py
    print("Hello world It's Tech Geek Buzz")

    Save or Redirect the Output

    Save or Redirect the Output:

    Python allows us to save the result of the script in a text file. For instance, in the above example, we just displayed the output of the script.py file on the command line, but we can save that output on a text file by using the python filename > textfile name command.

    Save or Redirect the Output - 1

    This command will create a result.txt file on the same directory where the script file exists and store the output returned by the script execution. However, every time you run this shell command, the new output will overwrite the old output. If you want to add the new output at the end of the previous output, use this command: < strong>Python script name >> textfile name .

    Save or Redirect the Output - 2

    This command would create a new result.txt file if the file did not exist, else it will write the output at the end of the old output.

    2. Run a Python Script on the Python Interactive Console or Shell

    In Python, we have a keyword import, which allows us to import a module or other Python files in the current file so that we can use the pre-written code of those files. Also, the import keyword lets us execute a Python script in the interactive Python console. To execute the Python script on the interactive Python console, you need to open your command line or terminal, navigate to the directory where the script is stored, open the interactive console, and use the import script_name to execute the file.

    Example:

    #C:\Users\Public\script.py
    print("Hello world It's Tech Geek Buzz")

    python script on the Python Interactive console

    Note: This method will work only when you are in the same directory where the script is stored.

    3. Run a Python Script on an IDE

    If you are using a Python IDE, you will get a run option that allows you to directly execute your Python code. This is one of the significant advantages of using any IDE. An IDE provides us with an interactive text editor where we can write our Python code. PyCharm is a very popular Python IDE , and it comes with an inbuilt python interpreter, code editor, and debugger which can be used to write and execute any Python script. To execute the Python script on any IDE, you first need to open the Python script on the IDE. After that, you need to right Click on the text editor and click on the run option.

    Execute the Python script on an IDE

    Output

    Execute the Python script on an IDE - Example

    4. Run a Python Code on a Text Editor

    Text editors are not supposed to execute the code. They only provide basic features such as code editing and debugging. For the execution process, we have to depend on the Command Line or Terminal. However, text editors, such as Visual Studio Code, provide plugins to run the code on the code editor itself.

    Conclusion

    For any Python developer, running Python scripts or code is an everyday task. Knowing how to run a Python script is important because it helps to know about the output of a Python code or program. In this article, we explained the various methods to run a Python script. It does not matter which method you choose as the output of your Python script would remain the same. The command-line and terminal approaches come very useful when you are using a Text editor; otherwise, you can simply execute your Python code on an IDE. In the development phase, we often use the command line and IDE approach, but we do not use the Python shell terminal and import method, because import is only used when we want to import any module in our program.

    People are also reading:

    Leave a Comment on this Post

    0 Comments