Python File Execution

lesson tools active tools/python-file-execution.md View on GitHub

Python File Execution

Rule

Choose the correct Python execution method based on script type and context: uv scripts with shebang use direct execution, poetry projects use poetry run, standalone scripts use appropriate tooling.

Context

When running Python scripts from shell commands or automated workflows in any repository or project context.

Detection

Observable signals that you need proper execution method:

Pattern

Quick decision:

Script has shebang (#!/usr/bin/env python3) and +x → ./script.py
Poetry project → poetry run python script.py
uv project → uv run python script.py
Standalone → python3 script.py

For uv scripts with inline metadata:

# If script has: # /// script dependencies = [...] ///
./script.py  # Direct execution works

# Or explicitly
uv run script.py

For poetry projects:

poetry run python script.py
poetry run pytest

Setting executable permissions:

chmod +x script.py
./script.py

Outcome

Following this pattern leads to:

Related

Match Keywords

python script permission denied bash: script.py: Permission denied choose correct python execution method shebang python execution ModuleNotFoundError when dependencies should be available