Python Invocation

lesson tools active tools/python-invocation.md View on GitHub

Python Invocation

Rule

Always use python3 explicitly instead of python in shell commands and scripts.

Context

When executing Python code from shell commands, scripts, or automated workflows on systems that may not have a python symlink.

Detection

Observable signals that you need python3:

Pattern

# Wrong: assumes python exists
python script.py  # Error: command not found

# Correct: explicit python3
python3 script.py

# Wrong: shebang assuming python exists
#!/usr/bin/env python

# Correct: explicit python3 shebang
#!/usr/bin/env python3

# Wrong: subprocess with python
import subprocess
subprocess.run(['python', 'script.py'])

# Correct: subprocess with python3
subprocess.run(['python3', 'script.py'])

Outcome

Following this pattern results in:

Benefits:

Related

Match Keywords

bash: python: command not found python command not found use python3 instead of python python vs python3 which python to use