• Get application security done the right way! Detect, Protect, Monitor, Accelerate, and more…
  • You should probably know how to run Python scripts if you are familiar with Python.

    Why should you read this article? And another chance that you don’t know how to run Python scripts as you are not familiar with them. It’s definitely for you. Is this only for you? No, both who are familiar and who are not with Python can get something new in this article. Without further ado let’s jump into the article.

    Before going into the execution part of the tutorial, we need to have installed Python on our systems.

    Open a text editor and create a Python script to use throughout this tutorial. You can use the following script of adding two numbers.

    a, b = list(map(int, input().split()))
    print(a + b)
    

    IDE

    You can run the Python scripts with IDE using a single click. Running Python scripts in IDE is a straightforward thing. You will find a button to run the Python script with the same name. Click it to run the Python script.

    Common Way

    The most common way to run the Python scripts is to use the command line or terminal. Let’s see the steps to run the Python scripts using the command line or terminal.

    • Open your command line or terminal.
    • Navigate to the directory where your Python script lies.
    • Run the script with the python3 script_name.py command (The keyword may change to python according to your configuration).
    • Done. You can see the output in the next line.

    You can run the Python scripts with the above steps irrespective of your operating system. The above steps work with all major operating systems.

    There are other ways to run the Python scripts in different operating systems. Let’s see them one by one.

    macOS / Ubuntu

    We can make use of the bash script to run the Python script in macOS/Ubuntu. Both these operating systems support bash scripts. Let’s see the steps to run Python scripts using a bash script.

    • Open any text editor.
    • Create a file to write a bash script with the extension sh and paste the following code.

    #!/usr/bin/env bash
    python3 /path/to_your/python/script.py

    • Update the path in the bash script.
    • Run the bash script with the command ./bash_script_name.sh

    You may also want to learn how to run bash scripts in Python.

    Ubuntu

    There is another cool way to run the Python scripts in Ubuntu.

    Let’s say you have a GUI application that’s written in Python. It’s difficult to run it by following the above methods every time. How can we run it in a single click? What about running it from the dashboard? Isn’t it cool?

    Yeah, we can create a desktop entry for the Python script.

    Let’s see the steps to run the Python script from the dashboard.

    • Open any text editor and create a new file.
    • Paste the following code into the file and save it with the extension desktop in the location /home/your_username/.local/share/application/.
    [Desktop Entry]
    Name=add.py
    Exec=gnome-terminal -- /home/your_username/path_to_entry_bash_file/bash_script.sh
    Type=Application
    Categories=GTK;GNOME;Utility;

    You can change the name from add to anything you like.

    • Create a bash script to run the Python script. It’s the entry point for the application. And paste the following code into it.
    #!/usr/bin/env bash 
    python3 /relative/path/to_script/from/home/script.py

    Give the path of your script relative from the home directory in the bash script. And update the bash script path in the above desktop entry.

    • Now, press the Windows key and search for the desktop entry with the name you have given. And run it.

    You will see the GUI application. But, if you run any script which doesn’t have any user inputs, then you will face the following problem.

    We can see that the terminal closes after running the Python script. We can’t see the output for even a sec. To avoid this problem, we need to keep open the terminal even after the execution of the script. Add a new line to the bash script as follows.

    #!/usr/bin/env bash 
    python3 /relative/path/to_script/from/home/script.py bash

    Now, run it again. And there is output in front of you.

    Windows

    There is another way to run the Python scripts in windows. We can use Run to execute the Python scripts. Let’s see the steps to run the Python scripts using Run.

    • Open the Run by pressing Windows Key + R.
    • Enter the py C:\path\to\script.py in the Run dialog and press enter.
    • The command line closes as soon as the execution complete. We can even see the output. How to solve this problem?

    We can use the batch script to keep open the command line even after the execution of the Python script. Todo so, create a file with an extension bat and paste the following code into it.

    @py.exe C:\path\to\script.py %*
    @pause

    Now, once again open the Run dialog with Windows Key + R shortcut and enter the path of your batch script. It will run the Python script that’s given in the batch script. You can see the output now.

    Conclusion

    Choose the most appropriate way to run your Python scripts. And enjoy writing the code in Python.

    Happy Scripting 🙂