0

I've read a few how-tos but still struggling with this. In my app I was previously just pulling in my secret keys from a .py file that was in my folder system (which is not best practice). Instead I'd like to use the os module to get my secret keys in my dev environment (and do the same in heroku eventually), similar to the code below:

import os    
OAUTH_TOKEN = os.getenv('OAUTH_TOKEN')

I've read that I need to add my actual OAUTH_TOKEN to the .profile file for this to work.

OAUTH_TOKEN = 'mysecretkey'

However, when I got to run the first snippet of code above and attempt to print the OAUTH_TOKEN variable I get nothing in return. I also added my key to the .bashsrc file as well as some other websites said to do this.

What is going on and why can't I retrieve my secret key this way? What am I missing?

4
  • I've never used the Procfile for environment variables. This is how I do it: devcenter.heroku.com/articles/config-vars#set-a-config-var and it would be accessed in the way you've specified. Commented Apr 28, 2021 at 5:57
  • Thanks for the response. I've read that documentation and I think that is for the environment variables when the app is launched on heroku. What about when I am developing/testing it on my machine? Commented Apr 28, 2021 at 6:16
  • 1
    Oh, you can add them to your .bashrc file like this: export VARNAME="my value". Commented Apr 28, 2021 at 7:29
  • Thank you, it turns out that I didn't have "export" at the front and also had spaces around "=". Very helpful. Commented Apr 29, 2021 at 5:22

1 Answer 1

1

To retrieve your secret keys while working on a Python-Flask app, you will need to install python-dotenv. This package will help with loading of secret or even runtime environment variables.

(venv)$ pip3 install python-dotenv

In your secret keys file, say, .env, initialize your secret environment variables:

# .env

OAUTH_TOKEN='<your-secret-oauth-token>'

To load OAUTH_TOKEN, use load-dotenv from dotenv:

# config.py

import os
from dotenv import load_dotenv

load_dotenv('.env')

class Config(object):
    OAUTH_TOKEN = os.getenv('OAUTH_TOKEN') 
    # or OAUTH_TOKEN = os.environ.get('OAUTH_TOKEN')

In your terminal, try to retrieve the value of OAUTH_TOKEN as follows. I am using flask shell command rather than python3:

# start your python interpreter in the context of your application
(venv)$ flask shell

>>> from app import Config
>>> print(app.config['OAUTH_TOKEN']))

# Your token should be printed here

The assumption above is that in your application instance, say __init__.py file, you have imported Config.

# __init__.py

from flask import Flask
from config import Config

app = Flask(__name__)
app.config.from_object(Config)

# ...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.