24
votes

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?

9

9 Answers

8
votes

This seems to be a known issue.

17
votes

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

6
votes

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.

4
votes

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.

2
votes

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

1
votes

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.

0
votes

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!)

0
votes

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
-1
votes

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