# Using doppler with Pycharm

**URL:** https://community.doppler.com/t/using-doppler-with-pycharm/47
**Category:** Need Help
**Created:** [January 6, 2021, 5:55am UTC](https://community.doppler.com/t/using-doppler-with-pycharm/47 "2021-01-06T05:55:46Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![ankit](https://sea2.discourse-cdn.com/flex016/user_avatar/community.doppler.com/ankit/32/36_2.png) [@ankit](https://community.doppler.com/u/ankit)
#### Post date: [January 6, 2021, 5:55am UTC](https://community.doppler.com/t/using-doppler-with-pycharm/47/1 "2021-01-06T05:55:46Z")

</div>

Hi everyone,

We are trying to use Pycharm as our IDE, and it’s hard to run some of our python scripts because they need environment variables. There is a way to set environment variables in Pycharm, but of course if we copy paste them, it kinda defeats the purpose of using Doppler (we have to remember to update them).

In Pycharm, there is also a way to run a shell script that runs before any python script is run. I suspect that’s the way to go, but my n00by shell skills are woefully inept.

Any thoughts? Thanks!

---

<div class="post-metadata">

### Author: ![ryan-blunden](https://sea2.discourse-cdn.com/flex016/user_avatar/community.doppler.com/ryan-blunden/32/9_2.png) [@ryan-blunden](https://community.doppler.com/u/ryan-blunden)
#### Post date: [January 6, 2021, 6:45am UTC](https://community.doppler.com/t/using-doppler-with-pycharm/47/2 "2021-01-06T06:45:45Z")

</div>

Hey Ankit,

Thanks for stopping by and I’ll take a look at how Doppler and PyCharm can work together for you.

---

<div class="post-metadata">

### Author: ![ryan-blunden](https://sea2.discourse-cdn.com/flex016/user_avatar/community.doppler.com/ryan-blunden/32/9_2.png) [@ryan-blunden](https://community.doppler.com/u/ryan-blunden)
#### Post date: [January 6, 2021, 8:59am UTC](https://community.doppler.com/t/using-doppler-with-pycharm/47/3 "2021-01-06T08:59:05Z")

</div>

Hi Ankit,

Sorry, but I wasn’t able to find a nice solution like running a script prior to the Python command to populate the environment variables.

Here is the quickest flow I could come up with for populating the env vars using copy and paste which while still not ideal, is relatively fast.

 ![PyCharm Paste Env Vars](https://us1.discourse-cdn.com/flex016/uploads/doppler/original/1X/fa58cb857e01ba534d405b23662c9cae83940d2b.gif)

I did experiment with using Python’s subprocess library to effectively run `doppler run -- python3 app.py` but I could not get breakpoints to work so its usefulness is limited. The code is below in case this gives you new ideas.

```
import subprocess
import sys
from signal import signal, SIGINT

app = subprocess.Popen(['doppler', 'run', '--', 'python', 'app.py'], stdout=subprocess.PIPE)

def signal_handler(sig, frame):
    app.terminate()
    sys.exit(0)

signal(SIGINT, signal_handler)

while app.poll() is None:
    line = app.stdout.readline().decode('utf-8')
    sys.stdout.write(line)
```

---

<div class="post-metadata">

### Author: ![ankit](https://sea2.discourse-cdn.com/flex016/user_avatar/community.doppler.com/ankit/32/36_2.png) [@ankit](https://community.doppler.com/u/ankit)
#### Post date: [January 6, 2021, 3:35pm UTC](https://community.doppler.com/t/using-doppler-with-pycharm/47/4 "2021-01-06T15:35:54Z")

</div>

Hey Ryan,

Awesome, thanks! This is super helpful. My way of copy pasting was way clunkier, so this will definitely be a step up.

Speaking of, is there any shell script that can load the doppler secrets into the environment? Where you said, “[you weren’t] able to find a nice solution like running a script prior to the Python command to populate the environment variables”, I wasn’t sure if that meant that it’s not possible using Doppler, or you couldn’t find a way to run a pre-execution shell script in Pycharm. If latter, see screenshot; under “Before Launch” in configuration, one can select “External tool”, which can be a shell script.

Not sure if that’s useful.

 ![Screen Shot 2021-01-06 at 7.32.21 AM.jpg](https://us1.discourse-cdn.com/flex016/uploads/doppler/original/1X/3a8b89c0bd025589b7b48181613b4df5348856b6.jpeg)

---

<div class="post-metadata">

### Author: ![ryan-blunden](https://sea2.discourse-cdn.com/flex016/user_avatar/community.doppler.com/ryan-blunden/32/9_2.png) [@ryan-blunden](https://community.doppler.com/u/ryan-blunden)
#### Post date: [January 7, 2021, 12:46am UTC](https://community.doppler.com/t/using-doppler-with-pycharm/47/5 "2021-01-07T00:46:56Z")

</div>

Hey Ankit,

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 `HOSTNAME` and `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:

```python
from os import environ as env, popen
import json

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__ )

@app.route('/')
def hello():
    return 'Flask using Doppler secrets'

if __name__ == ' __main__':
    app.run(host=env['HOSTNAME'], port=env['PORT'])

```

## 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.

 ![Screen Shot 2021-01-07 at 10.45.27 am](https://us1.discourse-cdn.com/flex016/uploads/doppler/original/1X/753c762eba56278845d6e177f64cff5318c5af46.jpeg)

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!

---

<div class="post-metadata">

### Author: ![ankit](https://sea2.discourse-cdn.com/flex016/user_avatar/community.doppler.com/ankit/32/36_2.png) [@ankit](https://community.doppler.com/u/ankit)
#### Post date: [January 7, 2021, 4:46am UTC](https://community.doppler.com/t/using-doppler-with-pycharm/47/6 "2021-01-07T04:46:17Z")

</div>

This is GOLD! Thanks so much Ryan; you’re the best!

---

<div class="post-metadata">

### Author: ![ryan-blunden](https://sea2.discourse-cdn.com/flex016/user_avatar/community.doppler.com/ryan-blunden/32/9_2.png) [@ryan-blunden](https://community.doppler.com/u/ryan-blunden)
#### Post date: [May 5, 2021, 5:34am UTC](https://community.doppler.com/t/using-doppler-with-pycharm/47/7 "2021-05-05T05:34:51Z")

</div>

Hi @ankit,

I’m pleased to announce that we’ve released a [doppler-env](https://pypi.org/project/doppler-env/) Python package to make it even easier to integrate Doppler into both PyCharm and Visual Studio code.

To use:

1. Install `doppler-env` in your local development environment:

```auto
pip install doppler-env

```

1. Define the `DOPPLER_ENV` environment variable in your IDE, editor, or terminal:

```auto
export DOPPLER_ENV=1

```

1. Run or debug your application in your IDE, editor, or terminal:

```auto
python src/app.py

```

---

<div class="post-metadata">

### Author: ![ankit](https://sea2.discourse-cdn.com/flex016/user_avatar/community.doppler.com/ankit/32/36_2.png) [@ankit](https://community.doppler.com/u/ankit)
#### Post date: [May 7, 2021, 1:24am UTC](https://community.doppler.com/t/using-doppler-with-pycharm/47/8 "2021-05-07T01:24:11Z")

</div>

Thanks Ryan,

This sounds awesome! Is there a typo in the package name?

 ![Screen Shot 2021-05-06 at 6.21.28 PM.png](https://us1.discourse-cdn.com/flex016/uploads/doppler/original/1X/ade0bacb6b51618fc7aa150eb4dcae9065720404.png)

Thanks,

---

<div class="post-metadata">

### Author: ![ryan-blunden](https://sea2.discourse-cdn.com/flex016/user_avatar/community.doppler.com/ryan-blunden/32/9_2.png) [@ryan-blunden](https://community.doppler.com/u/ryan-blunden)
#### Post date: [May 7, 2021, 1:41am UTC](https://community.doppler.com/t/using-doppler-with-pycharm/47/9 "2021-05-07T01:41:28Z")

</div>

It’s probably because I set the Python version to 3.8 or higher.

What version are you using?

---

<div class="post-metadata">

### Author: ![ankit](https://sea2.discourse-cdn.com/flex016/user_avatar/community.doppler.com/ankit/32/36_2.png) [@ankit](https://community.doppler.com/u/ankit)
#### Post date: [May 7, 2021, 1:53am UTC](https://community.doppler.com/t/using-doppler-with-pycharm/47/10 "2021-05-07T01:53:07Z")

</div>

Ah gotcha. We are on 3.6.8

---

<div class="post-metadata">

### Author: ![ryan-blunden](https://sea2.discourse-cdn.com/flex016/user_avatar/community.doppler.com/ryan-blunden/32/9_2.png) [@ryan-blunden](https://community.doppler.com/u/ryan-blunden)
#### Post date: [May 7, 2021, 2:01am UTC](https://community.doppler.com/t/using-doppler-with-pycharm/47/11 "2021-05-07T02:01:38Z")

</div>

Ok, I’ll have it support that version and will let you know once PyPi has the latest version.

---

<div class="post-metadata">

### Author: ![ryan-blunden](https://sea2.discourse-cdn.com/flex016/user_avatar/community.doppler.com/ryan-blunden/32/9_2.png) [@ryan-blunden](https://community.doppler.com/u/ryan-blunden)
#### Post date: [May 7, 2021, 3:34am UTC](https://community.doppler.com/t/using-doppler-with-pycharm/47/12 "2021-05-07T03:34:17Z")

</div>

Hey @ankit! Version 0.2.2 has been released which adds Python 3.6 support.
