I'm new to Python, so please bear with me.
I am attempting to create a file in which to store my configuration settings in a Flask project. However, I seem to be getting errors when I attempt to import the file.
Here's my configuration file (location: app/config.py):
database_uri = 'something here'
secret_key = something here"
And here's where I'm using it (location: app/models.py):
from app import config
...
app.config['SQLALCHEMY_DATABASE_URI'] = config.database_uri
However, I seem to be getting this error when launching the application:
[Sat Aug 08 19:00:15.539773 2015] [:error] [pid 29784] [client 188.183.57.54:64122] mod_wsgi (pid=29784): Target WSGI script '/var/www/pwforum/pwforum.wsgi' cannot be loaded as Python module.
[Sat Aug 08 19:00:15.540014 2015] [:error] [pid 29784] [client 188.183.57.54:64122] mod_wsgi (pid=29784): Exception occurred processing WSGI script '/var/www/pwforum/pwforum.wsgi'.
[Sat Aug 08 19:00:15.540146 2015] [:error] [pid 29784] [client 188.183.57.54:64122] Traceback (most recent call last):
[Sat Aug 08 19:00:15.540250 2015] [:error] [pid 29784] [client 188.183.57.54:64122] File "/var/www/pwforum/pwforum.wsgi", line 7, in <module>
[Sat Aug 08 19:00:15.540448 2015] [:error] [pid 29784] [client 188.183.57.54:64122] from app import app as application
[Sat Aug 08 19:00:15.540537 2015] [:error] [pid 29784] [client 188.183.57.54:64122] File "/var/www/pwforum/app/__init__.py", line 12, in <module>
[Sat Aug 08 19:00:15.540685 2015] [:error] [pid 29784] [client 188.183.57.54:64122] from app import views, models
[Sat Aug 08 19:00:15.540773 2015] [:error] [pid 29784] [client 188.183.57.54:64122] File "/var/www/pwforum/app/views.py", line 3, in <module>
[Sat Aug 08 19:00:15.541061 2015] [:error] [pid 29784] [client 188.183.57.54:64122] from app.models import db, User, Category, Topic, Post
[Sat Aug 08 19:00:15.541154 2015] [:error] [pid 29784] [client 188.183.57.54:64122] File "/var/www/pwforum/app/models.py", line 11, in <module>
[Sat Aug 08 19:00:15.541333 2015] [:error] [pid 29784] [client 188.183.57.54:64122] app.config['SQLALCHEMY_DATABASE_URI'] = config.database_uri
[Sat Aug 08 19:00:15.541413 2015] [:error] [pid 29784] [client 188.183.57.54:64122] AttributeError: 'module' object has no attribute 'database_uri'
Your config file should look like this:
SQLALCHEMY_DATABASE_URI = '<your-db-driver>://<user>:<pw>#<db-url>'
SECRET_KEY = '<your-very-secret-key>'
Then you can do:
app = Flask(__name__)
app.config.from_object('config')
Related
I'm experimenting and working on a Flask webpage what's gonna use some data from MySQL database and after a little coding I got internal server error and I don't know why.
After several days I cant work out the problem what can I do for it to work?
I'm using Python3, the server where is hosted is Ubuntu system and I can reach the site via LAN right now with the servers LAN domain (server's name) name that I host via Hyper-V cause I don't have a spare PC for that with Ubuntu on it.
The weird part abut it that when I test it locally on my laptop I wont get the 500 Error only if I want to connect the page what's hosted on the server virtual machine.
Because I don't know where is the problem I uploaded most of the files to GitHub, this gonna be changed of course and later deleted for security reasons but now its easier.
Link: https://github.com/Csabatron99/Webpage
Here is the apache2 error log
[Wed Jul 07 12:42:08.597500 2021] [wsgi:error] [pid 10179] [client fe80::1047:5664:1d7b:6d86:1170] mod_wsgi (pid=10179): Failed to exec Python script file '/var/www/FlaskApp/flaskapp.wsgi'.
[Wed Jul 07 12:42:08.597550 2021] [wsgi:error] [pid 10179] [client fe80::1047:5664:1d7b:6d86:1170] mod_wsgi (pid=10179): Exception occurred processing WSGI script '/var/www/FlaskApp/flaskapp.wsgi'.
[Wed Jul 07 12:42:08.597762 2021] [wsgi:error] [pid 10179] [client fe80::1047:5664:1d7b:6d86:1170] Traceback (most recent call last):
[Wed Jul 07 12:42:08.597784 2021] [wsgi:error] [pid 10179] [client fe80::1047:5664:1d7b:6d86:1170] File "/var/www/FlaskApp/flaskapp.wsgi", line 7, in <module>
[Wed Jul 07 12:42:08.597788 2021] [wsgi:error] [pid 10179] [client fe80::1047:5664:1d7b:6d86:1170] from FlaskApp import app as application
[Wed Jul 07 12:42:08.597793 2021] [wsgi:error] [pid 10179] [client fe80::1047:5664:1d7b:6d86:1170] File "/var/www/FlaskApp/FlaskApp/__init__.py", line 11, in <module>
[Wed Jul 07 12:42:08.597795 2021] [wsgi:error] [pid 10179] [client fe80::1047:5664:1d7b:6d86:1170] from .dbconnect import connection_db
[Wed Jul 07 12:42:08.597799 2021] [wsgi:error] [pid 10179] [client fe80::1047:5664:1d7b:6d86:1170] File "/var/www/FlaskApp/FlaskApp/dbconnect.py", line 3, in <module>
[Wed Jul 07 12:42:08.597802 2021] [wsgi:error] [pid 10179] [client fe80::1047:5664:1d7b:6d86:1170] import mysql.connector
[Wed Jul 07 12:42:08.597811 2021] [wsgi:error] [pid 10179] [client fe80::1047:5664:1d7b:6d86:1170] ModuleNotFoundError: No module named 'mysql'
[Wed Jul 07 12:42:08.816124 2021] [wsgi:error] [pid 10178] [client fe80::1047:5664:1d7b:6d86:1171] mod_wsgi (pid=10178): Failed to exec Python script file '/var/www/FlaskApp/flaskapp.wsgi'., referer: http://tikva-server-hv/
[Wed Jul 07 12:42:08.816173 2021] [wsgi:error] [pid 10178] [client fe80::1047:5664:1d7b:6d86:1171] mod_wsgi (pid=10178): Exception occurred processing WSGI script '/var/www/FlaskApp/flaskapp.wsgi'., referer: http://tikva-server-hv/
[Wed Jul 07 12:42:08.816386 2021] [wsgi:error] [pid 10178] [client fe80::1047:5664:1d7b:6d86:1171] Traceback (most recent call last):, referer: http://tikva-server-hv/
[Wed Jul 07 12:42:08.816409 2021] [wsgi:error] [pid 10178] [client fe80::1047:5664:1d7b:6d86:1171] File "/var/www/FlaskApp/flaskapp.wsgi", line 7, in <module>, referer: http://tikva-server-hv/
[Wed Jul 07 12:42:08.816413 2021] [wsgi:error] [pid 10178] [client fe80::1047:5664:1d7b:6d86:1171] from FlaskApp import app as application, referer: http://tikva-server-hv/
[Wed Jul 07 12:42:08.816418 2021] [wsgi:error] [pid 10178] [client fe80::1047:5664:1d7b:6d86:1171] File "/var/www/FlaskApp/FlaskApp/__init__.py", line 11, in <module>, referer: http://tikva-server-hv/
[Wed Jul 07 12:42:08.816426 2021] [wsgi:error] [pid 10178] [client fe80::1047:5664:1d7b:6d86:1171] from .dbconnect import connection_db, referer: http://tikva-server-hv/
[Wed Jul 07 12:42:08.816431 2021] [wsgi:error] [pid 10178] [client fe80::1047:5664:1d7b:6d86:1171] File "/var/www/FlaskApp/FlaskApp/dbconnect.py", line 3, in <module>, referer: http://tikva-server-hv/
[Wed Jul 07 12:42:08.816433 2021] [wsgi:error] [pid 10178] [client fe80::1047:5664:1d7b:6d86:1171] import mysql.connector, referer: http://tikva-server-hv/
[Wed Jul 07 12:42:08.816443 2021] [wsgi:error] [pid 10178] [client fe80::1047:5664:1d7b:6d86:1171] ModuleNotFoundError: No module named 'mysql', referer: http://tikva-server-hv/
and this is the code from the dbconnect.py
import mysql.connector
def connection_db():
conn1 = connector.connect(host="localhost",
user = "root",
passwd = "pass",
db = "db")
c1 = conn1.cursor()
return c1, conn1
Do not download the module I mistook content_management as pip module. you can uninstall the module pip uninstall contentful_management
The error comes because you are importing module in init.py.
try this:
from name_of_the_parent_directory import content_management
You an try using
from . import content_management
name_of_the_parent_directory - means the parent directory, in your github it will be webpage.
I am attempting to put my Flask application on Elastic Beanstalk (Amazon Web Services, EC2), but after uploading the files I receive a 500 error. Any thoughts on what could be the issue? The error log:
/opt/python/run/venv/bin/python3: can't open file 'mod_wsgi': [Errno 2] No such file or directory
[Fri Sep 08 03:23:05.754341 2017] [:error] [pid 13619] [remote 128.12.244.5:31824] mod_wsgi (pid=13619): Target WSGI script '/opt/python/current/app/application.py' cannot be loaded as Python module.
[Fri Sep 08 03:23:05.754577 2017] [:error] [pid 13619] [remote 128.12.244.5:31824] mod_wsgi (pid=13619): SystemExit exception raised by WSGI script '/opt/python/current/app/application.py' ignored.
[Fri Sep 08 03:23:05.754729 2017] [:error] [pid 13619] [remote 128.12.244.5:31824] Traceback (most recent call last):
[Fri Sep 08 03:23:05.754992 2017] [:error] [pid 13619] [remote 128.12.244.5:31824] File "/opt/python/current/app/application.py", line 11, in <module>
[Fri Sep 08 03:23:05.755007 2017] [:error] [pid 13619] [remote 128.12.244.5:31824] application.run(debug=True)
[Fri Sep 08 03:23:05.755116 2017] [:error] [pid 13619] [remote 128.12.244.5:31824] File "/opt/python/run/venv/lib/python3.4/site-packages/flask/app.py", line 772, in run
[Fri Sep 08 03:23:05.755131 2017] [:error] [pid 13619] [remote 128.12.244.5:31824] run_simple(host, port, self, **options)
[Fri Sep 08 03:23:05.755242 2017] [:error] [pid 13619] [remote 128.12.244.5:31824] File "/opt/python/run/venv/lib/python3.4/site-packages/werkzeug/serving.py", line 737, in run_simple
[Fri Sep 08 03:23:05.755257 2017] [:error] [pid 13619] [remote 128.12.244.5:31824] reloader_type)
[Fri Sep 08 03:23:05.755344 2017] [:error] [pid 13619] [remote 128.12.244.5:31824] File "/opt/python/run/venv/lib/python3.4/site-packages/werkzeug/_reloader.py", line 265, in run_with_reloader
[Fri Sep 08 03:23:05.755358 2017] [:error] [pid 13619] [remote 128.12.244.5:31824] sys.exit(reloader.restart_with_reloader())
[Fri Sep 08 03:23:05.755429 2017] [:error] [pid 13619] [remote 128.12.244.5:31824] SystemExit: 2
My file structure is as follows:
file hierarchy
My WSGI path is
config
Turns out I needed to wrap application.run() within a if __name__ == '__main__': block. This ensures that a local WSGI server is not loaded by default.
Source: http://flask.pocoo.org/docs/0.11/deploying/mod_wsgi/
|--------FlaskApp
|----------------FlaskApp
|----------------__init__.py
|----------------view.py
|----------------models.py
|----------------db_create.py
|-----------------------static
|-----------------------templates
|-----------------------venv
|-----------------------__init__.py
|----------------flaskapp.wsgi
this is my modules and folder layout i have a import problem in init i have db = sqlalchemy im trying to import db to views.py, models.py and db_create.py but im getting all kinds of importing errors because im not importing thr proper way now my question is if i want to import db to the modules i specified how would i do it without getting a error
some of the code importing db
models.py
from FlaskApp import db
class UserInfo(db.Model):
__tablename_ = 'user_info'
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True)
email = db.Column(db.String(80), unique=True)
password = db.Column(db.String(100), nullable=False)
posts = relationship('UserPosts', backref='posts')
def __init__(self, username, email, password):
self.username = username
self.email = email
self.password = password
def __repr__(self):
return '{}-{}'.format(self.username, self.email)
views.py
from FlaskApp import db
#app.route('/')
#login_required
def home():
user = db.session.query(UserInfo).all()
return render_template('home.html', user=user)
__init__.py
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy(app)
tail /var/log/apache2/error.log
[Tue Oct 11 04:17:20.925573 2016] [wsgi:error] [pid 5049:tid 139870897813248] [client 174.58.31.189:55884] Traceback (most recent call last):, referer: http://localhost/
[Tue Oct 11 04:17:20.925618 2016] [wsgi:error] [pid 5049:tid 139870897813248] [client 174.58.31.189:55884] File "/var/www/FlaskApp/flaskapp.wsgi", line 14, in <module>, referer: http://localhost/
[Tue Oct 11 04:17:20.925626 2016] [wsgi:error] [pid 5049:tid 139870897813248] [client 174.58.31.189:55884] from FlaskApp import app as application, referer: http://localhost/
[Tue Oct 11 04:17:20.925638 2016] [wsgi:error] [pid 5049:tid 139870897813248] [client 174.58.31.189:55884] File "/var/www/FlaskApp/FlaskApp/__init__.py", line 4, in <module>, referer: http://localhost/
[Tue Oct 11 04:17:20.925644 2016] [wsgi:error] [pid 5049:tid 139870897813248] [client 174.58.31.189:55884] import FlaskApp.main, referer: http://localhost/
[Tue Oct 11 04:17:20.925653 2016] [wsgi:error] [pid 5049:tid 139870897813248] [client 174.58.31.189:55884] File "/var/www/FlaskApp/FlaskApp/main.py", line 7, in <module>, referer: http://localhost/
[Tue Oct 11 04:17:20.925658 2016] [wsgi:error] [pid 5049:tid 139870897813248] [client 174.58.31.189:55884] from models import UserInfo, referer: http://localhost/
[Tue Oct 11 04:17:20.925668 2016] [wsgi:error] [pid 5049:tid 139870897813248] [client 174.58.31.189:55884] File "/var/www/FlaskApp/FlaskApp/models.py", line 2, in <module>, referer: http://localhost/
[Tue Oct 11 04:17:20.925673 2016] [wsgi:error] [pid 5049:tid 139870897813248] [client 174.58.31.189:55884] from FlaskApp import db, referer: http://localhost/
[Tue Oct 11 04:17:20.925707 2016] [wsgi:error] [pid 5049:tid 139870897813248] [client 174.58.31.189:55884] ImportError: cannot import name 'db', referer: http://localhost/
my goal is not just to solve one error is to be able to import the right way from now on
This is actually the most painful moment for a Flask learner. Basically, your FlaskApp is dependent on FlaskApp.main then FlaskApp.models and then back to FlaskApp which create a perfect case of circular dependency.
Here's the way which I think is elegant and pythonic, by attaching the db variable to app in __init__.py:
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy(app)
app.db = db
with app.app_context():
import FlaskApp.main
In main.py:
from flask import current_app as app
from .models import UserInfo
#app.route('/')
#login_required
def home():
user = app.db.session.query(UserInfo).all()
return render_template('home.html', user=user)
In models.py:
from flask import current_app as app
db = app.db
class UserInfo(db.Model):
__tablename_ = 'user_info'
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True)
email = db.Column(db.String(80), unique=True)
password = db.Column(db.String(100), nullable=False)
posts = relationship('UserPosts', backref='posts')
def __init__(self, username, email, password):
self.username = username
self.email = email
self.password = password
def __repr__(self):
return '{}-{}'.format(self.username, self.email)
Then you can use app.db or current_app.db inside of any functions/methods in your application and don't need to worry about the circular dependency.
got this after using the above answer
RuntimeError: Working outside of application context. This typically means that you attempted to use functionality that needed to interface with the current application object in a way. To solve this set up an application context with app.app_context(). See the documentation for more information.
tail -n 40 /var/log/apache2/error.log
[Wed Oct 12 00:20:02.315500 2016] [wsgi:error] [pid 19327:tid 139821662463744] [client 174.58.31.189:51784] import FlaskApp.main
[Wed Oct 12 00:20:02.315510 2016] [wsgi:error] [pid 19327:tid 139821662463744] [client 174.58.31.189:51784] File "/var/www/FlaskApp/FlaskApp/main.py", line 2, in <module>
[Wed Oct 12 00:20:02.315516 2016] [wsgi:error] [pid 19327:tid 139821662463744] [client 174.58.31.189:51784] from .models import UserInfo
[Wed Oct 12 00:20:02.315526 2016] [wsgi:error] [pid 19327:tid 139821662463744] [client 174.58.31.189:51784] File "/var/www/FlaskApp/FlaskApp/models.py", line 7, in <module>
[Wed Oct 12 00:20:02.315532 2016] [wsgi:error] [pid 19327:tid 139821662463744] [client 174.58.31.189:51784] db = app.db
[Wed Oct 12 00:20:02.315542 2016] [wsgi:error] [pid 19327:tid 139821662463744] [client 174.58.31.189:51784] File "/usr/local/lib/python3.5/dist-packages/werkzeug/local.py", line 343, in __getattr__
[Wed Oct 12 00:20:02.315547 2016] [wsgi:error] [pid 19327:tid 139821662463744] [client 174.58.31.189:51784] return getattr(self._get_current_object(), name)
[Wed Oct 12 00:20:02.315557 2016] [wsgi:error] [pid 19327:tid 139821662463744] [client 174.58.31.189:51784] File "/usr/local/lib/python3.5/dist-packages/werkzeug/local.py", line 302, in _get_current_object
[Wed Oct 12 00:20:02.315563 2016] [wsgi:error] [pid 19327:tid 139821662463744] [client 174.58.31.189:51784] return self.__local()
[Wed Oct 12 00:20:02.315573 2016] [wsgi:error] [pid 19327:tid 139821662463744] [client 174.58.31.189:51784] File "/usr/local/lib/python3.5/dist-packages/flask/globals.py", line 51, in _find_app
[Wed Oct 12 00:20:02.315579 2016] [wsgi:error] [pid 19327:tid 139821662463744] [client 174.58.31.189:51784] raise RuntimeError(_app_ctx_err_msg)
[Wed Oct 12 00:20:02.315611 2016] [wsgi:error] [pid 19327:tid 139821662463744] [client 174.58.31.189:51784] RuntimeError: Working outside of application context.
[Wed Oct 12 00:20:02.315620 2016] [wsgi:error] [pid 19327:tid 139821662463744] [client 174.58.31.189:51784]
[Wed Oct 12 00:20:02.315628 2016] [wsgi:error] [pid 19327:tid 139821662463744] [client 174.58.31.189:51784] This typically means that you attempted to use functionality that needed
[Wed Oct 12 00:20:02.315634 2016] [wsgi:error] [pid 19327:tid 139821662463744] [client 174.58.31.189:51784] to interface with the current application object in a way. To solve
[Wed Oct 12 00:20:02.315641 2016] [wsgi:error] [pid 19327:tid 139821662463744] [client 174.58.31.189:51784] this set up an application context with app.app_context(). See the
[Wed Oct 12 00:20:02.315648 2016] [wsgi:error] [pid 19327:tid 139821662463744] [client 174.58.31.189:51784] documentation for more information.
[Wed Oct 12 00:20:02.813916 2016] [wsgi:error] [pid 19326:tid 139821670856448] [client 174.58.31.189:51786] mod_wsgi (pid=19326): Target WSGI script '/var/www/FlaskApp/flaskapp.wsgi' cannot be loaded as Python module., referer: http://localhost/
[Wed Oct 12 00:20:02.814030 2016] [wsgi:error] [pid 19326:tid 139821670856448] [client 174.58.31.189:51786] mod_wsgi (pid=19326): Exception occurred processing WSGI script '/var/www/FlaskApp/flaskapp.wsgi'., referer: http://localhost/
[Wed Oct 12 00:20:02.814917 2016] [wsgi:error] [pid 19326:tid 139821670856448] [client 174.58.31.189:51786] Traceback (most recent call last):, referer: http://localhost/
[Wed Oct 12 00:20:02.814972 2016] [wsgi:error] [pid 19326:tid 139821670856448] [client 174.58.31.189:51786] File "/var/www/FlaskApp/flaskapp.wsgi", line 14, in <module>, referer: http://localhost/
[Wed Oct 12 00:20:02.814981 2016] [wsgi:error] [pid 19326:tid 139821670856448] [client 174.58.31.189:51786] from FlaskApp import app as application, referer: http://localhost/
[Wed Oct 12 00:20:02.814993 2016] [wsgi:error] [pid 19326:tid 139821670856448] [client 174.58.31.189:51786] File "/var/www/FlaskApp/FlaskApp/__init__.py", line 4, in <module>, referer: http://localhost/
[Wed Oct 12 00:20:02.814998 2016] [wsgi:error] [pid 19326:tid 139821670856448] [client 174.58.31.189:51786] import FlaskApp.main, referer: http://localhost/
[Wed Oct 12 00:20:02.815008 2016] [wsgi:error] [pid 19326:tid 139821670856448] [client 174.58.31.189:51786] File "/var/www/FlaskApp/FlaskApp/main.py", line 2, in <module>, referer: http://localhost/
[Wed Oct 12 00:20:02.815014 2016] [wsgi:error] [pid 19326:tid 139821670856448] [client 174.58.31.189:51786] from .models import UserInfo, referer: http://localhost/
[Wed Oct 12 00:20:02.815024 2016] [wsgi:error] [pid 19326:tid 139821670856448] [client 174.58.31.189:51786] File "/var/www/FlaskApp/FlaskApp/models.py", line 7, in <module>, referer: http://localhost/
[Wed Oct 12 00:20:02.815029 2016] [wsgi:error] [pid 19326:tid 139821670856448] [client 174.58.31.189:51786] db = app.db, referer: http://localhost/
[Wed Oct 12 00:20:02.815040 2016] [wsgi:error] [pid 19326:tid 139821670856448] [client 174.58.31.189:51786] File "/usr/local/lib/python3.5/dist-packages/werkzeug/local.py", line 343, in __getattr__, referer: http://localhost/
[Wed Oct 12 00:20:02.815046 2016] [wsgi:error] [pid 19326:tid 139821670856448] [client 174.58.31.189:51786] return getattr(self._get_current_object(), name), referer: http://localhost/
[Wed Oct 12 00:20:02.815055 2016] [wsgi:error] [pid 19326:tid 139821670856448] [client 174.58.31.189:51786] File "/usr/local/lib/python3.5/dist-packages/werkzeug/local.py", line 302, in _get_current_object, referer: http://localhost/
[Wed Oct 12 00:20:02.815061 2016] [wsgi:error] [pid 19326:tid 139821670856448] [client 174.58.31.189:51786] return self.__local(), referer: http://localhost/
[Wed Oct 12 00:20:02.815071 2016] [wsgi:error] [pid 19326:tid 139821670856448] [client 174.58.31.189:51786] File "/usr/local/lib/python3.5/dist-packages/flask/globals.py", line 51, in _find_app, referer: http://localhost/
[Wed Oct 12 00:20:02.815077 2016] [wsgi:error] [pid 19326:tid 139821670856448] [client 174.58.31.189:51786] raise RuntimeError(_app_ctx_err_msg), referer: http://localhost/
[Wed Oct 12 00:20:02.815107 2016] [wsgi:error] [pid 19326:tid 139821670856448] [client 174.58.31.189:51786] RuntimeError: Working outside of application context., referer: http://localhost/
[Wed Oct 12 00:20:02.815114 2016] [wsgi:error] [pid 19326:tid 139821670856448] [client 174.58.31.189:51786] , referer: http://localhost/
[Wed Oct 12 00:20:02.815118 2016] [wsgi:error] [pid 19326:tid 139821670856448] [client 174.58.31.189:51786] This typically means that you attempted to use functionality that needed, referer: http://localhost/
[Wed Oct 12 00:20:02.815123 2016] [wsgi:error] [pid 19326:tid 139821670856448] [client 174.58.31.189:51786] to interface with the current application object in a way. To solve, referer: http://localhost/
[Wed Oct 12 00:20:02.815139 2016] [wsgi:error] [pid 19326:tid 139821670856448] [client 174.58.31.189:51786] this set up an application context with app.app_context(). See the, referer: http://localhost/
[Wed Oct 12 00:20:02.815143 2016] [wsgi:error] [pid 19326:tid 139821670856448] [client 174.58.31.189:51786] documentation for more information., referer: http://localhost/
Getting the following error with flask on elasticbeanstalk:
/opt/python/run/venv/bin/python3: can't open file 'mod_wsgi': [Errno 2] No such file or directory
[Sat Aug 08 23:43:35.994357 2015] [:error] [pid 7748] [remote 127.0.0.1:45363] mod_wsgi (pid=7748): Target WSGI script '/opt/python/current/app/application.py' cannot be loaded as Python module.
[Sat Aug 08 23:43:35.994407 2015] [:error] [pid 7748] [remote 127.0.0.1:45363] mod_wsgi (pid=7748): SystemExit exception raised by WSGI script '/opt/python/current/app/application.py' ignored.
[Sat Aug 08 23:43:35.994449 2015] [:error] [pid 7748] [remote 127.0.0.1:45363] Traceback (most recent call last):
[Sat Aug 08 23:43:35.994511 2015] [:error] [pid 7748] [remote 127.0.0.1:45363] File "/opt/python/current/app/application.py", line 3, in <module>
[Sat Aug 08 23:43:35.994518 2015] [:error] [pid 7748] [remote 127.0.0.1:45363] app.run(debug=True)
[Sat Aug 08 23:43:35.994553 2015] [:error] [pid 7748] [remote 127.0.0.1:45363] File "/opt/python/run/venv/lib/python3.4/site-packages/flask/app.py", line 772, in run
[Sat Aug 08 23:43:35.994560 2015] [:error] [pid 7748] [remote 127.0.0.1:45363] run_simple(host, port, self, **options)
[Sat Aug 08 23:43:35.994590 2015] [:error] [pid 7748] [remote 127.0.0.1:45363] File "/opt/python/run/venv/lib/python3.4/site-packages/werkzeug/serving.py", line 623, in run_simple
[Sat Aug 08 23:43:35.994596 2015] [:error] [pid 7748] [remote 127.0.0.1:45363] reloader_type)
[Sat Aug 08 23:43:35.994624 2015] [:error] [pid 7748] [remote 127.0.0.1:45363] File "/opt/python/run/venv/lib/python3.4/site-packages/werkzeug/_reloader.py", line 231, in run_with_reloader
[Sat Aug 08 23:43:35.994630 2015] [:error] [pid 7748] [remote 127.0.0.1:45363] sys.exit(reloader.restart_with_reloader())
[Sat Aug 08 23:43:35.994652 2015] [:error] [pid 7748] [remote 127.0.0.1:45363] SystemExit: 2
I'm using elasticbeanstalk command line interface to deploy the flask app.
You're using app.run in production. Don't use the dev server in production.
mod_wsgi, like any WSGI app server, expects to find an application instance when importing, but you're running the dev server instead.
Guard against running the dev server on import. Depending on your mod_wsgi settings, it may expect the name application instead of app as well.
application = app
if __name__ == '__main__':
app.run()
I am currently trying to publish via ZIP my django app to an AWS elasticbeanstalk.
As I have a local and online development enviroment I would like to make use of the environ variables, that are used anyways by AWS.
For my development environments I am already using virtualenv. Apache runs with wsgi enabled and is supposed to use such environments. Unfortunately, it does not know the environ that I have set in the bin/activate.
Django throws a 500 error, since it cannot connect to the database as it does not have access to the environ vars:
1:53.862779 2015] [wsgi:error] [pid 20639] [remote 127.0.0.1:51896] KeyError: 'RDS_DB_NAME'
If I activate my env and look into the env vars:
(myenv)[bs#debian-gohan:]/var/www/vhosts/mysite $ env | grep RDS
RDS_HOSTNAME=localhost
... and I get the others as well...
My Apache Vhost is:
<VirtualHost *:80>
WSGIDaemonProcess mysite python-path=/home/bs/envs/myenv/bin/python2.7:/home/bs/envs/myenv/lib/python2.7/site-packages
WSGIProcessGroup mysite
WSGIScriptAlias / /var/www/vhosts/main-page/mysite/wsgi.py
Does anyone have an idea why the environ vars are not seen by the apache ?
EDIT 1:
I have tried to use SetEnv in the Apache config, like this:
SetEnv RDS_HOSTNAME "localhost"
Unfortunately, this did not work. I then removed the ProcessGroup and only used a simple WSGIPythonPath. That did not work either.
The error in the apache log remains the same:
[Thu Mar 26 11:30:34.046807 2015] [wsgi:info] [pid 23012] [client 127.0.0.1:55231] mod_wsgi (pid=23012, process='', application='mysite.dbz.dev|'): Loading WSGI script '/var/www/vhosts/main-page/mysite/wsgi.py'.
[Thu Mar 26 11:30:34.178717 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] mod_wsgi (pid=23012): Target WSGI script '/var/www/vhosts/main-page/mysite/wsgi.py' cannot be loaded as Python module.
[Thu Mar 26 11:30:34.178812 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] mod_wsgi (pid=23012): Exception occurred processing WSGI script '/var/www/vhosts/main-page/mysite/wsgi.py'.
[Thu Mar 26 11:30:34.178924 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] Traceback (most recent call last):
[Thu Mar 26 11:30:34.178947 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] File "/var/www/vhosts/main-page/mysite/wsgi.py", line 17, in <module>
[Thu Mar 26 11:30:34.179111 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] application = get_wsgi_application()
[Thu Mar 26 11:30:34.179157 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] File "/usr/local/lib/python2.7/dist-packages/django/core/wsgi.py", line 14, in get_wsgi_application
[Thu Mar 26 11:30:34.179319 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] django.setup()
[Thu Mar 26 11:30:34.179363 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] File "/usr/local/lib/python2.7/dist-packages/django/__init__.py", line 20, in setup
[Thu Mar 26 11:30:34.179606 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
[Thu Mar 26 11:30:34.179651 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 46, in __getattr__
[Thu Mar 26 11:30:34.179774 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] self._setup(name)
[Thu Mar 26 11:30:34.179789 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 42, in _setup
[Thu Mar 26 11:30:34.179918 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] self._wrapped = Settings(settings_module)
[Thu Mar 26 11:30:34.179959 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 94, in __init__
[Thu Mar 26 11:30:34.180072 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] mod = importlib.import_module(self.SETTINGS_MODULE)
[Thu Mar 26 11:30:34.180112 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
[Thu Mar 26 11:30:34.180220 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] __import__(name)
[Thu Mar 26 11:30:34.180246 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] File "/var/www/vhosts/main-page/mysite/../mysite/settings.py", line 94, in <module>
[Thu Mar 26 11:30:34.180400 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] 'NAME': os.environ['RDS_DB_NAME'],
[Thu Mar 26 11:30:34.180443 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] File "/usr/lib/python2.7/UserDict.py", line 23, in __getitem__
[Thu Mar 26 11:30:34.180613 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] raise KeyError(key)
[Thu Mar 26 11:30:34.180665 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] KeyError: 'RDS_DB_NAME'
Set like this
<VirtualHost hostname:80>
...
SetEnv VARIABLE_NAME variable_value
...
</VirtualHost>
In wsgi script.
os.environ.setdefault("variable", "value")
To override variable,
os.environ['var']="val"