Wrong Python version returned, with correct $PATH set in ZSH

0

something very wrong is happening with my environment set up. As I am trying to learn, I decided to reinstall Mac OSX El Capitan on my Macbook Pro and start installing and configuring stuff from scratch. Shell I am using is ZSH and I have configured it a bit with Oh-My-ZShell.

My $PATH and Python:

~ ❯ echo $PATH
/Users/edchigliak/bin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/edchigliak/bin
~ ❯ python --version
Python 2.7.10
~ ❯

Screenshot below clearly shows that there are two Python2's installed in /usr/bin and a Homebrewed Python3 in /usr/local/bin.

Terminal screenshot of Python2 and Python3 paths

Here is the result of printenv (only entries related to PATH and SHELL):

PATH=/Users/edchigliak/bin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/edchigliak/bin
PWD=/Users/edchigliak
SHELL=/usr/local/bin/zsh
SHLVL=2
SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.lRGo5iu4NA/Listeners
TERM=screen-256color
ZSH=/Users/edchigliak/.oh-my-zsh
__CF_USER_TEXT_ENCODING=0x1F5:0x0:0x0
_=/usr/bin/printenv

First entry in my .zshrc:

export PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/bin 

Many thanks!

Alex Starbuck

Posted 2018-04-13T10:44:52.607

Reputation: 155

None of these are called literally python which is the command you are typing in. What do you get with type python? – tripleee – 2018-04-13T11:30:20.180

@tripleee Hey thx for the quick reply. I get this python is /usr/bin/python – Alex Starbuck – 2018-04-13T12:18:54.720

1It's not entirely clear why you think this result is "wrong". – tripleee – 2018-04-13T12:42:34.397

I think it is wrong because I changed my $PATH in .zshrc file to export PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin and thought that which python should now return python 3xx – Alex Starbuck – 2018-04-13T12:46:08.310

It should not; only which python3.xx could possibly return that result. – tripleee – 2018-04-13T13:27:22.453

Answers

0

Only binaries named exactly python will be considered when you type python at the shell prompt.

If you want python to execute python3, the simple way to accomplish that is to set a function or alias in your personal shell startup files.

You absolutely mustn't make python point to python3 system-wide because this will break some OS and third-party tools which require python to be Python version 2. The two are mutually incompatible languages (or dialects, if you prefer).

To review what's happening here, when you type command at the prompt, the shell iterates through the directories in your PATH and looks for an executable file named exactly command in each, until it finds one, or runs out of places to look. It will simply ignore any files with other names, even if they would happen to be similar, like xcommand or commandante or command3.xx. If you have an alias or a function, that will be used instead, though your PATH will perhaps then be consulted to locate other commands called by your alias or function.

tripleee

Posted 2018-04-13T10:44:52.607

Reputation: 2 480

I understand this part, what I don't understand though is: why does which python return system one, even though I have changed the $PATH variable in my .zshrc file? ALso, why is the entry PATH in the output of the printenv set to this ridiculously long path instead of the one I set in .zshrc? Thanks! – Alex Starbuck – 2018-04-13T12:44:16.827

Probably other parts of your zsh startup files amend the path with something like PATH=$HOME/bin:PATH. It should only be necessary once, of course. But this has no direct relevance here and isn't at all mentioned in your actual question. – tripleee – 2018-04-13T13:31:24.170

0

The shell searches PATH for exact matches. When you type in python, it tries to find an executable matching ^python$; ^python...$ is not the same.

In your case, it could be there is no python in /usr/local/bin or perhaps there's a permissions issue with it, which would make it be skipped.

1) If there is no python in /usr/local/bin:

You could use sudo ln -s /usr/local/bin/python3.6 /usr/local/bin/python to create a soft link python pointing to the Python interpreter you want to use. Because /usr/local/bin is searched first for your PATH, the shell should now find this link.

2) If file exists in /usr/local/bin but doesn't have execute permission, try sudo chmod a+x /usr/local/bin/python.

Can't think of other problems right now.

Samuel

Posted 2018-04-13T10:44:52.607

Reputation: 125