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.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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
Method 2
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.
Method 3
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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0