Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
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?
Go to
File -> Settings -> Tools -> Terminal
.
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:
–
–
–
–
–
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.
Windows & Powershell
I used partial answers from the answers here and this answer to come up with the following solution on Windows with Powershell
Go to File > Settings > Terminal
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
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 "
–
Experienced the issue on Windows & Pycharm 2022.1 and conda environment
My solution is to change default Powershell to cmd shell
Open Settings > Tools > Terminal
Change Shell path from "powershell.exe" to "cmd.exe"
Make sure the "Activate virtualenv" checkbox is checked.
New terminal correctly opens with configured environment.
–
For powershell (I recommend this):
powershell.exe -ExecutionPolicy ByPass -NoExit -Command "& 'C:\tools\miniconda3\shell\condabin\conda-hook.ps1'"
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
–
–
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.
–
–
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
–
–
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!
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.
Mixing a few answers from here, I figured out a solution for Git Bash in Windows:
Go to File -> Settings -> Tools -> Terminal.
Replace the value in "Shell path" with
"C:\Program Files\Git\bin\bash.exe" --login && conda activate myenv
Solved for windows users based on the linux solution.
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%
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)
On pycharm goto Tools->Terminal->shell and enter
cmd.exe "/K" ./env_win10.bat
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%
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!)
I ran into the same problem and used this solution.
Go to File -> Settings -> Tools -> Terminal.
Replace the value in Shell path with
powershell.exe -ExecutionPolicy ByPass -NoExit -Command "& conda activate <yourcondaenvname>
–
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
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.