So I dug further into configuring Doppler as an external tool via an external script but this doesn’t work because Pycharm executes the script using
/bin/bash meaning it is executed in a subshell and therefore, any variables exported there are not visible to the subsequent Python script.
I’ve got two more ideas for you though.
1. Manually populate env vars during development
The example here I’ll use is a Flask app that requires a
PORT vars from Doppler.
You could check to see if you’re running in a development environment, e.g.
os.environ['FLASK_ENV'] == 'development', then you only need to only set the
FLASK_ENV environment variable in the PyCharm configuration for your app.
An entire script could look like this:
from os import environ as env, popen
from flask import Flask
# Load env vars with Doppler if not already populated by checking if `DOPPLER_PROJECT` env var exists
if env.get('FLASK_ENV') == 'development' and not env.get('DOPPLER_PROJECT'):
print('[info]: Populating secrets into env vars with Doppler')
doppler_vars = json.loads(popen('doppler secrets download --no-file --format json').read())
for key, value in doppler_vars.items():
env[key] = value
app = Flask(__name__)
return 'Flask using Doppler secrets'
if __name__ == '__main__':
2. Use the patch-env Python package as a development dependency
If you didn’t want to leak Doppler implementation details into your app code, another option is to use the
patch-env Python library.
You simply add a
PATCH_ENV_COMMAND env var in PyCharm whose value is the command to return a list of environment variables that the library will use to populate
os.environ prior to Python running your code.
It does this using a Python site-specific configuration hook.
Sorry we don’t have a turnkey solution for you but at least you’ve got a couple of additional options to try.
Let me know how you go!