Flask is not able to read my environment variable - flask

I want to automatically import my FLASK_APP environment variable when running the Windows cmd.
I installed the python-dotenv, then created an .env file under my project main path.
However, I kept getting errors saying " Could not locate Flask application. You did not provide the FLASK_APP environment variable."
The following is the path I have for my project
project Path
And my .env is saved under the same path "Flask Virtue Environment\env\Voting Platform\Voting_Platform"
my .env file has the following code
FLASK_APP=runserver.py
Also I ran the following command to test if dotenv is able to locate my Environment File. I think it did find the file. (see below)
dotenv test
I am confused why the flask isn't able to read my .env file

I never develop flask in windows, but you can read this document to get clue for you problem.
Happy coding.

Related

vscode testing with pytest for dockerized Django project

i want to debug my tests in vscode for my Django project witch is set up in a docker container but the problem is it always says pytest discovery error,
sometimes it seems to not be able to import modules(for example models) inside the test file and after some configuration it seems to not find the test file itself. all the folders of my project have an init.py file so everything is treated as a package, by the way there is no problem importing modules in my Django project.
the odd thing is when I run pytest inside of my docker container it works fine but I want to debug the tests.
i am fairly new to this so any help would be welcomeenter image description here
I enabled pytest in the .vscode/settings.jsonenter image description here file as some other settings
I tried also creating a .env file with PYTHONPATH in it to my root folder cause every tutorial I found work with a virtual environment and not a docker container

Flask environment variables being ignored (FLASK_ENV and FLASK_APP) WINDOWS 10

After setting the environment variables FLASK_ENV and FLASK_APP running flask run will give me this error:
The snippet shows the command promt.
It says that environment is production and that I didn't provide the FLASK_APP environment variable, even though I typed them in. Did I miss something or can someone explain why this error occurs?
i suggest you this good read https://www.twilio.com/blog/how-run-flask-application on how to run Flask Application (there are 2 approaches)
i also recommend you reading this topic https://flask.palletsprojects.com/en/1.1.x/cli/?highlight=flaskenv#environment-variables-from-dotenv
the better approach is to create .flaskenv file in the root of your project where you set your environment variables like so :
in /.flaskenv file
FLASK_APP=myflaskproject:create_app()
FLASK_ENV=development
FLASK_DEBUG=0
# FLASK_RUN_EXTRA_FILES=
# FLASK_RUN_HOST=
# FLASK_RUN_PORT=8080
# FLASK_RUN_CERT=
# FLASK_RUN_KEY=
in FLASK_APP you call your app but usually it's recommended to use "Application Factory" pattern, see https://flask.palletsprojects.com/en/1.1.x/patterns/appfactories/
then
(.venv) flask run
don't forget to install python-dotenv
At least on Linux you cannot have spaces around the equal sign. Maybe on Windows this is the same.
In the official Flask documentation you can read:
set FLASK_APP=hello
https://flask.palletsprojects.com/en/1.1.x/cli/
set flask<space>=<space>src/app.py
As you can see you are NOT setting flask but flask<space>. Flask may accept that path but it is an illegal windows path.

Where do I set environment variables for Django?

everyone!
Django 1.11 + PostgreSQL 9.6 + Gunicorn + Ubuntu 16.04 in AWS
I want to set environment variables for sensitive info.(django secret key, DB password...)
I studied many articles about setting ways.
But when I tried os.environ['env_name'],
.bashrc: Not working
.bash_profile: Not working
.profile: Not working
/etc/environment: Not working
Gunicorn script file.(systemd): I set them in gunicorn systemd script. It work very well.
But because I want to use the environment variables in other program too, I set them among 1~5 configurations. I don't understand why 1~5 configurations didn't work. Is there scope or priority of setting environment variables?
EDIT:
I use Ubuntu 16.04 server. I can't restart terminal session.
I tried 'source .bashrc' and logout/login. But It didn't work.
Of cource, 'echo $some_env_var' is working, I say, django can't read.
.bashrc will work for local development but not for a production environment. I just spent quite a bit of time looking for the answer to this and here's what worked for me:
1) Create a file somewhere on your server titled settings.ini. I did this in /etc/project/settings.ini
2) Add your config data to that file using the following format where the key could be an environmental variable and the value is a string. Note that you don't need to surround the value in quotes.
[section]
secret_key_a=somestringa
secret_key_b=somestringb
3) Access these variables using python's configparser library. The code below could be in your django project's settings file.
from configparser import RawConfigParser
config = RawConfigParser()
config.read('/etc/project/settings.ini')
DJANGO_SECRET = config.get('section', 'secret_key_a')
Source: https://code.djangoproject.com/wiki/SplitSettings (ini-style section)
The simplest solution is as already mentioned using os.environ.get and then set your server environment variables in some way (config stores, bash files, etc.)
Another slightly more sophisticated way is to use python-decouple and .env files. Here's a quick how-to:
1) Install python-decouple (preferably in a venv if not using Docker):
pip install python-decouple
2) Create a .env file in the root of your Django-project, add a key like;
SECRET_KEY=SomeSecretKeyHere
3) In your settings.py, or any other file where you want to use the configuration values:
from decouple import config
...
SECRET_KEY = config('SECRET_KEY')
4) As you probably don't want these secrets to end up in your version control system, add the file to your .gitignore. To make it easier to setup a new project, you could have a .env_default checked into the VCS containing default/dummy-values that's not used in production.
create a file called .bashrc in your server
export('the_name_in_bashrc', some_value)
then in the settings.py
import os
some_variable = os.environ.get('the_name_in_bashrc')
If you're using a virtual ENV you can add the environment variables to that specific environment. You can use export KEY=VALUE in your terminal but that will not persist. If you would like your values to persist you can edit the file:
sudo nano your_environment/bin/activate
Then at the bottom add the values you want e.g.:
export MY_KEY="12345"
And save. Remember to restart your ENV for changes to take effect.
pip install python-dotenv
Go To settings.py
import os
from dotenv import load_dotenv
load_dotenv('.env')
SECRET_KEY = os.getenv('SECRET_KEY')
Go To .env file
SECRET_KEY = your_secret_key

django - How to manually set sys.path?

Till now I was making change on my django production server (yes, really really bad :p ). I am wanna to go to a git process, and creating a local test server before deployement. So, I downloaded my python files, and ran a :
python manage.py runserver
hoping and prayed... but it was not enough, I got a nice error :
django.core.exceptions.ImproperlyConfigured: WSGI application 'issc.issc.wsgi.application' could not be loaded; Error importing module: 'No module named issc.wsgi'
I read in the documentation that [manage.py] is created automatically and sets up several key parts :
In addition, manage.py is automatically created in each Django project. manage.py is a thin wrapper around django-admin.py that takes care of several things for you before delegating to django-admin.py:
It puts your project’s package on sys.path.
It sets the DJANGO_SETTINGS_MODULE environment variable so that it points to your project’s settings.py file.
It calls django.setup() to initialize various internals of Django.
My question is : how can I manually set up these variables ? Because in my case I downloaded all the files on an arbitrary directory, but it was not enough. Eveything is here, but it is missing the link to this everything....
If you want to manually set the address of your config file, you can set DJANGO_SETTINGS_MODULE with the following:
export DJANGO_SETTINGS_MODULE='settings_to_load'
You can set all your environmental variables this way and it should work. I recommend to use a Virtualenv to this at least.

Pycharm runserver error with custom settings.py files

just started using pycharm on a existing project, and having difficulty using the run/debug configurations in order to run the test server. The issue stems from their being no settings.py module as standard in django projects. Instead there is settings_base.py and then two different settings modules (settings_live.py and settings_test.py) that extend it by using 'from settings_base import *'. This causes manage.py to fail when running runserver because it can't find settings.py.
In the Django Support settings I have set the project root, and set Settings to settings_test.py however this has not helped. In the Django server run configuration I have setup I also have...
DJANGO_SETTINGS_MODULE=settings_test
... in the Environment Variables section. However when I use runserver it still says
Error: Can't find the file 'settings.py' in the directory containing '/home/pete/Projects/the_project/manage.py'. It appears you've customized things.
You'll have to run django-admin.py, passing it your settings module.
(If the file settings.py does indeed exist, it's causing an ImportError somehow.)
I tried wrapping the entire settings_test.py module in a try/except to see if it really was an import error, however it did not seem to work. Is there something I am missing?
Maybe you should try adding --settings=settings_test to the "Additional options" in your PyCharm Run configuration and make sure that "Working directory" points to correct path.