PyCharm terminal doesn't activate conda environment

旧城冷巷雨未停 提交于 2019-11-27 21:25:50

This seems to be a known issue.

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:

Muhammad Hannan

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.

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!