PyCharm terminal doesn't activate conda environment
Asked Answered
B

17

41

I have a conda environment at the default location for windows, which is C:\ProgramData\Anaconda2\envs\myenv. Also, as recommended, the conda scripts and executables are not in the %PATH% environment variable. I opened a project in pycharm and pointed the python interpreter to C:\ProgramData\Anaconda2\envs\myenv\python.exe and pycharm seems to work well with the environment in the python console, in the run environment, and in debug mode.

However, when opening the terminal the environment is not activated (I made sure that the checkbox for activating the environment is checked). To be clear - when I do the same thing with a virtualenv the terminal does activate the environment without a problem.

Here are a few things I tried and did not work:

  • Copied the activate script from the anaconda folder to the environment folder
  • Copied the activate script from the anaconda folder to the Scripts folder under the environment
  • Copied an activate script from the virtualenv (an identical one for which the environment is activated)
  • Added the anaconda folders to the path

None of these worked. I can manually activate the environment without a problem once the terminal is open, but how do I do it automatically?

Blanc answered 22/2, 2018 at 10:14 Comment(0)
R
42

I ran into the same problem and used this solution.

  1. Go to File -> Settings -> Tools -> Terminal.

  2. Replace the value in Shell path with cmd.exe "/K" C:\path\to\Anaconda3\Scripts\activate.bat your_environment_name.

If I installed Anaconda in C:\Anaconda3 and have an environment named myenv, then my settings would look like this:

Terminal Tool Application settings

Ringnecked answered 4/7, 2018 at 20:21 Comment(6)
Thanks! This is a great workaround, but will not work if I have multiple environments for testing purposes. Also, of course some minor changes are needed for linux/macBlanc
Do I need to do something more to get this to work? This activates the environment when opening the terminal, but not when trying to use "Run configuration".Ottar
@Ottar You need to configure your project interpreter. Changing the shell path doesn't effect your run/debug configurations, it's a separate setting.Ringnecked
If I have both conda env and virtualenv env in Pycharm, what should I do, it seems not recognizing the virtualenv env at all.Radish
it will work! Shell path: powershell.exe -ExecutionPolicy ByPass -NoExit -Command "& 'C:\tools\miniconda3\shell\condabin\conda-hook.ps1'Autosome
With @Roald's answer below and this answer, I added the environment variable CONDA_ENV=my_venv_name in project settings and changed Shell path as cmd.exe "/K" C:\Anaconda3\Scripts\activate.bat %CONDA_ENV%. This is to set venv in terminal of each project with different venv. If your projects use same venv you don't need this.Collectivism
D
10

If any one wondering for settings for Linux, here is how to do it. Create a file .pycharmrc in your home dir. Open the file and add following

source ~/.bashrc
source ~/anaconda3/bin/activate your_env_name 

Now go to Pycharm File > Settings > Tools > Terminal > Shell path replace your shell path with /bin/bash --rcfile ~/.pycharmrc.

Now when you open your terminal specified conda env will activate.

Delk answered 13/10, 2018 at 12:32 Comment(2)
Thanks! This works great for PyCharm 2020.2.3 (PopOS! 20.04 LTS)Compander
Works for 2021.3.2 CE on Mac OSXClinometer
F
9

This seems to be a known issue.

Franzen answered 28/2, 2018 at 11:41 Comment(2)
FYI: that issue has been fixed in version 2019.1.Jugal
It has not been fixed/resurfaced, In version 2021.1.3Dinodinoflagellate
W
8

Experienced the issue on Windows & Pycharm 2022.1 and conda environment My solution is to change default Powershell to cmd shell

  1. Open Settings > Tools > Terminal
  2. Change Shell path from "powershell.exe" to "cmd.exe" enter image description here
  3. Make sure the "Activate virtualenv" checkbox is checked.

New terminal correctly opens with configured environment.

Waterfront answered 17/5, 2022 at 16:11 Comment(2)
not working for me on 2022.2Muns
I had this issue on 2022.3.2 Professional and it worked! Why is this not marked as the accepted solution?Quanta
A
7

Solution for Windows

Go to Settings -> Tools -> Terminal
set Shell path to:

  1. For powershell (I recommend this):
    powershell.exe -ExecutionPolicy ByPass -NoExit -Command "& 'C:\tools\miniconda3\shell\condabin\conda-hook.ps1'"
  2. For cmd.exe:
    cmd.exe "C:\tools\miniconda3\Scripts\activate.bat"

PyCharm will change environment automatically in the terminal
PS: I'm using my paths to miniconda, so replace it with yours

Autosome answered 27/10, 2021 at 8:58 Comment(3)
This helped me get going. Using this I came up with a general working solution. See below.Toilsome
I tried the first solution and didn't worked well in my case, but the second one worked perfectly even when changing the environment in pycharm so thank you very much!Barraza
@MarinerZZ, there was a typo in the first solution, fixed that.Autosome
T
7

Windows & Powershell

I used partial answers from the answers here and this answer to come up with the following solution on Windows with Powershell

  1. Go to File > Settings > Terminal
  2. For each project add the following under Environmental variables. Here for each project you need to change venv_name to the name of your virtual environment name.
    • CONDA_ENV=venv_name
  3. For the shell path use the following. This you do not need to change for each project, since it uses the environmental variable you declared in the previous step. Just edit the path to your Anaconda/Miniconda installation.
    • powershell.exe -ExecutionPolicy ByPass -NoExit -Command "& 'C:\path\to\miniconda3\shell\condabin\conda-hook.ps1' ; conda activate $env:CONDA_ENV "
Toilsome answered 29/3, 2022 at 8:18 Comment(1)
If you want to use Powershell on Windows and also want to activate project specific virtual environment immediately, this might be a best option.Swig
S
6

Here's my solution for MacOS or Linux users:

First, add this to your ~/.bash_profile or ~/.zshrc depends on your shell. Remember to put it after conda has been initialized:

##### Activate conda env ######
[[ -n $CONDA_ENV ]] && conda activate $CONDA_ENV

And then go to your PyCharm settings, go to Tools -> Terminal In Project Settings, add CONDA_ENV=yourenv to your Environment Variables.

yourenv is the env name from your conda for this specific project.

Shapely answered 12/4, 2019 at 19:36 Comment(3)
This is a great idea, but: (1) First I had to run conda init bash just once (2) I had to prepend conda deactivate before the suggested line, otherwise conda activates the base env by default (3) The -n should not be necessary.Arlinda
This worked perfectly for me on Windows 10 with PyCharm's terminal pointing to GitBashKauslick
On PowerShell, add conda activate $env:CONDA_ENV to your profile.ps1 to activate the conda environmentXenophobe
F
3

Here's a working solution for Windows* Pycharm version: 2022.1.3 Windows: 11

Open up the anaconda prompt and type conda init powershell Notice the highlighted path for the powershell profile (C:\ProgramData\Anaconda3\shell\condabin\conda-hook.ps1), and copy that.

retrieving conda-hook powershell profile

Open up the project in PyCharm and press Ctrl+Alt+S to bring up the settings and navigate to Tools > Terminal. On the right side, in Environment variables text field, set a new environment variable as CONDA_ENV=your_conda_env_name Note that you have to mention your conda environment name correctly. Then in the shell path, type the following.

powershell.exe -ExecutionPolicy ByPass -NoExit -Command "& 'path/to/conda-hook.ps1' ; conda activate $env:CONDA_ENV "

for example:

powershell.exe -ExecutionPolicy ByPass -NoExit -Command "& 'C:\ProgramData\Anaconda3\shell\condabin\conda-hook.ps1' ; conda activate $env:CONDA_ENV "

setting the environment vars and shell path

Apply and close settings, close all the terminals if you have opened any, and try opening a terminal back. The mentioned conda environment should be automatically activated. Cheers!

Fecal answered 8/7, 2022 at 5:2 Comment(0)
M
2

Expanding on darksinge's answer to accommodate for conda>=4.4 and multiple environments, if 1) your conda environments and projects share the same name and 2) you keep your projects in the same directory, you can use this workaround:

cmd.exe "/K" C:\path\to\Anaconda3\Scripts\activate.bat C:\path\to\Anaconda3 & activate %cd:C:\path\to\project\parent\directory\=%

The last part (%cd:C:\path\to\project\parent\directory\=%) should infer the project name from the current working directory. For example, I keep my projects in Z:\, so %cd:Z:\=% returns my project name. You can read more at: How to replace substrings in windows batch file

Mince answered 10/9, 2018 at 21:22 Comment(2)
I'm currently searching for a special name/variable in pycharm to use the project name in settings (eg cmd.exe "/K" C:\path\to\Anaconda3\Scripts\activate.bat C:\path\to\Anaconda3 & activate $project_name. not found yetGhassan
Thanks this gave me the idea to try "C:\Program Files\Git\bin\bash.exe" --login && conda activate myenv for Git Bash and it worked!Gatian
P
1

Found a solution. Problem is we have been creating conda environments from within Pycharm while starting a new project.

This is created at the location /Users/<username>/.conda/envs/<env-name>.

e.g. /Users/taponidhi/.conda/envs/py38.

Instead create environments from terminal using conda create --name py38. This will create the environment at /opt/anaconda3/envs/.

After this, when starting a new project, select this environment from existing environments. Everything works fine.

Phyto answered 10/10, 2020 at 9:31 Comment(0)
G
1

Mixing a few answers from here, I figured out a solution for Git Bash in Windows:

  1. Go to File -> Settings -> Tools -> Terminal.

  2. Replace the value in "Shell path" with

"C:\Program Files\Git\bin\bash.exe" --login && conda activate myenv
Gatian answered 8/3, 2021 at 2:48 Comment(1)
worked for me even by adding only "C:\Program Files\Git\bin\bash.exe"Muns
I
1

Solved for windows users based on the linux solution.

  1. Create a batch script (mine named env_win10.bat) in the project root(important) and paste in it the following:
@echo OFF
:: conda activation path
set conda_activate_path=C:\ProgramData\Anaconda3\Scripts\activate.bat

:: environments for this project on several of different computers
set env_path=C:\Users\GiladEiniKbyLake\.conda\envs\bin
set env_basename=bin

if exist %env_path% (
    echo activating env: %env_path%
    %conda_activate_path% %env_basename%
) else (
    echo cant find path %env_path%
    echo activating base interpreter...
    %conda_activate_path%
)
  1. edit in the bat file the paths to:
  • conda_activate_path to the activate.bat of anaconda
  • env_path to the Environment folder
  • env_basename to the Environment name (dont know how to get os.path.basename() on batch script)
  1. On pycharm goto Tools->Terminal->shell and enter
cmd.exe "/K" ./env_win10.bat
  1. Bonus:
  • create a shortcut on desktop and insert pycharm exe path and name it in your project name. e.g. wizzi_utils.
  • right click properties and add in the target the path to your project. e.g.
"C:\Program Files\JetBrains\PyCharm Community Edition 2018.2.4\bin\pycharm64.exe" "D:\workspace\2021wizzi_utils"
  • now your project have a shortcut

Notice

you now have to do it on every project you open in pycharm because pycharm will look for "env_win10.bat" in the root folder of the project. The good news are that you need to do it once per project. It's recommended to put it on ".gitignore", and create this batch script on every user's computer. What if you do want it combined for all users and upload it to git? just add more paths variables. here is an example of a project of mine that i code on my laptop and my desktop:

@echo OFF
:: conda activation path
set conda_activate_path=C:\ProgramData\Anaconda3\Scripts\activate.bat

:: environments for this project on several of different computers
set laptop_env_path=C:\Users\gilad\.conda\envs\bin
set laptop_env_basename=bin
set desktop_env_path=C:\Users\GiladEiniKbyLake\.conda\envs\bin
set desktop_env_basename=bin

:: checking if we are on the 1st option
if exist %laptop_env_path% (
    echo activating env: %laptop_env_path%
    %conda_activate_path% %laptop_env_basename%
) else (
    :: checking if we are on the 2st option
    if exist %desktop_env_path% (
        echo activating env: %desktop_env_path%
        %conda_activate_path% %desktop_env_basename%
    ) else (
        :: fail safe - just activate conda base interpreter
        echo cant locate any of the paths:
        echo    %laptop_env_path%
        echo    %desktop_env_path%
        echo    activating base interpreter...
        %conda_activate_path%
    )
)
Isacco answered 31/7, 2021 at 13:30 Comment(0)
T
0

I am using OSX and zshell has become the default shell in 2020. I faced the same problem: my conda environment was not working inside pycharm's terminal.

File -> Settings -> Tools -> Terminal. the default shell path was configured as /bin/zsh --login

I tested on a separate OSX terminal that /bin/zsh --login somehow messes up $PATH variable. conda activate keep adding conda env path at the end instead of at the beginning. So the default python (2.7) always took precedence because of messed up PATH string. This issue had nothing to do with pycharm (just how zshell behaved with --login),

I removed --login part from the script path; just /bin/zsh works (I had to restart pycharm after this change!)

Tichonn answered 16/10, 2020 at 7:5 Comment(0)
E
0

I ran into the same problem and used this solution.

  1. Go to File -> Settings -> Tools -> Terminal.

  2. Replace the value in Shell path with

    powershell.exe -ExecutionPolicy ByPass -NoExit -Command "& conda activate <yourcondaenvname>

Extenuation answered 29/1, 2023 at 7:11 Comment(2)
Please give an explanation on why does this work or what does the syntax mean.Ario
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.Monahon
L
0

Go to Tools -> Terminal (by pressing Ctrl+Alt+S) in Pycharm.

Configure the following two settings.

  1. Environment variables : CONDA_EVN=[Complete Conda Virtual Env Name] i.e. CONDA_ENV=Python 3.8 (EmbeddingService) C:\Users\Anaconda3\envs\Application\python.exe
  2. Shell path : C:\WINDOWS\system32\cmd.exe
Lessielessing answered 8/8, 2023 at 7:41 Comment(0)
S
0

My solution is: open conda_packages.xml C:\Users\your_username\AppData\Roaming\JetBrains\PyCharm2023.1\options\conda_packages.xml And fixed your conda execution file path

<application>

  <component name="PyCondaPackageService">

    <option name="PREFERRED_CONDA_PATH" value="D:\ProgramData\anaconda3\Scripts\conda.exe" />

  </component>

</application>

As for errors caused by other reasons, you can open log file in pycharm: Help > Show Log in Explorer When you open the latest log file. Then you will see something like this.

2024-07-21 19:16:42,429 [  17847]   INFO - #c.j.p.s.s.PySkeletonGenerator$Run - D:\ProgramData\conda_env\envs\my_env\python.exe C:/Users/your_username/AppData/Local/JetBrains/Toolbox/apps/PyCharm-P/ch-0/231.9225.15/plugins/python/helpers/generator3/__main__.py -d C:\Users\your_username\AppData\Local\JetBrains\PyCharm2023.1\python_stubs\-525708597 -s D:/ProgramData/conda_env/envs/my_env/DLLs;D:/ProgramData/conda_env/envs/my_env/Lib;D:/ProgramData/conda_env/envs/my_env/Lib/site-packages
2024-07-21 19:16:45,803 [  21221]   INFO - #c.j.p.p.PyPIPackageCache - Loaded 556165 packages from C:\Users\your_username\AppData\Local\JetBrains\PyCharm2023.1\python_packages\pypi-cache.json
2024-07-21 19:16:46,565 [  21983]   INFO - #c.j.p.p.PyCondaPackageService - Using D:\ProgramData\miniconda3\Library\bin\conda.bat as a conda executable for D:\ProgramData\conda_env\envs\my_env\python.exe (specified as a preferred conda path)
2024-07-21 19:16:46,566 [  21984]   WARN - #c.j.p.s.PyVirtualEnvTerminalCustomizer - Can't find D:\ProgramData\miniconda3\Library\bin\conda.bat, will not activate conda
2024-07-21 19:16:46,656 [  22074]   INFO - #o.j.p.t.LocalTerminalDirectRunner - Started com.pty4j.windows.conpty.WinConPtyProcess in 74 ms from [powershell.exe, -ExecutionPolicy, RemoteSigned, -NoExit, -Command, Failed to activate conda environment. 

You can see that there is a corresponding error log near Failed to activate conda environment. You can use this log to know the specific cause of the error.

Southdown answered 21/7 at 13:33 Comment(0)
L
-1

This can happen when you disable activating the (base) environment by default like so:

conda config --set auto_activate_base false 

To resolve the issue, reactivate the feature.

conda config --set auto_activate_base true 
Laband answered 24/2, 2021 at 18:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.