0

I have a machine running OSX Yosemite (it has been through several versions of OSX, which may make a difference).

I noticed an anomily with whether python could import libraries depending on whether the script was run directly, i.e.

./Myscript.py

Or by expressly calling python

python Myscript.py

Now, if I type

$whereis python
/usr/bin/python

And my shebang line in the script is

#!/usr/bin/python

So I assumed that the same version of python was running in both cases.

But after investigating I find

$python --version
Python 2.7.6
$/usr/bin/python --version
Python 2.7.10

So it would seem that the python being executed is not the one I get when I do a whereis

Can anyone please shed some light on this, and also clarify how to fix it? I really want to be running 2.7.10 in both cases, since right now when I install libraries they go into 2.7.6, but when I run scripts, they run 2.7.10 and can't see the libraries.

Thanks

Jon

Jon Hodgson
  • 129
  • 10

1 Answers1

4

Don't use whereis, that command ignores your PATH environment variable. From the manpage:

The whereis utility checks the standard binary directories for the specified programs, printing out the paths of any it finds.

Emphasis mine.

You have a PATH environment variable that includes a 'nonstandard' binary directory. Use which to find where python comes from:

$ which python

which gives you the actual binary used for your current shell configuration:

The which utility takes a list of command names and searches the path for each executable file that would be run had these commands actually been invoked.

You could use which -a to find all possible completions for the command:

$ which -a python

Also see “whereis” and “which” return different paths in Mac OS X on Super User.

Demo:

$ PATH=/opt/homebrew/bin:$PATH whereis python
/usr/bin/python
$ PATH=/opt/homebrew/bin:$PATH which -a python
/opt/homebrew/bin/python
/usr/bin/python

So even with PATH explicitly pointing to my homebrew directory, whereis ignores it. which finds it and lists it first (the -a argument made it look for more options).

Community
  • 1
  • 1
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343