0

Well I thought I had set up my conda environment in Pycharm just like their docs require:

  • Settings > Project Interpreter > Add
  • Select Conda Environment
  • Check existing environment
  • Browse for interpreter path (in this case C:\ProgramData\Anaconda2\envs\wps_env36\python.exe)
  • Click OK

I did notice, however that the environment was not automatically called in the 'Terminal' tab in PyCharm. So I followed this post which seems to adequately set it up for the terminal (although in my opinion that should automatically be taken care of when I select the environment above).

Now the confusing part: I tried to run the following in 3 different places in pycharm. These are 1) regular script, 2) Python Console, and 3) Terminal (by doing python and forcing it into python interpreter mode`:

import os
print(os.environ['CONDA_PREFIX'])

For cases 1) and 2) I get the following error:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:\ProgramData\Anaconda2\envs\wps_env36\lib\os.py", line 669, in __getitem__
    raise KeyError(key) from None
KeyError: 'CONDA_PREFIX'

For case 3) it runs fine and outputs the path for the conda environment:

C:\ProgramData\Anaconda2\envs\wps_env36

This indicates to me that although the right python.exe is used to run the interpreter, the necessary environment variables are not transferred automatically. I need these variables to do additional things, and I think the above explains why I have to add the following environment variables to my build configurations:

GDAL_DATA PROJ_LIB

In the regular anaconda prompt/pycharm terminal these are already defined (as they originate from the batch scripts in C:\ProgramData\Anaconda2\etc\conda\activate.d, but they are not defined in my main anaconda python interpreter.

How do I make sure the entire conda environment is defined in Pycharm as my main interpreter. I am talking complete with environment variables and any other shenanigans I am not aware of? The docs only indicate how you can set conda as your interpreter, and this is not enough in my case.

user32882
  • 5,094
  • 5
  • 43
  • 82

1 Answers1

0

Hopefully you sorted it out in the last five months but in case anyone else is stuck...

This answer explains how to get the terminal to activate a conda environment by passing the activate command into cmd.exe when the terminal starts:

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.

Pycharm terminal config

The only reliable way I've found to make the console work correctly is to launch PyCharm from the activated environment. From a windows command prompt:

activate my-env-name && pycharm64.exe & 
#the trailing '&' causes pycharm to run in the background rather than freezing the terminal

Or from git-bash:

source activate my-env-name && pycharm64.exe &

Where my-env-name is the name of your conda environment.

Note that if you installed the 32 bit version you need to swap pycharm64.exe for pycharm.exe

You can make this a bit more repeatable by creating an "edit.sh" file in your project:

#!/bin/sh
source activate my-env-name && pycharm64.exe &

And then running that script when you want to run your project cd my-project && bash edit.sh

Community
  • 1
  • 1
  • Links to answers break, which in this case would make your answer of little value. Can you include the relevant info in your answer? Thanks – hat Mar 04 '19 at 15:37
  • @hat - sorry - thought that might be seen as plagiarism. Updated now! – harryrobbins Mar 04 '19 at 18:08
  • So long as you give attribution, it is fine. SO answers are under CC BY-SA 4.0 – hat Mar 05 '19 at 07:19