Erick Castillo

Full Stack Developer

Erick Castillo

Full Stack Developer

Run Python Scripts Instantly on Ubuntu with a Right-Click Nautilus Shortcut

by | Oct 15, 2025 | Linux Tutorials

Right-Click to Run Python on Ubuntu (Nautilus Script Guide)

Want a faster workflow? This guide shows how to create a Nautilus right-click action that activates your virtual environment and runs any selected Python file—no extra typing. Learn how to build a simple nautilus python script and save time on every project.

Ubuntu Nautilus right-click menu highlighting ActivateVenv_Run script

Why nautilus python script Matters

Ubuntu’s Files app (Nautilus) supports custom scripts that appear in the right-click menu. By adding one that finds your nearest virtual environment and launches the selected .py file, you eliminate repetitive commands and misfires. For background, see Python’s venv docs and the GNOME Files (Nautilus) help. For Linux setup tips, browse more on Linux Tutorials.

What You’ll Build

A Nautilus script named ActivateVenv_Run that:

  • Detects the file you right-click (e.g., app_gui.py).
  • Walks up parent folders to find a venv named venv, .venv, or env.
  • Activates that venv and runs the file in a new terminal window.

Step-by-Step Instructions

1) Create the Nautilus scripts folder

mkdir -p ~/.local/share/nautilus/scripts

2) Create the script file

nano ~/.local/share/nautilus/scripts/ActivateVenv_Run

3) Paste this Bash script

#!/bin/bash
# Nautilus script: Run selected Python file inside nearest venv (.venv/venv/env)

# Get first selected path from Nautilus (newline-separated list)
SEL="$(printf '%s' "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" | head -n1)"

# If nothing selected, show message and exit
if [ -z "$SEL" ]; then
  gnome-terminal -- bash -lc 'echo "No file selected."; exec bash'
  exit 1
fi

# Resolve directory and filename
if [ -d "$SEL" ]; then
  DIR="$SEL"
  FILE=""
else
  DIR="$(dirname "$SEL")"
  FILE="$(basename "$SEL")"
fi

# Only run if a .py file was selected (loosen if you want)
if [ -n "$FILE" ] && [[ "$FILE" != *.py ]]; then
  gnome-terminal -- bash -lc "echo 'Selected file is not a .py file: $FILE'; exec bash"
  exit 1
fi

# Open a terminal, locate a nearby venv, activate, and run the file
gnome-terminal -- bash -lc "
set -e

DIR=\"$DIR\"
FILE=\"$FILE\"

# Find nearest venv by walking up to /
VENV=\"\"
SEARCH_DIR=\"\$DIR\"
while [ \"\$SEARCH_DIR\" != \"/\" ]; do
  for V in venv .venv env; do
    if [ -x \"\$SEARCH_DIR/\$V/bin/python\" ] && [ -f \"\$SEARCH_DIR/\$V/bin/activate\" ]; then
      VENV=\"\$SEARCH_DIR/\$V\"
      break 2
    fi
  done
  SEARCH_DIR=\"\$(dirname \"\$SEARCH_DIR\")\"
done

if [ -n \"\$VENV\" ]; then
  echo \"Using venv: \$VENV\"
  source \"\$VENV/bin/activate\"
  PY=\"\$VENV/bin/python\"
else
  echo \"No venv found; falling back to system python\"
  PY=\"python3\"
fi

cd \"\$DIR\"

if [ -n \"\$FILE\" ]; then
  echo \"Running: \$PY \$FILE\"
  \"\$PY\" \"\$FILE\"
else
  echo \"No file selected; opening shell in \$DIR\"
fi

echo
echo \"--- Finished (interpreter: \$PY) ---\"
exec bash
"

4) Make it executable

chmod +x ~/.local/share/nautilus/scripts/ActivateVenv_Run

5) Restart Nautilus

nautilus -q

Close and reopen the Files app. (If needed, log out/in.) See Ubuntu’s guidance on restarting processes: help.ubuntu.com.

6) Use it

  • Right-click any .py file (e.g., app_gui.py).
  • Go to ScriptsActivateVenv_Run.
  • A terminal opens, your venv activates, and the script runs.

Troubleshooting

  • “No venv found”: Create one with python3 -m venv .venv then source .venv/bin/activate.
  • Different terminal app: Replace gnome-terminal -- bash -lc with your favorite (e.g., Kitty or Tilix).
  • Run non-.py files: Remove the file extension check in the script.

With this setup, running code is one right-click away. If you found this helpful, explore more automation guides in Linux Tutorials and related topics like Bash Script and Virtualenv. This guide is published by Erick Castillo to streamline day-to-day dev workflows.