57

I have the following conda environment under Linux:

$ conda info -e
# conda environments:
#
py33                     /u21/coyotito/.anaconda/envs/py33
root                  *  /u21/coyotito/.anaconda

And py33 is created with this command:

$ conda create -n py33 python=3.3 anaconda

The problem is when I activate py33 it still cannot call Python version 3.3.

[coyotito@pearl ~]$ source activate py33
(coyotito)[coyotito@pearl ~]$ python --version
Python 2.7.10 :: Anaconda 2.1.0 (64-bit)
(coyotito)[coyotito@pearl ~]$ conda info -e
# conda environments:
#
py33                     /u21/coyotito/.anaconda/envs/py33
root                  *  /u21/coyotito/.anaconda

Namely it still calling old python. Notice also that the prompt under bracket is not (py33).

(coyotito)[coyotito@pearl ~]$ which python
~/.anaconda/bin/python

Instead of python in new environment:

~/.anaconda/envs/py33/bin/python3.3

How can I resolve this issue?

Update

My PATH environment in ~/.bash_profile looks like this:

export PATH=$HOME/.anaconda/bin:$PATH
Ulrich Eckhardt
  • 16,572
  • 3
  • 28
  • 55
neversaint
  • 60,904
  • 137
  • 310
  • 477
  • 1
    This is strange. If you get an error when typing `source deactivate` may be a sign that you have two concurrent versions of anaconda one in an environment? Maybe this is what is creating problems? – Reblochon Masque Apr 20 '16 at 03:58
  • 1
    When you are in your root conda environment what does `which activate` return? – Paul Apr 20 '16 at 14:48
  • @Paul: `$HOME/bin/activate` – neversaint Apr 21 '16 at 00:28
  • Same happens to me when I create an environment for 2.7, when I use a more modern python 3.4 anaconda : / I need 2.7 for an online course, which still didn't update their material. – Zelphir Kaltstahl May 30 '16 at 15:41

8 Answers8

79

I had the exact same problem. Not sure what I did to get into that mess, but I solved it with a simple:

conda deactivate
conda activate foo_env

(If you have activated multiple environments, you may need to run conda deactivate multiple times.)

Tahlor
  • 1,642
  • 1
  • 17
  • 21
  • 4
    I can confirm, but do you know why exactly do we need to deactivate ? – marman Sep 23 '19 at 17:43
  • 2
    In my case, the problem happened after installing new packages. `conda` was "forgetting" it was using a different python version. – Dr_Zaszuś Jun 25 '20 at 09:50
  • 13
    Just make sure you exit the default 'base' environment too. – Jerry Ajay Sep 21 '21 at 19:06
  • 1
    what @JerryAjay said saved me, conda will load 'base' environment by default when terminal initializes. Screwed up my whole path. – jtgi Jan 07 '22 at 07:33
  • O oh... this is not the first time I'm here. This does solve it and I don't understand what causes it. So that I don't come back another time I added to my .zshrc: `function condaenv() { conda deactivate; conda activate $1;}` and use `condaenv ` – milcak Apr 25 '23 at 17:47
  • @JerryAjay That was it for me – Eduardo Pignatelli May 18 '23 at 16:41
46

TLDR;

# deactivate Conda environment
# (until even base environment is deactivated)
conda deactivate
# activate your environment
conda activate your_env_name_goes_here

try this

Activate an environment A and then check the location of Python package by using the command below.

python -c "import sys; print(sys.executable)"

Activate another environment, let's say environment B and rerun the above python command. If conda isn't using the correct Python version then most likely running the above command will print the same path in both environments.

My conda installation wasn't using the correct version because I had activated my environment on top of the conda base environment.

Deactivating the base environment and then activating the environment I wanted, worked.

Conda calls this nested activation and you could read more about it in the docs.

If you wanted to Disable Nested Activation

conda config --set auto_stack 0

If you wanted to Disable Base Env Activation

conda config --set auto_activate_base false
Raza
  • 831
  • 9
  • 10
  • 8
    This really helped. I first deactivated my current env, then deactivated base env, and then reactivated my env. Worked. – Cletus Ajibade May 07 '20 at 02:46
  • 4
    Yes, deactivating all the environments did the trick for me. – 2dor Sep 22 '20 at 11:32
  • 5
    This worked for me, I did not notice I was activating from the base environment until running across this answer. All good now. – jthomas Oct 28 '20 at 14:42
  • 1
    This worked for me, thanks! still, this is strange, I don't get the same behavior on fedora. If I activate the env on top of the base env, it changes as it should. – jorgehumberto Jun 01 '21 at 09:31
  • this worked for me as well. is there to change the default setting such that the base environment is not activated to start with? – doij790 Jun 26 '22 at 23:40
  • 2
    Another way (on Unix) to check which python you're using: `which python`. Also useful for pip problems. – Théo Rubenach Jul 04 '22 at 08:55
11

Landed here with this same issue, but by moving out of the anaconda executable directory, the correct python was called. I was in a directory that contained the python executable that was installed with Anaconda2.

Example:

(py35) C:\Anaconda>python --version
Python 2.7.11 :: Anaconda 4.0.0 (64-bit)

(py35) C:\Anaconda>cd ..

(py35) C:\>python --version
Python 3.5.2 :: Anaconda 4.2.0 (64-bit)

(py35) C:\>
lwright
  • 111
  • 1
  • 5
  • 1
    thank god finally I got to know this after wasting hours, everything was correct once I moved to another directory: ) – Abhinav Kumar Sep 05 '21 at 08:42
  • Besides the documented security reason, this example is another justification for not keeping "." in your PATH environment, if you are using any of *NIX based operating systems. – Seshadri R May 07 '22 at 08:17
6

This only happens when you create an environment without specifying any additional packages. I am using Conda 4.10.3

Refer https://github.com/conda/conda/issues/9392#issuecomment-696897058

The following comment from the above link worked in my case:

I noticed that it works if i install a packet when i create the environment.

antony@antony-fedora : ~/Workspace/tmp : which python /usr/bin/python
antony@antony-fedora : ~/Workspace/tmp : conda create --name my_env 
antony@antony-fedora : ~/Workspace/tmp : conda activate my_env 
(my_env) antony@antony-fedora : ~/Workspace/tmp : which python
/usr/bin/python 
(my_env) antony@antony-fedora : ~/Workspace/tmp : conda create --name my_env pip  
(my_env) antony@antony-fedora : ~/Workspace/tmp : conda activate my_env 
(my_env) antony@antony-fedora : ~/Workspace/tmp : which python ~/anaconda3/envs/my_env/bin/python

Also, recommend adding conda deactivate to the end of your .zshenv or .bash_profile

wuerfelfreak
  • 2,363
  • 1
  • 14
  • 29
rhimanshu909
  • 61
  • 1
  • 1
4

So in my situation, a intern before me append anaconda path to path variable in /etc/profile, which override my conda setting in ~/.bashrc, simply removing those line in /etc/profile can help.

Chimaopen
  • 41
  • 1
  • I was my own intern, this was messed up on the back of me trying to fix pyenv. Thanks for the tip. – bidi Aug 19 '21 at 17:35
2

I had the same issue and solved it by defining the Python version

conda create --name env_name python=3.10

instead of

conda create --name env_name
EnesZ
  • 403
  • 3
  • 16
1

With MacOS, I was facing a similar issue. I was able to resolve the issue by changing the python interpreter (appears on bottom left corner) within VS-code.

0

I had a similar issue with macOS Ventura 13.2.1. This was caused by vscode which first activates base then the specified environment. As it has already been suggested

# deactivate Conda environment
# (until even base environment is deactivated)
conda deactivate
# activate your environment
conda activate your_env_name_goes_here

also worked but didn't quite solve the problem since I had to do this every time I opened a vscode terminal. The real problem is that there is no python in the base environment installed so I switched into the base env

# deactivate Conda environment
# (until even base environment is deactivated)
conda deactivate
# activate your environment
conda activate base

Then install python with

conda install python=3.10 #for example

I also had a permission denied error there so I made sure the specific folder had correct access writes before trying again

sudo chmod a+rwx <path_to_miniconda> 
conda install python=3.10 #for example

NOTE: you probably don't need to have the x in a+rwx but I've only tested it this way

PS: I also had some aliases configured in my .zshrc (or .bashrc if you use bash) which I had to remove

alias python=python3
alias pip=pip3

then it worked like a charm!

Hope that helps!

Tallyho
  • 1
  • 1