Run Python Files from the Right-Click Menu in GNOME Files

Introduction

If you’re a developer, you probably write and run Python scripts frequently. One of the most common ways to execute a Python script is through the terminal. However, every time you need to execute a Python script, you might find yourself navigating to the terminal, typing: python3 script_name.py and executing the command.

While this works fine, there’s a more efficient and user-friendly way to run Python scripts directly from your file manager—GNOME Files—with a simple right-click.

In this post, I’ll walk you through how to set up a script that adds a “Run Python Script” option to the right-click menu in GNOME Files. This way, you can easily run your Python files with just a click of a button.

Why This Script is Useful

This script is incredibly useful because it allows you to:

  • Make Python files executable with a single click.
  • Automatically run Python scripts in a terminal, keeping it open for you to inspect the output.
  • Avoid navigating to the terminal every time you want to execute a script.

Additionally, this script makes it much easier to run Python files without having to worry about manually opening a terminal and typing out the command. It works even with other IDEs like Geany, solving common issues with file executions.

The Script

Below is the script I use to run Python files directly from the GNOME Files right-click menu:

Explanation

  • $file="$1": This grabs the file you right-clicked on and passes it as an argument to the script.
  • chmod +x "$file": This makes sure that the Python file is executable before running it. Although Python scripts don’t strictly need to be executable, it’s a good practice to set this permission.
  • gnome-terminal -- zsh -c "python3 '$file'; exec zsh": This runs the Python script in gnome-terminal with python3. After the script finishes executing, it keeps the terminal open with exec zsh, allowing you to view any output or errors.

Features of the Script

  • Automatic Python Script Execution: Runs the Python file with a single click.
  • Terminal Stay-Open: Keeps the terminal open after running the script for easy debugging.
  • Error Handling: The script checks that the file is a Python file before attempting to run it.

Installing the Script

To use this script, follow these steps:

1. Create the Script

First, create the script file. You can do this by opening your terminal and using a text editor like nano:

Paste the script shown above into the file and save it (Ctrl + O, then Enter). Exit the nano editor with (Ctrl + X).

2. Make the Script Executable

After saving the script, you’ll need to make it executable:

3. Move the Script to the Appropriate Directory

Next, move the script to the directory where Nautilus can access it, which is typically ~/.local/share/nautilus/scripts/:

4. Enable the Script in Nautilus

Once the script is placed in the correct directory, you can right-click any Python file in GNOME Files. Under the Scripts section in the right-click menu, you should now see the run_python.sh script.

5. Running the Script

Screenshot of the GNOME right-click menu showing the 'Scripts' option with 'run_python.sh' for executing Python files.

To use the script, simply right-click on any .py file in GNOME Files and select Scripts → run_python.sh. The script will make the file executable (if it’s not already) and open gnome-terminal to run the Python file. The terminal will stay open afterward, allowing you to inspect the output.

Conclusion

This simple script makes executing my Python scripts easier than ever. With a simple right-click, I can run Python files and ensure that your terminal stays open for further inspection.

Feel free to modify the script if needed—whether you want to use a different terminal or make additional customizations. This script can be a great addition to your toolbox for improving productivity!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.