0

I am creating a Python app and hosting it on Heroku. Recently I have been playing around with Heroku's Config Vars so that I can keep my secret keys away from prying eyes. So I put my secret key in the Heroku config vars:

Heroku Config Vars

And then in my Python code, I access the secret var using something like this:

print("This is my secret key: " + str(os.environ.get("secret_key")))

And when hosted on Heroku, that works great!

>>>This is my secret key: 1234

But I would also like to be able to run this code local in PyCharm. Typically I would just do something like:

secret_key = sys.argv[1]

And set the secret key in the script parameters of PyCharm. But that will not run on Heroku. Is there something that I can put in the script parameters to make this work? Something like this? (tried, does not work...)

Pycharm Script Parameters

Or is there some other way to go about this?

halfer
  • 19,824
  • 17
  • 99
  • 186
Brian L
  • 549
  • 7
  • 21

1 Answers1

4

Why would you use different code in production (respecting environment variables) vs. locally (requiring settings to be passed in as CLI arguments)? Just use environment variables everywhere. You can set them in PyCharm, or you could create an untracked, ignored .env file containing your environment variables:

secret_key=1234

And run your code using a tool that populates environment variables from such a file, e.g. heroku local or pipenv run. The .env flow is officially endorsed by Heroku.

Dodge
  • 3,219
  • 3
  • 19
  • 38
ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257