How to see all environment variable in flask - python

How can I see all the environment variables in my flask application because when I was following a tutorial, instructor used "export FLASK_APP = app.py" command and then he went on to set other environment variables value. So, how can I see my all of the environment variables and their values ?

To see wich env variables are set on your OS you can use the commands below,
env vars are variables on your OS and not a Flask specific vars, if the set with example with export.
Also there a .env files that are loaded or sometimes used by application like node where on .env files configuration specifc data is stored like keys, or urls.
on Linux
printenv
on windows (powershell)
Get-ChildItem Env:
Wikipedia
an-introduction-to-environment-variables

Related

How are local environment variables evaluated by Azure Functions with Python runtime?

I understand that...
import os
foo = os.getenv('ENV_VAR_NAME')
... will pull "environment" variables from the local.settings.json file when running an Azure Function locally. This is similar to using a .env file.
I know that when deployed to Azure, environment variables are pulled from the Function App's App Settings.
Question:
When running the Function locally, if I have an environment variable set using my terminal (Ex: set DEBUG=true), and this variable is also included in the local.settings.json file (Ex: "DEBUG": false), how does the Function code know which env var to pull in?

Flask_APP environment variable not found

I'm a beginner to Flask and I ran into some errors. about how I did not provide the Flask_APP environment variable:
Error: Could not locate a Flask application. You did not provide the "FLASK_APP" environment variable, and a "wsgi.py" or "app.py" module was not found in the current directory.
I have this error with flask when I try and run my flask code. My code was working at first but then when I tried running it again it had this problem. I searched everywhere for help but I could only find ones about powershell. I'm currently using Ubuntu. Thank you!
In order to run a flask app using the flask command in the terminal, you need to set an environment variable in that terminal. For example, if your app is created in my_app.py, then you need to execute the following in your terminal:
export FLASK_APP=my_app.py
If you restart the terminal at any point you will need to re-export this variable.
You can check if it is set in your current terminal using:
echo $FLASK_APP
which will show nothing if unset or my_app.py if set as above.
Once in your project directory run the command below will solve your problem
flask --app app.py db init
Where app.py is your flask application file name.

Will my virtual environment folder still work if i shift it from my local machine to a shared folder?

Currently, I have a Flask app waiting to be shifted to a shared drive in the company's local network. My current plan is to create a virtual environment inside a folder locally, install all the necessary dependencies like Python3, Flask, Pandas and etc. This is to ensure that my Flask app can still reference to necessary dependencies to run the app.
As I can't access my shared drive via command prompt, my plan is to create a virtual environment locally then shift it to the shared folder together with all the scripts required by my Flask app. Will the app be able to run on the shared drive in this manner?
The standard virtualenv hardcodes the path of the environment into some of its files in the environment it creates, and stops working if you rename it etc. So no, you'll probably have to recreate the env on the destination server.
Perhaps your app's startup script could take care of creating the env if it's missing.

Is it possible to override uwsgi ini-file with environment variables

I'm trying to build a "base" docker image for running a python framework with uwsgi. The goal is to have others build their own docker images where they dump their application logic and any configuration overrides they need.
I thought it might be nice to be able to override any default settings from a uwsgi.ini file by supplying UWSGI_* environment variables passed to uwsgi at startup.
I've tried this approach, and setting a value via env var works if it's not in the ini-file at all (e.g UWSGI_WORKERS=4). But if I put a workers=1 line in the ini-file, it seems to override the env var.
Is this expected behaviour? I'm having trouble finding anything about config resolution order in the docs.
Do I have to resort to something like this? Using env vars seems so much cleaner.
if-exists = ./override.ini
include = %(_)
endif =
First, make all environment variables in the .ini file refer to the environment variables like below:
[uwsgi]
http = $(HTTP_PORT)
processes = $(UWSGI_WORKERS)
threads = $(UWSGI_THREADS)
...
Then set whatever default values you want for these environment variables inside the Dockerfile.
Now, anyone using your base image can overwrite any config by setting the specific env variable.

I added a SECRET_KEY config variable to my Django app on Heroku but now it won't work locally

I changed my secret key to an environment variable on my Heroku app. I changed it because I found out that keeping the secret key in settings.py was a security risk.
However, now it won't work locally when I use python manage.py runserver. It gives an error about the secret key.
How do I fix it so I can develop my Heroku app locally?
You can export your secret key as an environment variable locally.
export SECRET_KEY=mysecretkey
./manage.py runserver
Or you could change your settings.py to use a hardcoded secret key in DEBUG mode. If you do this, make sure you are running with DEBUG = False on Heroku.
import os
if DEBUG:
SECRET_KEY = 'mysecretkey'
else:
SECRET_KEY = os.environ['SECRET_KEY']
You have to set your environmental variables in your development environment.
Windows
Go to Computer > Properties > Advanced System Settings.
Go to the Advanced tab, and at the bottom there is an Environment Variables... button.
In there you can edit the variables as you like.
Linux
Edit /etc/environment to include:
SECRET_KEY = <yoursecretkey>
or
You should be using a virtual environment to isolate your system Python installation from your different projects (it solves conflicting version requirements) and to make deployment easier. Virtualenv Tutorial
To activate your virtual environment when you want to use it there is a shell script located at <your_virtualenv>/bin/activate that handles changing all the environmental variables that make the virtual environment work.
Add:
SECRET_KEY='<yoursecretkey>'
export SECRET_KEY
to the bottom of the activate file and when it is run it will add (export) the environmental variable.
I had a bit of trouble working this out too, found my answer here: Set up your local environment variables
If you are using heroku local to develop locally, this might work for you. I just needed to include this line in an .env file placed in the top directory with my Procfile:
SECRET_KEY = 'yourkey'
And in settings.py:
os.environ.get('SECRET_KEY')
This works great for me. Otherwise if for whatever reason you aren't using heroku local, maybe you could try importing your key from another file when working locally (and placing this file in your gitignore), and swapping back to the heroku config variable for deployment.

Categories

Resources