Skip to content

Instantly share code, notes, and snippets.

@nvie
Created February 13, 2013 09:43
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nvie/4943385 to your computer and use it in GitHub Desktop.
Save nvie/4943385 to your computer and use it in GitHub Desktop.
This is my favorite way of automatically invoking the best suitable Python REPL. It is smart about the environment that it is invoked in (e.g. will respect your current virtual env) and is smart about which interpreter fits best (e.g. using `ipython` if available, or using `python manage.py shell` in case of Django, or `python manage.py shell_pl…
function p --description 'Start the best Python shell that is available'
set -l cmd
if test -f manage.py
if pip freeze ^/dev/null | grep -q 'django-extensions'
set cmd (which python) manage.py shell_plus
else
set cmd (which python) manage.py shell
end
else
set -l interpreters (which ipython ^/dev/null; which python ^/dev/null)
if test -z "$interpreters"
set_color red
echo "No python interpreters found on the PATH."
set_color normal
return 127
end
# Try to find the first interpreter within the current virtualenv
# Rationale: it's more important to start a Python interpreter in the
# current virtualenv than it is to start an _IPython_ interpreter (for
# example, when the current virtualenv has no ipython installed, but such
# would be installed system-wide).
for interp in $interpreters
#echo '-' $interp
#echo '-' (dirname (dirname $interp))
if test (dirname (dirname $interp)) = "$VIRTUAL_ENV"
set cmd $interp
break
end
end
# If they all fall outside the virtualenv, pick the first match
# (preferring ipython over python)
if test -z "$cmd"
set cmd $interpreters[1]
end
end
# Run the command
printf "Using "; set_color green; echo $cmd; set_color normal
eval $cmd
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment