I am confused by the DEBUG and production/development settings running flask.
I have setup my config file as classes
app.config.from_object("config.Production")
For demonstration my class use has more content.
class Production(Config):
DEBUG = False
...
I also use a .env file which only includes
FLASK_ENV=production
FLASK_APP=run
Running the app in this way show this in the terminal
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
I am confused how to handle (and use) the differences between FLASK_ENV and what is set in the config file. Debug mode is always on despite setting my env as in production? It feels like I am missing something fundamental about how env and config is handled?
Related
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.
What is the expected process when one wants to deploy a django website automatically by means of a continuous integration process: how can we set debug mode to false without editing the configuration file?
Generally there are three very common approaches to switch between production and development environments in Django applications:
Create a separate settings.py file with a different name and point to it using the DJANGO_SETTINGS_MODULE environment variable. Once setting a value to it, the expected settings file within the project folder will be ignored.
Use Python conditional statements to check for variables specific to your environment like if settings.DEBUG: and decide for other ones within that block of code.
Make a settings directory inside the project folder and create three additional files; one for common settings variables like common.py, and another two for local and production specific variables such as dev.py and prod.py. You can specify your __init__ to always import common.py and try to import one of the others if they are found.
Generally, you don't change anything in your code repository when deploying to production. It's the same code as on your local machine. The only difference is that your app server (gunicorn or uwsgi) is running with a different DJANGO_SETTINGS_MODULE environment variable.
I cannot get my swagger to display properly on ubuntu django setup. And I am using function based API just like here https://github.com/m-haziq/django-rest-swagger-docs
This is the outcome which doesnt display swagger properly - ubuntu 16.04 (in AWS)
https://imgur.com/TnTNExa <-- this is the problem, how to fix this ?
But on my development environment mac pc
https://imgur.com/E1Zst0E <--- its good on PC (Mac)
Here is my swagger schema. As you can see I have some logging:
https://gitlab.com/firdausmah/railerdotcom/blob/master/railercomapp/swagger_schema.py
Here are some logging:
2017-11-30 06:06:57,367 DEBUG xxxx home hello
2017-11-30 06:07:25,131 DEBUG get(self, request)
2017-11-30 06:07:25,132 DEBUG Check and load if the function has __doc__
2017-11-30 06:07:25,132 DEBUG swagger try yaml_doc
2017-11-30 06:07:25,134 DEBUG if yaml_doc
My Django/NGINX/Ubuntu setup is based on this:
https://jee-appy.blogspot.my/2017/01/deply-django-with-nginx.html
Feel free to look through my code,
https://gitlab.com/firdausmah/railerdotcom/tree/master
what could be the problem with swagger? On development its working. There is nothing different how I setup development & production. On production its using nginx, gunicorn, supervisor. on PC its running on python manage.py runserver.
This error is because your swagger static files are inaccessible or at unknown location. You can see the error shown in your (http://railer.com/swagger/) console, which is:
To fix error try adding following lines to your settings.py:
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
If this doesnt work then add your static files directory to it. For more info about it, look at this answer:
How to use a remote static file server while developing with django manage.py runserver
I'm having trouble understanding the switchover from local to production settings for deploying Django projects. I'm using an Ubuntu virtual machine (VM) if it matters.
I understand how to configure my settings. I understand best practices for creating settings files (base.py, local.py, production.py, blah, blah). I know that in local development DEBUG=True, in production DEBUG=False, blah, blah.
But how do I implement this switchover in deployment? Do I get rid of the local.py? Do I create some kind of logic so that my VM only reads base.py and production.py?
What's the best approach?
I'm not sure about the best approach but what I do works...
At the foot of my settings.py, I have:
try:
from local_settings import *
except ImportError, e:
pass
I keep all my local development settings in local_settings.py which overide any production settings. I also make sure not to upload my local_settings.py file!
What you could do is to check in your settings py which environment is used at the moment.
To do this you can set an environment variable on your system that would have diffrent values on your development environment and the production environment.
you can set these environment variables by
sudo -H gedit /etc/environment
and add the following line in the file:
DEBUG="true"
(to make this changes available you will have to log out and log back in into your system)
in the production environment you would then set DEBUG="false".
then you can do this in your settings.py:
DEBUG = os.environ.get('DEBUG', 'true') != 'false'
and then you can set every setting that would be different depending on the environment that is used like this:
if DEBUG:
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
else:
STATICFILES_STORAGE = STATICFILES_STORAGE = 'custom_storages.StaticStorage'
(the setting above uses the local django server to serve staticfiles if it is in the development environment, and amazon s3 with boto if in the production environment (which is defined in the custom_storages module)
This way you can push your updates and always the right settings should be picked up depending on the environment.
My django app is communicating with external server and before running django server i would like load some config file. Variables from this file are going to be used by some module while app is running
Problem is that config file can be located in many places.
My dream would be to run manage.py --cfg "/path/to/cfg/file.cfg" or
manage.py runserver --cfg "/path/to/cfg/file.cfg"
and some variables (like globals?) are going to be loaded and they are going to be avaible for django modules to be used. After django server shutdown those variables can dissapear
Is there some nice way to accomplish this?
There seem to be two parts to your problem:
How do I support changing which set of variables (as defined in a config file) are used for a given run
How can I load these variables such that they are visible to all the modules of my application.
The standard mechanism for doing the 2nd is to put stuff in settings.py.
If you do FOO="bar" in settings.py, in your module you can do:
from django.conf import settings
if settings.FOO == "bar":
# Do something
As far as how you can support multiple configurations, the first thing I could come up with is to rename your real settings.py to be real_settings.py and then create a series of config1_settings.py, config2_settings.py, config3_settings.py ... which look like:
from real_settings.py import *
from path_to_configX.py import *
where configX.py has all the values for whatever variables you want for configuration X.
You would then start django's built in server via:
manage.py runserver --settings=configX_settings.py
Note that doing this for a production server (where you can't as easily just pass something on the command line to kick it off) may be a bit trickier, but you're going to need to provide more use case details for that.