Can't get Python to Read Environment Variables

31 Views Asked by At

Python returns "None" when I try to print variable which pulls value from variables in .Env file.

This is what my base.py file looks like.

from flask import Flask, redirect, url_for, request, jsonify, make_response, render_template
import datetime
import pandas as pd
from dotenv import load_dotenv

load_dotenv()

app = Flask(__name__)


# A bunch of other code is here.

print(api_key_id)

# A bunch of other code is here.


if __name__ == "__main__":
   api_key_id = os.getenv('APCA_API_KEY_ID')
   api_secret = os.getenv('APCA_API_SECRET_KEY')
   base_url = "https://paper-api.alpaca.markets"
   app.run(host="127.0.0.1", port=8080, debug=True)```

I expected the print function to return a string of the APCA_API_KEY_ID from my .env file. Instead, I received "None" from Python.

0

There are 0 best solutions below