1

I am trying to run ipython from the bash (version 4.4.19) command line.

As a Python developer, I have various installations of ipython at various versions in various virtualenvs' paths, and so it is important to know which one I am running. Hence the $PATH is always changed when I change virtualenv, and this would a typical value for PATH:

$ echo $PATH
/Users/jab/.virtualenvs/tools/bin:/Users/jab/bin:/Users/jab/src/git/hub/jab/bin:/usr/local/gnu:/bin:/usr/local/bin:/usr/bin

The important detail in that is that the first entry is "/Users/jab/.virtualenvs/tools/bin", and that the file /Users/jab/.virtualenvs/tools/bin/ipython does exist:

$ ls -l /Users/jab/.virtualenvs/tools/bin/ipython
-rwxr-xr-x 1 jab staff 252 May 11 15:18 /Users/jab/.virtualenvs/tools/bin/ipython

As expected, which says that that file will be run as the "$ ipython" command

$ which ipython
/Users/jab/.virtualenvs/tools/bin/ipython

$ $(which ipython) -c "import sys; print(sys.executable)"
/Users/jab/.virtualenvs/tools/bin/python

However, that is not actually the case, and /usr/local/bin/ipython is run instead

$ ipython -c "import sys; print(sys.executable)"
/usr/local/bin/python3

Can someone explain why bash is ignoring my $PATH and using the "wrong" executable? And what do I need to change (in my bashrc, or on my system (macOS 10.12.3)) so that executables are chosen by bash in the order determined by my $PATH.

Note: This is not a duplicate Bash is not finding a program even though it's on my path, because that asks how PATH works to find any program, whereas this question is anout why the wrong program is found.

  • The output of (which ipython) -c "import sys; print(sys.executable)" gives /Users/jab/.virtualenvs/tools/bin/python. what does ls -l /Users/jab/.virtualenvs/tools/bin/python return? – Kevin Kruse May 11 '18 at 16:22
  • FYI: the python (pip, ...) command runs fine: from the virtualenv. – jalanb May 11 '18 at 16:23
  • $ ls -l /Users/jab/.virtualenvs/tools/bin/python gives -rwxr-xr-x 1 jab staff 42720 Nov 9 2016 /Users/jab/.virtualenvs/tools/bin/python – jalanb May 11 '18 at 16:24
  • 1
    @jalanb Your command $ (which ipython) -c ... should actually be $ $(which ipython) -c ..., since you need $(...) for command substitution in order to use that. Maybe you should update your question to do that (or just use the full path /Users/jab/.virtualenvs/tools/bin/ipython directly, since there's no ambiguity there...) – filbranden May 11 '18 at 18:25
  • 1
    @FilipeBrandenburger Thanks, good catch (and bad paste!). Updated. – jalanb May 11 '18 at 18:50
6

It looks like at some previous time in your bash session, the "wrong" executable is called an then its pathname is remembered by Bash (that's normal, such feature prevents further PATH lookups for already known commands).

To fix this you should run

$ hash -d ipython

This clears the remembered location of ipython, so Bash needs to search PATH again to find the command (and that's when it finds the right executable).

Not the answer you're looking for? Browse other questions tagged or ask your own question.