Pycharm Plugin: Set Environment Variables on current RunConfig



I'm writing a Pycharm plugin. Basically what I'm trying to do is setting an environment variable on the selected PyTest run configuration. The configuration runs, but I'm not able to set the evars.

Code I came up with:

RunManager runManager = RunManager.getInstance(project);
RunnerAndConfigurationSettingsImpl currRunConfig = (RunnerAndConfigurationSettingsImpl) runManager.getSelectedConfiguration();
RunnerAndConfigurationSettings runConfig = currRunConfig.clone();
PyUniversalPyTestConfiguration prc = (PyUniversalPyTestConfiguration) runConfig.getConfiguration();

Map<String, String> envs = prc.getEnvs();
envs.put("FOOBAR", "True");

ProgramRunnerUtil.executeConfiguration(project, runConfig, DefaultRunExecutor.getRunExecutorInstance());

I'm not sure what's the correct way to to set the new Configuration in the RunnerAndConfigurationSettings.
Maybe somebody can help?

Best regards

Official comment


You can create new configuration with something like that (Kotlin example).

It is valid for current master and will be valid in 2018 probably, but we do not offer official stable API for that unfortunately, so make sure to test your code against future versions

fun createPyTestWithEnv(module: Module) {
val conf = PyRunConfigurationFactory.getInstance().createRunConfiguration(module, PyTestFactory)
(conf.configuration as PyTestConfiguration).envs["SPAM"] = "eggs"


There is no public API to modify env variables in run configuration. Besides it ``PyUniversalPyTestConfiguration`` is removed in 2018.

Please describe what are you trying to achieve in general and I will try to suggest you better approach. 



Thanks for your quick reply.

Actually it's very simple. I'm wrapping calls to our functions in our PyTests in a decorator. In this decorator I'm checking some flags (my idea was environment variables). My Plugin creates other run option besides Run and Debug (Statistical Profiling / Deterministic Profiling / etc...). Depending on which option was chosen flags should get set accordingly. So all I want to do is to control what should happen in this decorator.

An alternative that's working is to set boolean variables in my decorator, search for that assignments in my actionPerformed() method and replace them. However, I don't like the fact that this change is pushed onto the Undo-stack, and I couldn't find a way to remove them from the stack. Maybe you can help me with that?

Best regards


Please sign in to leave a comment.