8

What's happening, here?!

$ /usr/bin/env which python /home/dbanas/.local/bin/python $ /home/dbanas/.local/bin/python -V Python 2.7.3 -- EPD_free 7.3-2 (64-bit) $ /usr/bin/env python -V Python 2.4.3 

I stumbled upon this, trying to debug one of my Python scripts, which uses a

#! /usr/bin/env python

first line. And I don't understand how it's possible.

Thanks! -db

I did just notice that '~/.local/bin/python' is a link, not an executable. Would that break the '/usr/bin/env ...' flow somehow?

Perhaps, this is a more succinct way to express the fundamental puzzle?:

$ env python -V Python 2.4.3 $ python -V Python 2.7.3 -- EPD_free 7.3-2 (64-bit) 

It just keeps getting curioser and curioser:

$ which python /home/dbanas/.local/bin/python $ python -c 'import sys; print sys.executable' /usr/bin/python 
20
  • Do you have an extra space in the shebang ? Commented Mar 26, 2014 at 20:04
  • 1
    the problem doesn't really have to do with shebang but with env running a different version than what which says it should. You might want to clarify the quesiton. What shell are you using? what does env (alone) report for your path? Commented Mar 26, 2014 at 20:20
  • @klashxx, I have a space in between '#!' and '/usr/bin/env'. Is that what you meant? Removing that space doesn't seem to change the behavior. Commented Mar 26, 2014 at 20:50
  • @george, Yes, I agree, the puzzler for me is: "How can '/usr/bin/env which python' yield a different python than the one that '/usr/bin/env python' invokes?" I'm using bash. '"/usr/bin/env | grep '^PATH'" yields: PATH=/apps/lsf/9.1/linux2.6-glibc2.3-x86_64/etc:/apps/lsf/9.1/linux2.6-glibc2.3-x86_64/bin:~/bin:~/.local/bin:... And I have confirmed that a 'python' can NOT be found in any of the directories preceeding '~/.local/bin'. Commented Mar 26, 2014 at 20:57
  • 2
    try type -a python under bash. Also in case you just installed (or moved) the python executable be sure to run a new shell so it sees it on startup. Commented Mar 26, 2014 at 21:44

1 Answer 1

6

Most likely what's happening is you don't have your PATH variable exported to the environment. In that case, /usr/bin/env won't have a PATH set, and its execvp call will end up searching a small set of default directories (which typically includes /usr/bin, naturally).

To see this (in bash):

$ export PATH $ declare -p PATH # verify PATH is exported, denoted by the -x declare -x PATH="<my usual path, omitted...>" $ /usr/bin/env python -V # shows my own python build Python 2.7.6 $ export -n PATH # un-export PATH $ declare -p PATH declare -- PATH="<my usual path, omitted...>" $ /usr/bin/env python -V # shows the system (/usr/bin/python) version Python 2.6.6 

So, in summary, make sure to export PATH somewhere in your shell dotfiles.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.