How to set "default" environment variables?

I have a few environment variables set in my "run configuration", but when I right-click a single test and click in "Run test" the new "run configuration" is created with empty environment variables, making my test fail.

Is there a way to set some default environment variables that will be automatically set in every new "run configuration"?
23
21 comments
Avatar
Permanently deleted user

I have the same question.  This is very frustrating.  How can I set some default variables for each test to "inherit"?

Thank you!

4

Please, go to `Run| Edit Configurations| Defaults| Python tests| Unitests/Nosetests/...` and specify your environment variables in "Environment variables" field, then apply changes. After that all newly created run configurations for tests will follow this setting.

19
Avatar
Permanently deleted user

Thank you!  That worked perfectly!

1

I tried using this with IntelliJ  2017.2 with a node config and also with an npm config but I don't see any Env tab. Please help?

0

@Claire

It's not a tab, there is a field "Environment variables" (for npm there is "Environment" field).

0

Sorry, I'm having a similar problem in Pycharm-2018.3. I selected "Run|Edit Configurations" but there is no "Defaults" button, tab or field to select. I could add a variable using the "Environment Variables" field in that screen, but I don't want to do that for each of the tests in the module. I'd like to be able to set the variable once for the module,  and have all tests in that module use that variable. I tried setting the variable in a configuration for the module itself, but running individual tests in that module did not inherit it.

2

Hi Mark,

As mentioned above (https://intellij-support.jetbrains.com/hc/en-us/community/posts/205820189/comments/115000136150) you can do that for a template of needed run configuration. Thus, all new run configurations will have these environment variables. As about existing run configurations - you should update them all manually.

0
Avatar
Permanently deleted user

This works for a given type (e.g. Python, Java, etc) of run config, and applies across all projects.  It'd be really useful to also have default environment variables in a project config, that any type of new run config in that project would use/inherit.  Such that I could say "AWS_PROFILE=foo" for my project foo, and any new Java, Bash, Node, whatever run config I create would default to using it.  "inheritance" would probably be better such that I could change it in my project and have any subsequent runs, use that new value

2

Please feel free to submit a feature request about it to our issue tracker using the link https://youtrack.jetbrains.com/issues/PY

0

I also could not find `Run| Edit Configurations| Defaults| Python tests| Unitests/Nosetests/...`. I could find 'Run| Edit Configurations' fine, but could not find the 'Defaults' tab after that. What am I missing?

 

Edit: Seems to be working for me now. Odd. For what it's worth, I edited the templates section of the 'Run| Edit Configurations' menu to hold my environment variables for pytest, so I think that solved it.

1

In newer versions of pycharm the "Default" Run Configuration tab mentioned is actually called "Templates".  See below 

 

6

Oh, defaults are now called templates.  Thanks @Hughesadam87

0
Avatar
Permanently deleted user

Thanks for this clarification. I had to put my environment variable into the "Python" template to right click and run a test in Pycharm CE. Very convenient to put variable into master Python template.

0

I have a template with a environment variable set. If I create a new test config it does inherit the variable. But if I use the green "play" arrow to run a single test this variable is not inherited. That seems like incorrect behaviour. 

1

Dr Nick Bailey

Works correctly for me. I would suggest to submit an issue to https://youtrack.jetbrains.com with detailed steps to reproduce it and code/project sample.

0

Dr Nick Bailey

 

After setting environment variables on the default configuration template, you may need to first remove any existing ones before clicking on the green "play" button (the existing ones can be removed by going to Run > Edit Configrations > Selecting the one to remove > clicking on the "-" sign).

 

I think this is because when you click on the play button, it creates a temporary configuration (and therefore clicking it again still uses previous settings)

 

Doing this worked for me, and clicking the play button started respecting the environment variable

1

Dr Nick Bailey

I had the same issue, and it appears that when the default test runner is set to autodetect, template env vars are not used. See https://youtrack.jetbrains.com/issue/PY-50334. The workaround is simply to set your test runner to something else than autodetect in Settings/Python Integrated Tools. 

2

Laundro Your suggested workaround did the trick - thank you!!

0

Laundro's workaround did not work for me, unittest configurations in Windows have this issue of clearing out pythonpath. In linux our test configurations inherit pythonpath from pycharms launch environment. However, we are using pycharm 2020.2.3 in Linux and pycharm 2022.2 in Windows, which probably explains the difference in behaviour. How can we get unittests to inherit the launch environment in 2022.2? Creating templates works, but it hard-codes the environment to the specific test whereas we have our own in-house environment management system that is what pycharm gets launched in.

0

If you have “Test runner” type set to Autodetect, you need to update your test runner “Autodetect” template.

 

 

0

Please sign in to leave a comment.