Use Conda environment in pycharm

99,855

Solution 1

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

Solution 2

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.

Solution 3

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.

Solution 4

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

Solution 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

Share:
99,855

Related videos on Youtube

Abhinav Rai
Author by

Abhinav Rai

Updated on July 08, 2022

Comments

  • Abhinav Rai
    Abhinav Rai almost 2 years

    Conda env is activated using source activate env_name.

    How can I activate the environment in pycharm ?

  • ctrueden
    ctrueden about 6 years
    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.
  • DataMan
    DataMan almost 6 years
    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"?
  • clockworks
    clockworks almost 6 years
    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
  • softmarshmallow
    softmarshmallow almost 6 years
    i dont have that toggle. only add local and add remove.
  • user2856
    user2856 over 5 years
    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.
  • user32882
    user32882 almost 5 years
    @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.
  • Ari Cooper-Davis
    Ari Cooper-Davis almost 5 years
    @user32882 You're absolutely right, I lifted that quote verbatim and was wrong to do so! I'll edit my answer.
  • xdola
    xdola about 4 years
    Ctrl+Alt+S to show the "project preferences" or "project settings" as I cannot find a fricking menu button
  • brethvoice
    brethvoice almost 3 years
    @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
    brethvoice almost 3 years
    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
    brethvoice over 2 years
    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: docs.anaconda.com/anaconda/user-guide/tasks/integration/…
  • rocksNwaves
    rocksNwaves almost 2 years
    This works,. Thank you! But surely there is a way to just open the PyCharm app and use the environment specified in the settings?