61

Version Anaconda 4.8.3

What I cannot do:
I can't activate any environment from powershell. conda activate base

What I can do:
conda env list conda create -n xxx conda remove -n xxx

I have tried many solutions, but they turn out to be useless:

  1. conda init powershell
    restart powershell
    conda activate

  2. conda update -n base conda and redo 1

  3. conda install -n root -c pscondaenvs pscondaenvs
    Set-ExecutionPolicy RemoteSigned
    Run activate base

The result of running conda activate

Z Chen
  • 721
  • 1
  • 6
  • 5
  • 1
    Were you running Powershell with elevated/admin permissions when you tried `conda init` ? There were no errors? – AMC Oct 01 '20 at 23:58
  • Yeah, I tried both admin and non-admin powershell, and the result turns out the same. " no change " @AMC – Z Chen Oct 07 '20 at 16:29

8 Answers8

83

You don't need Admin permission.

Once you install Anaconda or Miniconda on Windows, open a Anaconda Powershell Prompt from Start Menu.

Or, If you don't see it there, then assuming you have installed miniconda3 at path C:\miniconda3\4.9.2, do:

powershell -ExecutionPolicy ByPass -NoExit -Command "& 'C:\miniconda3\4.9.2\shell\condabin\conda-hook.ps1' ; conda activate 'C:\miniconda3\4.9.2' "

Now try:

conda init powershell

and reopen powershell.

Additional note: By default conda will autoactivate itself, when we open terminal. If you prefer not, then disable auto-activation with:

conda config --set auto_activate_base false
prashant
  • 1,144
  • 9
  • 15
  • 2
    Solution looked promising, but actually it is not bypassing the restriction and after restart (with full modificatoin of files by conda) it is still not working properly. On restart the same ```\WindowsPowerShell\profile.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170. At line:1 char:3``` (referencing the automatically created ps script) – CheTesta Jun 08 '21 at 14:36
  • 3
    In this case you simply need to [unblock the file named `profile.ps1`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.security/set-executionpolicy?view=powershell-7.1#example-7--unblock-a-script-to-run-it-without-changing-the-execution-policy): `Unblock-File -Path .\profile.ps1` (replace relative path with full path) See [Unblock-File cmdlet doc](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/unblock-file) – gcharbon Aug 28 '21 at 10:40
  • Does that work on Powershell 7? (Doesn't seem to on my machine but it may be from another cause). – LoneCodeRanger Mar 12 '23 at 11:03
71

Open a Anaconda Powershell Prompt from Start Menu. Now Try:

conda init powershell

Now restart the powershell, if find some error like this in powershell:

\WindowsPowerShell\profile.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170. At line:1 char:3

Then change the execution policy. Type this code to powershell:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned

To find more with execution policy you can visit this link.

sunghangga
  • 711
  • 4
  • 2
  • 21
    without admin priviliges: `Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser` – Winand Sep 03 '21 at 09:01
  • This is the best answer with the comment from @Winand. Solved my headaches with VS Code's integrated terminal. – Matt Jun 09 '23 at 15:25
27

Here is my easier solution which works with Anaconda, Miniconda, and even Miniforge:

  1. Open Powershell and browse to condabin folder in your conda installation directory, for example: C:\Users\<username>\anaconda3\condabin
  2. Run ./conda init powershell in that folder, and re-open the powershell.
  3. Please note: If you encountered ps1 cannot be loaded because running scripts is disabled on this system, simply run the Powershell as Administrator and enter the following: Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted
  4. Restart the Powershell & Enjoy!
8

When you use Anaconda or Miniconda type in Anacondaprompt:

conda init powershell
maanijou
  • 1,067
  • 1
  • 14
  • 23
Matthias
  • 81
  • 1
  • 1
4

In order to activate conda on Powershell, I just executed this command:

C:\Users\<username>\anaconda3\shell\condabin\conda-hook.ps1

Then I could see all environments without needing to restart Powershell.

conda env list
conda activate base

So you need to basically just run the conda-hook.ps1 script.

gil.fernandes
  • 12,978
  • 5
  • 63
  • 76
3

Encountered the same problem. The trick is to make sure that the environment is included in the PATH variable of Windows Powershell. To view the PATH variables enter the following in the powershell:

$env:Path.split(';')

If the environment you desire doesn't show up in the output list, as in ..... ..... ..../envs/yourEnv/... .....

run the following command on cmd:

conda init powershell

then restart _Anaconda Powershell, then:

conda activate yourEnv

Hopefully, now the conda prompt should now show your desired environment.

Hex Heager
  • 107
  • 7
2

To solve the problem without changing te security policy on powershell, you need to be on powershell 7. Powershell 7 will let you use a \WindowsPowerShell\profile.ps1 file as startup script with default (restricted) security policy settings. Windows Powershell is by default (on windows 10 at 14-08-2022) version 5.x.

Check your powershell version with

$PSVersionTable

If you're still on powershell 5, install powershell from the windows store. This will install powershell 7 alongside powershell 5. It will probably be called Powershell instead of Windows Powershell. Or if you're not allowed to use the windows store, ask IT to install it for you. Check the version again in the new powershell.

After this, we can follow the answer of prashant:

Open Anaconda prompt and run

conda init powershell

To stop conda starting by default run

conda config --set auto_activate_base false
JeeHaa
  • 23
  • 3
0

& 'C:\ProgramData\Miniconda3\shell\condabin\conda-hook.ps1'; conda activate 'C:\ProgramData\Miniconda3'

This is the command found in Anaconda Powershell Prompt (Miniconda3).lnk on my computer.

On my machine, I can enter the environment anywhere by executing these two commands.

Andy
  • 1,077
  • 1
  • 8
  • 20