Python – The Python Command Starts the Wrong Version of the Python Interpreter

pythonpython3

I am using Mac OS X Version 10.13.1 and I have just installed anaconda. I have created a virtual environment using the command

conda create -n py3 python=3

Then, I have started the python interpreter using the command

python

To my surprise, the preinstalled python 2.7 from /usr/bin showed up instead of python 3.6. In order to check what is going wrong I issued the command

which python

The result was even more surprising, I got the following:

/Users/karlstroetmann/anaconda2/envs/py3/bin/python

When I then invoked the command

/Users/karldrstroetmann/anaconda2/envs/py3/bin/python

I did get python 3.6.3. But I don't understand why I cannot invoke this version by just typping python. What am I missing here? Any hints would be very much appreciated.

Best Answer

It's very likely that the python command has been hashed and that you need to clear the cache. In order to see what executable is actually being run you can use the type command, e.g.:

type -a python

Unlike the which command, the type command is aware of hashed programs, as well as aliases and shell functions.

For further discussion of which (no pun intended) commands to use to determine which programs are executed by the shell, see the following post:

Alternatively, you can also use the hash command itself to determine if a given command has been hashed, e.g:

hash -t python

You can also list all hashed commands by running hash without any arguments, i.e.:

hash

Similarly, you can use the alias command to check if a given command is an alias, e.g.:

alias python

And you can list all active aliases as well:

alias

To clear the cached Python program you can use the following command:

hash -d python

Alternatively, you can clear everything all at once:

hash -r

To clear a single alias you could use the unalias command, e.g.:

unalias python

Or you could clear all aliases at once:

unalias -a
Related Question