98

Conda env is activated using source activate env_name.

How can I activate the environment in pycharm ?

Abhinav Rai
  • 1,027
  • 1
  • 9
  • 9

11 Answers11

63

open

pycharm/preferences/project/Project Interpreter

And check existing interpreter. Conda environments may already be listed there.

enter image description here

If not exists, you can create a new conda environment with "Create Conda Env" button

enter image description here

If you are looking for a specific conda environment you can use 'add local'. When you click 'add local' you will input conda environment path + /bin/python

You can list all conda environment in your system with following commnad.

>>conda info --env
# conda environments:
#
tensorflow            *  /Users/username/miniconda3/envs/tensorflow

you can chose the approach best fits your needs.

clockworks
  • 3,755
  • 5
  • 37
  • 46
  • 1
    Is there any difference between using Pycharm's "Create Conda Env" vs creating a new conda environment in terminal and then accessing it via Pycharm's "Add Local"? – DataMan Jun 10 '18 at 17:08
  • 1
    You can easily test. Create a conda env via pycharm and then oepn terminal and list all environments, if it is listed there, it means there are no difference. you can list conda env with following command on terminal: conda info --envs – clockworks Jun 10 '18 at 21:04
  • 1
    i dont have that toggle. only add local and add remove. – softmarshmallow Jul 16 '18 at 14:22
  • 2
    `Ctrl+Alt+S` to show the "project preferences" or "project settings" as I cannot find a fricking menu button – xdola Apr 07 '20 at 22:02
44

The best PyCharm specific answer is this one by wasabi (below).

In general though, if you want to use an interpreter from within a Conda environment then you can change the location of the interpreter to point to the particular environment that you want to use e.g. /home/username/miniconda/envs/bunnies as mentioned in this comment.

However, as mentioned in this answer by Mark Turner, it is possible to have a shell script executed when activating an environment. This method will not run that shell script, but you can follow his workaround if you need that shell script run:

  • open a conda prompt
  • activate the environment
  • run pycharm from the conda prompt
Ari Cooper-Davis
  • 3,374
  • 3
  • 26
  • 43
  • 5
    It is possible for a conda package to ship a shell script that runs on environment activation. In that case, activating an environment does more than changing the PATH—it also runs such scripts. So in some cases there could be PyCharm-specific issues. In practice in my (limited) experience, however, such scripts are rare. – ctrueden Feb 15 '18 at 23:00
  • 2
    This is not entirely correct. As per above comment, conda envs can also have activation (and deactivation) scripts installed by packages in `[your_env]/etc/conda/activate.d` (and `deactivate.d`). PyCharm doesn't run these, but `activate`/`source activate` does and not running these scripts can (and does) lead to unexpected behaviour in PyCharm. – user2856 Dec 05 '18 at 22:21
  • 2
    @user2856 completely agree. Activate does much more than change PATH. It can point to other batch scripts which set all kinds of environment variables beyond path. I would argue that this answer is wrong. – user32882 May 29 '19 at 08:04
  • @user32882 You're absolutely right, I lifted that quote verbatim and was wrong to do so! I'll edit my answer. – Ari Cooper-Davis May 29 '19 at 10:04
12

How about environment.yml

Pycharm can create a new conda environment indeed. Unfortunately, until this issue is fixed, it won't offer environment.yml support, which means it won't install the dependencies declared there.

When working on a project based on such a file, you need to create / update the dedicated env manually on your machine:

conda env create -n <my-project>

Then remember to update each time environment.yml changes (from you or upstream).

conda env update -n <my-project>

Not ideal

Arnaud P
  • 12,022
  • 7
  • 56
  • 67
12

As mentioned in one of the comments above, activating an environment can run scripts that perform other actions such as setting environment variables. I have worked in one environment that did this. What worked in this scenario was to:

  • open a conda prompt
  • activate the environment
  • run pycharm from the conda prompt

Pycharm then had access to the environment variables that were set by activating the environment.

Mark Turner
  • 161
  • 1
  • 4
  • This works,. Thank you! But surely there is a way to just open the PyCharm app and use the environment specified in the settings? – rocksNwaves May 04 '22 at 14:43
5

I had the same problem i am on windows 10 professional 64 bit my solution was to start Pycharm as adminstrator and it worked

TarekB
  • 757
  • 14
  • 20
5
  1. Go to settings at the top right corner of the PyCharm IDE.
  2. Go to Project:{Your Project Name}->Python Interpreter
  3. Go to the settings inside here and click add:

enter image description here

  1. In Add Python Interpreter select conda env
  2. Select existing environment and click on your required conda environment path from the dropdown menu OR add the path of the python.exe file in your conda environment. As a reference, I am adding the path for my windows10 system: C:\Users\maria\AppData\Local\Continuum\anaconda3\envs<mycondaenv>\python.exe It can vary for your system based on installation configs.
Maria
  • 171
  • 1
  • 4
  • I have selected the conda environment, but when i click on new terminal ,it still says ``` Failed to activate conda environment. Please open Anaconda prompt, and run `C:\Users\HP\anaconda3\condabin\conda.bat init powershell` there. ``` how do i activate conda environment on terminal? – Rohan Devaki Mar 01 '23 at 05:00
4

It seems important to me to know, that setting project interpreter as described in wasabi's comment does not actually activate the conda environment.

I had issue with running xgboost (that I installed with conda) inside PyCharm and it turned out that it also need some folders added to PATH. In the end I had to make do with an ugly workaround:

  1. Find out what are the additional folders in PATH for given environment (with echo %PATH% in cmd)

  2. In the file I wish to run put to the top before anything else:

import os os.environ["PATH"] += os.pathsep + os.pathsep.join(my_extra_folders_list)

I know this is not at all proper solution, but i was unable to find any other beside what Mark Turner mentioned in his comment.

jiripi
  • 656
  • 1
  • 5
  • 15
3

To use Conda environment as PyCharm interpreter

  1. activate Conda environment from Conda navigator
  2. open PyCharm from the navigator tool list
  3. in Conda Add interpreter section choose existing Conda environment and it automatically recognises the path of that environment's python.exe file
Gunjan Paul
  • 511
  • 6
  • 12
  • 1
    @user11675463 this worked, but a quicker way is to click the "Terminal" button at the bottom of Pycharm, activate your conda environment, and move on. – brethvoice May 14 '21 at 20:24
  • 1
    The good thing about Pycharm is that you can update conda environments from within it. So if you try to run code in an environment that does not allow it due to a lack of modules/libraries, you can install them with one mouse click. – brethvoice May 14 '21 at 20:25
  • 1
    Also make sure to *not* try and specify the conda executable first. Just browse for the Python interpreter as described here and PyCharm will fill in the other needed fields: https://docs.anaconda.com/anaconda/user-guide/tasks/integration/python-path/ – brethvoice Aug 06 '21 at 20:33
2
  • First , select Interpreter setting ... in right bottom of Pycharm.

enter image description here

  • Then choose python.exe from your desired conda environment. My environment path is : C:\Users\javadsh\anaconda3\envs\tf-gpu\python.exe

enter image description here

Javad Shirkhani
  • 343
  • 6
  • 11
2

In PyCharm 2022.3.3 (at least the version i use now) you can go to add your interpreter, select Conda Environment and there you have to select the conda.exe, not the python.exe.

Afterwards you can load the defined environments and select one from the list.

enter image description here

LBi
  • 49
  • 1
  • 6
0

Go to Pycharm -> Preferences -> Project Interpreter. At the top left of the packages table there is a plus sign, minus sign, a green circle and an eye; uncheck the green sign; that will let you have access to the packages while using conda environment.