5

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.

1
  • hash -t python? Then try hash -r if it doesn't match & see. Nov 12, 2017 at 1:14

3 Answers 3

11

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
1
  • 1
    Maaan... for a linux newbie... this wasted a good couple of ours of my time... > installs python > python --version > WRONG version > which python > THIS/python > THIS/python --version > correct version... Turns out it was hashed... :( Jun 17, 2019 at 13:39
0

I have solved my problem, it was a stupid mistake: I had an alias for python in my .bashrc. I had forgotten about this alias and hence the problem resulted.

0

This can also happen if you invoked your virtual env from the anaconda (base) environment. Use conda deactivate to exit out of all anaconda environments, then conda activate <myenv> to get back to your env.

That worked for me anyway. Found it in a StackExchange thread.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .