How to ACTUALLY set environment variables in PyCharm?
I am attempting to run a series of tests for a python project in PyCharm and have been running into os.environ[] KeyErrors near non-stop the last few days. Device is a MacBook Pro M2.
I can access the env vars from the built in "Terminal" but booting up python and testing with os.environ, but not when I actually go and run the test or debug. That's when the KeyErrors come up.
Here's what I have done so far:
1. gone to Run>Edit Configurations and added my env vars there
2. gone to Settings>Build,Execution,Deployment>Console>Python Console and added them there too
3. gone to tools>Terminal and done the same.
The current workaround is to add an EnvFile config for every individual test! It seems like Run>Edit Configurations does absolutely nothing unless I specify the exact file_name/python_function. Creating a run config for an entire test folder doesn't seem to do anything.
Here is the content of my config.xml file:
```xml
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Unittests in tests/unit" type="tests" factoryName="Unittests">
<module name="smde_refactor" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
<option name="SDK_HOME" value="$PROJECT_DIR$/venv/bin/python" />
<option name="SDK_NAME" value="Python 3.10 (smde_refactor)" />
<option name="WORKING_DIRECTORY" value="" />
<option name="IS_MODULE_SDK" value="false" />
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<EXTENSION ID="net.ashald.envfile">
<option name="IS_ENABLED" value="true" />
<option name="IS_SUBST" value="false" />
<option name="IS_PATH_MACRO_SUPPORTED" value="false" />
<option name="IS_IGNORE_MISSING_FILES" value="false" />
<option name="IS_ENABLE_EXPERIMENTAL_INTEGRATIONS" value="false" />
<ENTRIES>
<ENTRY IS_ENABLED="true" PARSER="runconfig" IS_EXECUTABLE="false" />
<ENTRY IS_ENABLED="true" PARSER="env" IS_EXECUTABLE="false" PATH="tests/unit/environment.env" />
</ENTRIES>
</EXTENSION>
<option name="_new_pattern" value="""" />
<option name="_new_additionalArguments" value="""" />
<option name="_new_target" value=""$PROJECT_DIR$/tests/unit"" />
<option name="_new_targetType" value=""PATH"" />
<method v="2" />
</configuration>
</component>
```
^I know this is a folder instead of a script -- but even when a specific script is selected there's still no luck picking up env vars with any of the functions inside that script.
If I run a .py script as a whole (after adding env vars to the run config), it succeeds (os.environ picked up!). But if I run any of the tests within that .py script individually, they all fail (could not pick up os.environ).
Thank you!
Please sign in to leave a comment.
The key was to then go in and "Edit configuration templates". Just to make sure, I added the env vars desired to the templates for Python, Python tests/pytest and Python tests/Unittests.
^THIS did not work immediately. I had to go and delete all my existing configs that appear when I first opened Run>Edit Configurations.
Then voila -- all pytests scripts+modules in the folder started picking up on the required env vars.
*on a side note, I think the EnvFile plug-in adds that
line under ENTRIES instead of specifying the keys/values directly in the .xml (not that it worked anyway)