Linux Commands

How to Run a Python Script From the Desktop Icon in Linux

“The flexibility and power of Python make it a popular programming language. It is an open-source programming language that plays an imperative role in web & software development, machine learning, and performing complex mathematics.

Python language can make most of our work easier. Python paces development and troubleshooting quickly. Python is a must-do thing for you if you want to get a grip on creating software and web applications.

In Linux, you only need to execute the Python command to run the Python process. However, some users always look for ways to run Python scripts from the desktop icon. So in this guide, we will explain how to run Python from a desktop in Linux.”

How to Run Python Script From the Desktop Icon in Linux?

As Ubuntu users, we know that the system automatically opens the Text Editor when we try to open the python file. Hence, we need to configure the file before making it executable from its icon:

First, you will need an executable python script, so make an executable python program.

Once you create a python file, then save it and download an icon image you want for this file.

Now open the Sublime Text Editor and enter the following information into it:

[Desktop Entry]
Version= 1.0
Icon= /home///
Name=
Exec=/home/// .py
Terminal=true
Type=Application

Let’s breakdown the above lines one by one:

  • Name= Name of the icon
  • Encoding = Indicates the type of encoding used on this desktop file
  • Icon = Path of the icon
  • Exec = Path of the Python file. You can also use the following line that includes Python location:

Exec= /usr/bin/env python3 /home/<user>/<directory>/ <python_file>.py

  • Type= Indicates that the file is an application.
  • Terminal= Type true if you want to use the file in the terminal or type true to turn off this option.

After typing the above information, save it to Desktop with the suffix “.desktop.”

Now you have to give all the required access to the python file, so run the following command in the terminal.

chmod u+x /home/<user>/<directory>/<python>.py

Moreover, you also need to give the required permissions to the .desktop file as well. Therefore run the below command for it:

gio set /home/<user>/<directory>/<.desktop file> metadata:: trusted true

The above command will turn the desktop file into a trusted icon. Finally, click on the .desktop file to execute it like other applications.

If you get the following error that shows Invalid Permission on the Desktop File, please turn on the allow launching option by right-clicking on the file.

Summing Up

Python can only be run on Linux by executing the Python command. That’s why we have a guide so that you can use our approach to run the Python script from the desktop icon in Linux. This guide has been made after thorough research and categorized into multiple steps as it will be easier for you to understand. Following the above-mentioned basic steps, you can quickly run your python script from anywhere on your desktop in Linux. For more information related to Linux, you can also visit our official website.

About the author

Prateek Jangid

A passionate Linux user for personal and professional reasons, always exploring what is new in the world of Linux and sharing with my readers.