Fix Python Not Found Errors Easily | Generated by AI
Yes — the "python not found"
error happens because your login shell might not have the same PATH
environment as the interactive shell where you normally type python
. When you call subprocess.run(..., shell=True)
, the subshell might not see python
if it’s not on $PATH
or if your system only has python3
.
Here are reliable ways to fix it:
1. Use sys.executable
Instead of relying on python
being found, always call the currently running Python interpreter:
import os
import subprocess
import platform
import sys
def gpa():
python_exec = sys.executable # full path to current Python interpreter
system = platform.system()
if system == "Linux":
shell_command = f"bash -l -c '{python_exec} ~/bin/gitmessageai.py --api deepseek --allow-pull-push'"
elif system == "Darwin":
shell_command = f"zsh -l -c '{python_exec} ~/bin/gitmessageai.py --api deepseek --allow-pull-push'"
else: # Windows
shell_command = f'cmd.exe /c "{python_exec} %USERPROFILE%\\bin\\gitmessageai.py --api deepseek --allow-pull-push"'
subprocess.run(shell_command, shell=True)
This guarantees that the same Python you used to run your script will also run gitmessageai.py
.
2. Try both python
and python3
Some systems only have python3
. You can detect availability:
import shutil
python_exec = shutil.which("python") or shutil.which("python3")
Then substitute that into your command string.
3. Hardcode a known path
If you know the absolute path (e.g. /usr/bin/python3
or /opt/homebrew/bin/python3
), you can directly use it in your command.
👉 The most robust solution is Option 1 (sys.executable
), since it uses the interpreter already running your script. That way you never depend on $PATH
.
Do you want me to refactor your function with sys.executable
fully integrated so you don’t ever see the "python not found"
error again?