# Add the following code your your .aliases, .zshrc, .bashrc or .bash_profile
# Then restart your shell
# Now you can use `python` or `py` to run your python files
# Python
alias python=python3
alias python2='/usr/bin/python'
alias py2='/usr/bin/python'
alias python=PATH=/usr/local/bin:/usr/local/sbin:~/bin:$PATH
check_venv_dirs() {
local venv_dirs=(".env" "env" ".venv" "venv")
for dir in "${venv_dirs[@]}"; do
if [ -d "$PWD/$dir" ]; then
return 0 # At least one directory exists
fi
done
return 1 # None of the directories exist
}
# Conditional alias for 'py'
if check_venv_dirs; then
# If any of the directories exist, set the 'py' alias to the Python interpreter in the current working directory's virtual environment
for dir in ".env" "env" ".venv" "venv"; do
if [ -d "$PWD/$dir" ]; then
alias py="$PWD/$dir/bin/python"
# echo "Alias 'py' set to Python interpreter in the current directory's virtual environment: $dir"
break
fi
done
else
# If none of the directories exist, use the system Python interpreter or set a different alias
alias py="/opt/homebrew/bin/python3"
alias py='$HOME/.pyenv/shims/python'
alias py="alias py="python3"
# echo "Alias 'py' set to the system Python interpreter"
fi