Skip to content

Instantly share code, notes, and snippets.

@ichernev
Created March 17, 2012 11:36
Show Gist options
  • Select an option

  • Save ichernev/2057859 to your computer and use it in GitHub Desktop.

Select an option

Save ichernev/2057859 to your computer and use it in GitHub Desktop.
Switch python versions (2 <-> 3)
#!/bin/bash
# source this file in your ~/.bashrc or ~/.zshrc and then use pyswitch function
_add_shortcut() {
mkdir -p /tmp/bin
cd /tmp/bin
rm -f python
ln -s /usr/bin/python2 python
export PATH="/tmp/bin:$PATH"
echo "using version $(python --version 2>&1 | cut -c8-)"
}
_remove_shortcut() {
export PATH=$(echo "${PATH}:" | sed -e 's_/tmp/bin:__' -e 's_:$__')
rm -rf /tmp/bin
echo "using version $(python --version 2>&1 | cut -c8-)"
}
pyswitch() {
current=$(python --version 2>&1 | cut -c8)
if [ "$current" -eq 2 ]; then
_remove_shortcut
else
_add_shortcut
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment