126
 gonzo  ~/a/packages  conda env list
# conda environments:
#
ppo_latest               /nohome/jaan/abhishek/anaconda3/envs/ppo_latest
root                  *  /nohome/jaan/abhishek/anaconda3

 gonzo  ~/a/packages  conda activate ppo_latest
 gonzo  ~/a/packages  which python                                                                                     (ppo_latest)
/nohome/jaan/abhishek/anaconda3/bin/python
 gonzo  ~/a/packages  conda deactivate                                                                                 (ppo_latest)
 gonzo  ~/a/packages  which python
/nohome/jaan/abhishek/anaconda3/bin/python

The environment gets activated without an error. Then we check which python it is referring to it. It is doesn't change, why?

Abhishek Bhatia
  • 9,404
  • 26
  • 87
  • 142
  • 2
    Did you check your env folder? Mine was empty because i didnt provide a python version. If its empty gets activated but theres no python. – Julian Nov 04 '19 at 16:28
  • use this in your cmd: ```activate ``` – Swapnil Sep 09 '20 at 22:57
  • 1
    For others who are experiencing this problem, the problem could also be this known bug: https://github.com/conda/conda/issues/9392 – user4815162342 Oct 01 '20 at 18:47
  • Closing the current terminal and opening another one worked for me. – Mahmoud Mousa Hamad Dec 12 '21 at 01:16
  • As @Julian mentions, I discovered that if I didn't create the environment with an explicit Python version then it simply defaults to something like `/usr/bin/python` rather than using the base env Python version. This is an unexpected new behavior that seems more like a bug. – shellcat_zero Jul 02 '22 at 19:50

26 Answers26

145

As of conda 4.4, the command

conda activate <envname>

is the same on all platforms. The procedure to add conda to the PATH environment variable for non-Windows platforms (on Windows you should use the Anaconda Prompt), as well as the change in environment activation procedure, is detailed in the release notes for conda 4.4.0.


For conda versions older than 4.4, command is either

source activate <envname>

on Linux and macOS or

activate <envname>

on Windows. You need to remove the conda.

darthbith
  • 18,484
  • 9
  • 60
  • 76
  • 4
    Can't use source with fish. – Abhishek Bhatia Nov 13 '17 at 17:23
  • 1
    Then you need to add that requirement into your post. – darthbith Nov 13 '17 at 19:23
  • 2
    Also, according to this post, `source` is the correct command: https://superuser.com/a/84624/229278 You might need to install some additional dependencies: https://superuser.com/a/1235985/229278 – darthbith Nov 13 '17 at 19:28
  • 30
    I found this question after getting `CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.` on windows. The fix is to use `activate ...` instead of `conda activate ...` – cedd Jun 06 '18 at 13:20
  • @cedd Your edit is not correct. The new command is `conda activate` and is the same on all the platforms. I'm not sure what conda version you have, but that sounds like a separate question – darthbith Jun 06 '18 at 19:28
  • 1
    @darthbith. It's your answer, and I'm not a python expert, so do what you think is best. However, I have conda 4.5.4 on windows, and for me `conda activate ...` returns the CommandNotFoundError, whereas `activate ...` works as I would expect. – cedd Jun 07 '18 at 10:33
  • 1
    @cedd Are you using Powershell? Powershell is not supported: https://github.com/conda/conda/issues/626 You have to use the Anaconda Prompt. Also the [release notes](https://github.com/conda/conda/releases/tag/4.4.0) for conda 4.4 explicitly say that `conda activate` is the new way. – darthbith Jun 07 '18 at 13:30
  • @darthbith: Fair enough, I believe you! I was probably using git bash, but it might have been the windows 10 command prompt (definitely not powershell). Maybe I should be using the Anaconda prompt ... – cedd Jun 08 '18 at 09:10
  • This unfortunately does not answer the question. – Adam Erickson May 18 '19 at 01:26
  • -1 for not quoting the relevant part of the release notes. The point of SO answers is to not read external, giant walls of texts – Gulzar Feb 14 '22 at 12:50
  • @Gulzar Please feel free to edit the answer to include that, if you would find it valuable... – darthbith Feb 14 '22 at 17:25
64

I just ran into a similar issue. Recently started developing on windows, so getting used to the PowerShell. Ironically when trying to use 'conda activate ' in Git-bash i got the error

$ conda activate obf

CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
If using 'conda activate' from a batch script, change your
invocation to 'CALL conda.bat activate'.

To initialize your shell, run

    $ conda init <SHELL_NAME>

Currently supported shells are:
  - bash
  - cmd.exe
  - fish
  - tcsh
  - xonsh
  - zsh
  - powershell

See 'conda init --help' for more information and options.

IMPORTANT: You may need to close and restart your shell after running 'conda init'. 

Running the command in my PowerShell (elevated) as instructed did the trick for me.

conda init powershell 

This should be true across all terminal environments, just strange PowerShell didn't return this error itself.

Jordan Simba
  • 1,046
  • 7
  • 10
37

To use "conda activate" via Windows CMD, not the Anaconda Prompt:
(in response to okorng's question, although using the Anaconda Prompt is the preferred option)

First, we need to add the activate.bat script to your path:
Via CMD:

set PATH=%PATH%;<your_path_to_anaconda_installation>\Scripts

Or via Control Panel, open "User Accounts" and choose "Change my environment variables".

Then calling directly from Windows CMD:

activate <environment_name>

without using the prefix "conda".

(Tested on Windows 7 Enterprise with Anaconda3-5.2.0)

stephinity
  • 576
  • 5
  • 6
  • 1
    Note that you need to have administrator privileges to accomplish this. If you add the path using the process above without admin privileges, it will not raise an error. However, if you close and re-open the command prompt, and run ```echo %PATH%```, your change will not be there. – Foggy Aug 11 '20 at 16:24
  • @Foggy You can also add the Anaconda\Scripts PATH to your "user environment variables" without having admin rights (both on Win 7 & Win 10). These will then "persist" for new command prompts (after the first log-out / restart of your machine). – stephinity Aug 14 '20 at 08:31
  • @stephinity thanks this is the only solution that worked for me, on Windows 10 environment. – mnm Oct 06 '21 at 07:57
22

The anaconda functions are not exported by default, it can be done by using the following command:

source ~/anaconda3/etc/profile.d/conda.sh

conda activate my_env
Areza
  • 5,623
  • 7
  • 48
  • 79
sumellan shans
  • 221
  • 2
  • 2
14

As of conda 4.10.1, here is what worked for me using the Git Bash terminal in Sublime text 3 (same for cmd and Git cmd) on Windows:

$ source activate env_name

for me: $ activate env_name and $ conda activate env_name did not work!

to check the list of activated conda environments, in my case I use

$ conda env list

or

$ conda info --envs

the activated environment is preceded by *

note that I have already added anaconda to my path.

karbi
  • 193
  • 1
  • 12
10

I just created a new environment with conda and things are different. My sys.path was not correct for a bit until I figured out way.

As a result, I want to point out for anyone else confused by a change in conda, that if you have upgraded conda and created an environment, it will now tell you (as opposed to previous behavior):

# To activate this environment, use
#
#     $ conda activate test
#
# To deactivate an active environment, use
#
#     $ conda deactivate

Thus, the new way to activate/deactivate environments is to do it like the above.

Indeed, if you upgrade from an older version of conda and you try the above, you may see the following helpful message (which I did):

CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
If your shell is Bash or a Bourne variant, enable conda for the current user with

    $ echo ". ~/anaconda/etc/profile.d/conda.sh" >> ~/.bash_profile

or, for all users, enable conda with

    $ sudo ln -s ~/anaconda/etc/profile.d/conda.sh /etc/profile.d/conda.sh

The options above will permanently enable the 'conda' command, but they do NOT
put conda's base (root) environment on PATH.  To do so, run

    $ conda activate

in your terminal, or to put the base environment on PATH permanently, run

    $ echo "conda activate" >> ~/.bash_profile

Previous to conda 4.4, the recommended way to activate conda was to modify PATH in
your ~/.bash_profile file.  You should manually remove the line that looks like

    export PATH="~/anaconda/bin:$PATH"

^^^ The above line should NO LONGER be in your ~/.bash_profile file! ^^^

Changing the above fixed my issues with sys.path in activated conda environments.

erewok
  • 7,555
  • 3
  • 33
  • 45
  • 7
    The above instructions assume Mac/Linux env with the reference to a Bash or Bourne variant shell. What about for the standard Windows CMD shell? My miniconda3/etc/profile.d folder only has conda.sh and conda.csh files, but no Windows conda.bat version. Does one exist to support these instructions? Thanks. – okorng May 01 '18 at 06:30
  • 5
    I followed these instructions and I'm still getting that error, even though conda seems to be working otherwise. It's very strange. – szeitlin Sep 07 '18 at 00:28
10

If your console does not show (base) after running conda activate base, then try running:

conda init

Then running conda activate <your_env> should show the name of (<your_env>) at the beginning of the shell prompt.

This worked for me on Windows. My PATH environment variable was set properly so conda activate base did not raise any error but quietly failed.

Alex Ishida
  • 1,021
  • 8
  • 9
8
  • run conda init in cmd
  • restart cmd
  • run conda activate envName
Gimnath
  • 824
  • 9
  • 11
7

Try this:

export PATH=/home/your_username/anaconda3/bin:$PATH
in ~/.bashrc

Then source ~/.bashrc This works for me for the same problem.

Joykal Infotech
  • 1,840
  • 3
  • 9
  • 17
Shuai Li
  • 71
  • 1
  • 1
  • This is an answer for installations where there is no home folder as well. Sometimes there's only a root "user" and no home directory. Just add the path to miniconda/bin or anaconda3/bin (whereever you might have installed it) to $PATH and it should start running. e.g. `export PATH=/root/minconda3/bin:$PATH` – nlhnt Feb 17 '21 at 14:47
7
conda init

Run this in command prompt.

Worked for me.

sw_aka
  • 71
  • 1
  • 1
5

In the windows environment use "anaconda prompt" instead of "command prompt".

Shirantha Madusanka
  • 1,461
  • 1
  • 11
  • 16
  • Thanks. In Powershell on Windows 10, nothing happened, no error message but also no environment activation. Using the "anaconda prompt" worked. – tcmb Mar 17 '20 at 13:57
4

In my case the change of default terminal to command promt (cmd.exe) did a trick. VS Code - Windows 10

swed1983
  • 85
  • 6
2

For windows, Use the Anaconda Powershell Prompt and conda command to activate the virtual environment

enter image description here

Vijay Anand Pandian
  • 1,027
  • 11
  • 23
2

Just use this command in your cmd:

activate <envname>

Works like charm! (worked for windows, don't know about mac)

Swapnil
  • 1,812
  • 2
  • 7
  • 12
2

I had same issue but For linux this worked : in terminal Type:

$ bash
$ conda init
$ cd /path_that_include_env_dir
$ conda activate ./<env_name>

or

$ conda activate /env_path

"env_path" is full environment path as : /home/usr/env_dir

To check environments list and their paths $ conda env list

2

go settings and change "shell path" to "cmd" from power shell. i'm using pycharm on windows 10, and that fixed my prolem. settings>tools>terminal>shell path>cmd

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 14 '22 at 18:34
1

This solution is for those users who do not want to set PATH.

Sometimes setting PATH may not be desired. In my case, I had Anaconda installed and another software with a Python installation required for accessing the API, and setting PATH was creating conflicts which were difficult to resolve.

Under the Anaconda directory (in this case Anaconda3) there is a subdirectory called envs where all the environments are stored. When using conda activate some-environment replace some-environment with the actual directory location of the environment.

In my case the command is as follows.

conda activate C:\ProgramData\Anaconda3\envs\some-environment

Stephen Jacob
  • 889
  • 1
  • 15
  • 33
1

Here's what worked for me using the Git Bash terminal in VS Code on windows in succinct steps:

  1. source activate env-name - You should see your line appended by the (base) tag now.

  2. After calling on source activate, I've found following conda activate commands to work: i.e. conda activate env2-name

What didn't work for Git Bash (as a VS Code terminal) for me: activate env-name and conda activate env-name.

Not exactly sure why this specific behaviour occurs on the Git Bash terminal on VS Code, but the accepted answer + this stackoverflow question I've found might provide clues.

decoder247
  • 134
  • 2
  • 12
1

After installing conda in Linux if you are trying to create env just type bash and hit Enter later you can create env

prajwal b
  • 11
  • 3
1

I changed my shell from bash to zsh according to Apple prompt message and restarted the terminal, and it works for me after doing this.

Bicheng.Cao
  • 313
  • 3
  • 9
0

If nothing works for you, you can specify the full path of your python environment setup by conda.

For me, I set up an environment called "testenv" using conda.

I searched all python environments using

whereis python | grep 'miniconda'

It returned a list of python environments. Then I ran my_python_file.py using the following command.

~/miniconda3/envs/testenv/bin/python3.8 my_python_file.py

You can do the same thing on windows too but looking up for python and conda python environments is a bit different.

Wasim Khan
  • 31
  • 2
0

Have you tried with Anaconda command prompt or, cmd it works for me. Giving no error and activation is not working in PowerShell may be some path issue.

0

Hello let me share what worked for me. (WINDOWS USERS)

  1. Add conda to path(that is to your environment variables - the scripts folder to be precise) i.e. C:\Users\kboys\anaconda3\Scripts
  2. Reopen your cmd, type activate and the name of your target environment, i.e

$ activate <env_name>

$ activate tensorflow

Note this way you don't need to call conda.

0

I just created a new environment in miniconda, but when trying to activate it in VSCode's terminal, it gives this

CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'. If using 'conda activate' from a batch script, change your invocation to 'CALL conda.bat activate'.

Then I found in the VSCode settings this option, terminal.integrated.shellArgs:

Terminal options

By default, it is 'null', but changing it to 'Command Prompt', it made the 'conda activate [my env]' command in the VSCode terminal work.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
NVRSee
  • 1
  • 1
0

For using anaconda activate in Windows cmd/terminal
just simple Run as Administrator

ofir_aghai
  • 3,017
  • 1
  • 37
  • 43
-3

set-executionpolicy remotesigned Set-ExecutionPolicy unrestricted