failed to find 'app' attribute while using gunicorn - flask

I have two files app.py which is in root and the init.py inside a directory named website.
error log:
(vidscript) root#localhost:~# gunicorn --workers 3 --bind 0.0.0.0:8000 vidscript:app
[2023-02-06 17:01:56 +0000] [5272] [INFO] Starting gunicorn 20.1.0
[2023-02-06 17:01:56 +0000] [5272] [INFO] Listening at: http://0.0.0.0:8000 (5272)
[2023-02-06 17:01:56 +0000] [5272] [INFO] Using worker: sync
[2023-02-06 17:01:56 +0000] [5273] [INFO] Booting worker with pid: 5273
Failed to find attribute 'app' in 'vidscript'.
app.py:
from website import create_app
if __name__ == "__main__":
app = create_app()
app.run()
I expected to run the home page while it actually wokers in development server mode using. python app.py

Related

gunicorn Failed to find attribute 'app' in 'app'

I am hoping someone can help. I have been trying to launch my flask app on ubuntu. The app runs on dev mode, but when I try to run it with gunicorn, I get the following error:
[2022-07-24 21:55:44 +0000] [3710] [INFO] Starting gunicorn 20.1.0
[2022-07-24 21:55:44 +0000] [3710] [INFO] Listening at: http://127.0.0.1:8000 (3710)
[2022-07-24 21:55:44 +0000] [3710] [INFO] Using worker: sync
[2022-07-24 21:55:44 +0000] [3711] [INFO] Booting worker with pid: 3711
Failed to find attribute 'app' in 'app'.
[2022-07-24 21:55:44 +0000] [3711] [INFO] Worker exiting (pid: 3711)
[2022-07-24 21:55:44 +0000] [3710] [INFO] Shutting down: Master
[2022-07-24 21:55:44 +0000] [3710] [INFO] Reason: App failed to load.
Here is my app.py file
from website import create_app
if __name__ == "__main__":
app = create_app()
app.run(debug=True)
Here the site structure.
Website
script
static
-templates
-apy.py
-auth.py
-db
-models
-views
-innit.py
app.py
Can someone help?
Thanks
S
I found the issue. Had to move the app=create_app above the 'if' statement.

Start CKAN 2.9.X with Gunicorn?

Is there a way to start CKAN 2.9 with gunicorn?
I can see from the documentation that gunicorn is mentioned:
Install Supervisor (a Process Control System) used to control
starting, stopping the uwsgi or gunicorn servers:
But not much information is in there.
Since 2.9 is flask I was looking at how to serve a flask application with Gunicorn an I came across this post: How To Serve Flask Applications with Gunicorn and Nginx on Ubuntu 18.04 and I tried by using this code (wsgi.py):
import os
from ckan.config.middleware import make_app
from ckan.cli import CKANConfigLoader
from logging.config import fileConfig as loggingFileConfig
app = None
if __name__ == "__main__":
config_filepath = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'ckan.ini')
abspath = os.path.join(os.path.dirname(os.path.abspath(__file__)))
loggingFileConfig(config_filepath)
config = CKANConfigLoader(config_filepath).get_config()
app = make_app(config)
Then running:
gunicorn --bind 0.0.0.0:5000 wsgi:app
But I get:
[2022-06-22 20:09:34 +0000] [365] [INFO] Starting gunicorn 20.1.0
[2022-06-22 20:09:34 +0000] [365] [INFO] Listening at: http://0.0.0.0:5000 (365)
[2022-06-22 20:09:34 +0000] [365] [INFO] Using worker: sync
[2022-06-22 20:09:34 +0000] [367] [INFO] Booting worker with pid: 367
Failed to find application object: 'app'
[2022-06-22 20:09:35 +0000] [367] [INFO] Worker exiting (pid: 367)
[2022-06-22 20:09:35 +0000] [365] [INFO] Shutting down: Master
[2022-06-22 20:09:35 +0000] [365] [INFO] Reason: App failed to load.
Any idea is appreciated

serve static files from Django Docker to nginx

I'm dockerizing Django application but static files are not being served.
settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(os.path.dirname(BASE_DIR), 'static_my_project')
]
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'static_cdn', 'static_root')
docker-compose.yml
services:
nginx:
image: nginx:alpine
container_name: "originor-nginx"
ports:
- "10080:80"
- "10443:43"
volumes:
- .:/app
- ./config/nginx:/etc/nginx/conf.d
- originor_static_volume:/app/static_cdn/static_root
- originor_media_volume:/app/static_cdn/media_root
depends_on:
- web
web:
build: .
container_name: "originor-web"
command: ["./wait-for-it.sh", "db:5432", "--", "./start.sh"]
volumes:
- .:/app
- originor_static_volume:/app/static_cdn/static_root
- originor_media_volume:/app/static_cdn/media_root
ports:
- "9010:9010"
depends_on:
- db
db:
image: postgres:11
container_name: "originor-postgres-schema"
volumes:
- originor_database:/var/lib/postgresql/data
ports:
- "5432:5432"
pgadmin:
image: dpage/pgadmin4
container_name: "originor_pgadmin"
volumes:
- originor_pgadmin:/var/lib/pgadmin
volumes:
originor_database:
originor_static_volume:
originor_media_volume:
originor_pgadmin:
and nginx.conf
error_log /var/log/nginx/error.log;
include /etc/nginx/conf.d/proxy.conf;
proxy_headers_hash_bucket_size 128;
upstream dweb {
ip_hash;
server web:9010 fail_timeout=0;
}
server {
listen 10080;
server_name localhost;
access_log /var/log/nginx/localhost.access.log combined;
location /static/ {
autoindex on;
alias /app/static_cdn/static_root/;
}
location /media/ {
alias /app/static_cdn/media_root/;
}
location / {
proxy_pass http://dweb/;
}
}
But on access /admin/ in browser, it consoles
f032d416bce1_originor-web | Not Found: /static/admin/css/login.css
f032d416bce1_originor-web | Not Found: /static/admin/css/responsive.css
f032d416bce1_originor-web | Not Found: /static/admin/css/base.css
f032d416bce1_originor-web | Not Found: /static/admin/css/base.css
I can verify the files there in /app/static_cdn/static_root directory by executing
docker exec -it <container_id> ls -la /app/static_cdn/static_root
Edit 2: docker logs <container>
wait-for-it.sh: waiting 15 seconds for db:5432
wait-for-it.sh: db:5432 is available after 0 seconds
--: Starting application build
--: Creating migration
No changes detected
------: makemigrations complete
--: Running migration
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
No migrations to apply.
------: migrate complete
--: load initial user data
--: load initial oauth app data
--: Running collectstatic
0 static files copied to '/app/static_cdn/static_root', 119 unmodified.
------: collectstatic complete
--: Starting Gunicorn.
[2019-01-11 13:26:47 +0000] [21] [INFO] Starting gunicorn 19.9.0
[2019-01-11 13:26:47 +0000] [21] [INFO] Listening at: http://0.0.0.0:9010 (21)
[2019-01-11 13:26:47 +0000] [21] [INFO] Using worker: sync
[2019-01-11 13:26:47 +0000] [23] [INFO] Booting worker with pid: 23
[2019-01-11 13:26:47 +0000] [24] [INFO] Booting worker with pid: 24
[2019-01-11 13:26:47 +0000] [25] [INFO] Booting worker with pid: 25
Not Found: /static/admin/css/fonts.css
Edit 3: nginx log
While running docker-compose up it gives the following log
But running docker logs originor-nginx it gives nothing
Somewhere in your 'web' initialization you must call manage.py collectstatic to put static files of application in your volume. More info https://docs.djangoproject.com/en/2.1/howto/static-files/
UPD:
nginx conf for uwsgi proxy:
location / {
uwsgi_pass uwsgi://web:9010;
include uwsgi_params;
}

nginx+gunicorn+django+aws bad request

I was following a tutorial at https://ashokfernandez.wordpress.com/2014/03/11/deploying-a-django-app-to-amazon-aws-with-nginx-gunicorn-git/
on deploying a django application. This is my current situation:
fab spawn instance created the aws instance with nginx and gunicorn installed but when I tried accessing the site on that machine I got a 400 Bad Request. I checked the nginx-error log but that was empty and the nginx-access log showed that it had received the requests. The supervisor log had the following:
[2015-01-31 21:26:20 +0000] [15823] [INFO] Starting gunicorn 19.2.0
[2015-01-31 21:26:20 +0000] [15823] [INFO] Listening at: http://127.0.0.1:8002/ (15823)
[2015-01-31 21:26:20 +0000] [15823] [INFO] Using worker: sync
[2015-01-31 21:26:20 +0000] [15832] [INFO] Booting worker with pid: 15832
[2015-01-31 21:26:20 +0000] [15833] [INFO] Booting worker with pid: 15833
[2015-01-31 21:26:20 +0000] [15834] [INFO] Booting worker with pid: 15834
[2015-01-31 21:26:20 +0000] [15835] [INFO] Booting worker with pid: 15835
[2015-01-31 21:26:20 +0000] [15836] [INFO] Booting worker with pid: 15836
[2015-01-31 21:26:31 +0000] [15837] [INFO] Starting gunicorn 19.2.0
[2015-01-31 21:26:31 +0000] [15837] [ERROR] Connection in use: (‘127.0.0.1′, 8002)
[2015-01-31 21:26:31 +0000] [15837] [ERROR] Retrying in 1 second.
[2015-01-31 21:26:32 +0000] [15837] [ERROR] Connection in use: (‘127.0.0.1′, 8002)
[2015-01-31 21:26:32 +0000] [15837] [ERROR] Retrying in 1 second.
[2015-01-31 21:26:33 +0000] [15837] [ERROR] Connection in use: (‘127.0.0.1′, 8002)
[2015-01-31 21:26:33 +0000] [15837] [ERROR] Retrying in 1 second.
[2015-01-31 21:26:34 +0000] [15837] [ERROR] Connection in use: (‘127.0.0.1′, 8002)
[2015-01-31 21:26:34 +0000] [15837] [ERROR] Retrying in 1 second.
[2015-01-31 21:26:35 +0000] [15837] [ERROR] Connection in use: (‘127.0.0.1′, 8002)
[2015-01-31 21:26:35 +0000] [15837] [ERROR] Retrying in 1 second.
[2015-01-31 21:26:36 +0000] [15837] [ERROR] Can’t connect to (‘127.0.0.1′, 8002)
[2015-01-31 21:26:37 +0000] [15846] [INFO] Starting gunicorn 19.2.0
[2015-01-31 21:26:37 +0000] [15846] [INFO] Listening at: http://127.0.0.1:8002 (15846)
[2015-01-31 21:26:37 +0000] [15846] [INFO] Using worker: sync
[2015-01-31 21:26:37 +0000] [15855] [INFO] Booting worker with pid: 15855
[2015-01-31 21:26:37 +0000] [15856] [INFO] Booting worker with pid: 15856
[2015-01-31 21:26:37 +0000] [15857] [INFO] Booting worker with pid: 15857
[2015-01-31 21:26:38 +0000] [15858] [INFO] Booting worker with pid: 15858
[2015-01-31 21:26:38 +0000] [15859] [INFO] Booting worker with pid: 15859
I changed ALLOWED_HOSTS from [] to ["*"] and then '*'
When I changed it to the string, I got "The requested URL / was not found on this server." On other instances, I got 400 bad request.
This is the first time I am deploying a django app on nginx and I can’t figure out what the problem might be. Could you please help me debug this error?
Thanks in advance!!
PS: Please let me know if I need to post any config files. So far I have just followed the tutorial and I have not changed any configurations.
UPDATE:
common.py
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
SECRET_KEY = 'blah'
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
ROOT_URLCONF = 'mosaic.urls'
WSGI_APPLICATION = 'mosaic.wsgi.application'
# Internationalization
# https://docs.djangoproject.com/en/1.7/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
prod.py
from mosaic.settings.common import *
DEBUG = True #Change this after the project starts working
TEMPLATE_DEBUG = True
ALLOWED_HOSTS = []
# Database
# https://docs.djangoproject.com/en/1.6/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'mosaic',
'USER' : 'adminuser',
'PASSWORD' : '****',
'HOST' : 'abcd.cjcgmgnogsvc.us-west-1.rds.amazonaws.com',
'PORT' : '5432',
}
}
# Static files via Amazon S3 (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/
INSTALLED_APPS += ('storages',)
AWS_STORAGE_BUCKET_NAME = "xyz"
STATICFILES_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
S3_URL = 'http://%s.s3.amazonaws.com/' % AWS_STORAGE_BUCKET_NAME
STATIC_URL = S3_URL
Also in my project config files I have
fabconf['DOMAINS'] = ""
I thought this might be relevant as well
The allowed hosts has to match the host names you'll be using. So put the ALLOWED_HOSTS = [] in a settings file specific to your environment (Dev or production). On the dev side set it to ALLOWED_HOSTS = ['localhost', '127.0.0.1'] and on production set it go your domain & server IPs.
From the docs:
Values in this list can be fully qualified names (e.g. 'www.example.com'), in which case they will be matched against the request’s Host header exactly (case-insensitive, not including port). A value beginning with a period can be used as a subdomain wildcard: '.example.com' will match example.com, www.example.com, and any other subdomain of example.com. A value of '*' will match anything; in this case you are responsible to provide your own validation of the Host header (perhaps in a middleware; if so this middleware must be listed first in MIDDLEWARE_CLASSES).
The status 400 response you get is due to a SuspiciousOperation exception being raised when your host header doesn't match any values in that list.

unable to run django app using foreman and gunicorn

I am trying to run my django app using gunicorn and foreman. I can successfully run it using python manage.py server. However when running it using forman it fails -
15:32:01 web.1 | started with pid 29188
15:32:01 web.1 | 2012-08-16 15:32:01 [29191] [INFO] Starting gunicorn 0.14.6
15:32:01 web.1 | 2012-08-16 15:32:01 [29191] [INFO] Listening at: http://127.0.0.1:8000 (29191)
15:32:01 web.1 | 2012-08-16 15:32:01 [29191] [INFO] Using worker: sync
15:32:01 web.1 | 2012-08-16 15:32:01 [29194] [INFO] Booting worker with pid: 29194
15:32:01 web.1 | 2012-08-16 15:32:01 [29194] [INFO] Worker exiting (pid: 29194)
15:32:02 web.1 | 2012-08-16 15:32:02 [29191] [INFO] Shutting down: Master
15:32:02 web.1 | 2012-08-16 15:32:02 [29191] [INFO] Reason: Worker failed to boot.
15:32:02 web.1 | exited with code 3
Below is the contents of the Procfile -
web: gunicorn tms.wsgi
I have been following the instructions given on heroku for setting it up.
What instructions are you following? I run Django apps on Heroku using this:
web: python django_project/manage.py run_gunicorn -b "0.0.0.0:$PORT" -w 3 -k gevent --preload
What directory is your settings.py file located in in which you added 'gunicorn' to your INSTALLED_APPS? I'm assuming you're following the Heroku Getting Started with Django tutorial. The .wsgi file you state in your Procfile should match with that directory which you have your settings.py in.
Your Procfile should be:
web: gunicorn <directory_containing_settings.py_file>.wsgi -b 0.0.0.0:$PORT
If you are facing trouble following the Heroku Documentation then you may check my sample app deployed to Heroku https://github.com/shinigamiryuk/Django-Heroku-Sample-Application