access env variable in flask

  1. Import the os module in your Flask application.
import os
  1. Access the environment variable using the os.environ dictionary.
my_variable = os.environ.get('MY_VARIABLE_NAME')

Replace 'MY_VARIABLE_NAME' with the name of your environment variable.

  1. Optionally, you can provide a default value in case the environment variable is not set.
my_variable = os.environ.get('MY_VARIABLE_NAME', 'default_value')

Replace 'default_value' with the default value you want to use.

  1. Use the obtained variable (my_variable) in your Flask application as needed.
print(f'My variable value: {my_variable}')

Replace the print statement with the specific code where you intend to use the environment variable.

Note: Ensure that the environment variable is set in your environment or configuration files before running the Flask application. You can set an environment variable using the terminal or in your development environment settings.