Search code examples
pythonpowershellanaconda

Conda not activated in Power Shell


I have already install anaconda on my Windows 10 laptop. I'm trying to activate the Python environment named pyenv.

First, I check the conda env list in my laptop, this is the output on the power shell:

PS C:\Users\User> conda env list
# conda environments:
#
base                  *  C:\Users\User\Anaconda3
pyenv                    C:\Users\User\Anaconda3\envs\pyenv

Then I activate pyenv:

PS C:\Users\User> conda activate pyenv

But I check again, it still activates base environment:

PS C:\Users\User> conda env list
# conda environments:
#
base                  *  C:\Users\User\Anaconda3
pyenv                    C:\Users\User\Anaconda3\envs\pyenv

When I use the Anaconda prompt, it works normally:

(base) C:\Users\User>conda activate pyenv

(pyenv) C:\Users\User>

Does anyone know why it causes this problem and how to fix this?

Update:

Running conda init powershell:

PS C:\Users\User> conda init powershell
no change     C:\Users\User\Anaconda3\Scripts\conda.exe
no change     C:\Users\User\Anaconda3\Scripts\conda-script.py
no change     C:\Users\User\Anaconda3\Scripts\conda-env-script.py
no change     C:\Users\User\Anaconda3\condabin\conda.bat
no change     C:\Users\User\Anaconda3\Library\bin\conda.bat
no change     C:\Users\User\Anaconda3\condabin\_conda_activate.bat
no change     C:\Users\User\Anaconda3\condabin\rename_tmp.bat
no change     C:\Users\User\Anaconda3\condabin\conda_hook.bat
no change     C:\Users\User\Anaconda3\Scripts\activate.bat
no change     C:\Users\User\Anaconda3\condabin\activate.bat
no change     C:\Users\User\Anaconda3\condabin\deactivate.bat
modified      C:\Users\User\Anaconda3\etc\profile.d\conda.sh
modified      C:\Users\User\Anaconda3\etc\fish\conf.d\conda.fish
no change     C:\Users\User\Anaconda3\shell\condabin\Conda.psm1
modified      C:\Users\User\Anaconda3\shell\condabin\conda-hook.ps1
no change     C:\Users\User\Anaconda3\Lib\site-packages\xontrib\conda.xsh
modified      C:\Users\User\Anaconda3\etc\profile.d\conda.csh
modified      C:\Users\User\Documents\WindowsPowerShell\profile.ps1

Update 2: It works when using CMD:

C:\Users\User>conda activate pyenv

(pyenv) C:\Users\User>

Solution

  • After a while, my Powershell appear this error when I open it.

    . : File C:\Users\User\Documents\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
        + . 'C:\Users\BinoyGhosh\Documents\WindowsPowerShell\profile.ps1'
        +   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            + CategoryInfo          : SecurityError: (:) [], PSSecurityException
            + FullyQualifiedErrorId : UnauthorizedAccess
    

    Then I found this solution.

    • Run Powershell as Administrator
    • Run this line set-executionpolicy remotesigned
    • Close the terminal

    Then it works.

    enter image description here