PyCharm - automatically set environment variables

pycharm, virtualenv

Solution

I was looking for a way to do this today and stumbled across another variation of the same question (linked below) and left my solution there although it seems to be useful for this question as well. They're handling loading the environment variables in the code itself.

Given that this is mainly a problem while in development, I prefer this approach:

Open a terminal

Assuming virtualenvwrapper is being used, activate the virtualenv of the project which will cause the hooks to run and set the environment variables (assuming you're setting them in, say, the postactivate hook)

Launch PyCharm from this command line.

Pycharm will then have access to the environment variables. Likely because of something having to do with the PyCharm process being a child of the shell.

https://stackoverflow.com/a/30374246/4924748

Problem

I'm using virtualenv, virtualenvwrapper and PyCharm. I have a postactivate script that runs an "export" command to apply the environment variables needed for each project, so when I run "workon X", the variables are ready for me. However, when working with PyCharm I can't seem to get it to use those variables by running the postactivate file (in the "before launch" setting). I have to manually enter each environment variable in the Run/Debug configuration window. Is there any way to automatically set environment variables within PyCharm? Or do I have to do this manually for every new project and variable change?

Original source

Related problems