0

I would like to alias python with ipython only when no arguments are follows so that I can use ipython's shell and its autocompletion feature. For example:

#This should start the ipython shell
python 

#These should run python as usual
python -c "print(100)" #run print in python
python --version #run python --version 

I am trying with something like

python() {
if [ "$#" -eq 0 ]; 
then
    ipython
else
    eval $(printf "%q " "python $@")     #python --help: command not found
    #eval $(printf 'python %s\n' "$@")   #infinite loop calling python function
fi
}

1 Answer 1

1

No need for the eval. This function uses env to run the "default" python command

function python() {
    if [ "$#" -eq 0 ]; 
    then
        ipython
    else
        env python "$@" # will run python wherever it's installed
    fi
}
Sign up to request clarification or add additional context in comments.

3 Comments

but then I will get in trouble when changing the default location for python, or when using a virtual environment (or at least I think)
Edited answer to fix that problem.
env python doesn't do anything that python alone doesn't already do. env performs path lookup the same way the shell does.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.