|--------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/
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 have a python flask app hosted on ubuntu-apache with WSGI-mod. It works perfectly on my local windows machine, but on the ubuntu machine I cannot seem to access './static/images/' through my python script? Everything else works, but this exact piece of code does not for some reason and produces this error.
I've tried searching around and editing the VirtualHost, but nothing seems to work. Can't wrap my head around the problem. Any ideas?
Code that gives error:
if session.get('logged_in'):
directory = r'./static/images/'
urls = []
for filename in os.listdir(directory):
if filename.endswith(".jpg") or filename.endswith(".png") or filename.endswith(".jpeg"):
urls.append(os.path.join(directory, filename))
else:
continue
Error Message:
[Wed Feb 17 10:13:10.857968 2021] [wsgi:error] [pid 7746:tid 140653174441728] [client IP] [2021-02-17 10:13:10,856] ERROR in app: Exception on /cdn-controlpanel [GET], referer: website-uri
[Wed Feb 17 10:13:10.858012 2021] [wsgi:error] [pid 7746:tid 140653174441728] [client IP] Traceback (most recent call last):, referer: website-uri
[Wed Feb 17 10:13:10.858019 2021] [wsgi:error] [pid 7746:tid 140653174441728] [client IP] File "/usr/local/lib/python3.8/dist-packages/flask/app.py", line 2447, in wsgi_app, referer: website-uri
[Wed Feb 17 10:13:10.858025 2021] [wsgi:error] [pid 7746:tid 140653174441728] [client IP] response = self.full_dispatch_request(), referer: website-uri
[Wed Feb 17 10:13:10.858030 2021] [wsgi:error] [pid 7746:tid 140653174441728] [client IP] File "/usr/local/lib/python3.8/dist-packages/flask/app.py", line 1952, in full_dispatch_request, referer: website-uri>
[Wed Feb 17 10:13:10.858035 2021] [wsgi:error] [pid 7746:tid 140653174441728] [client IP] rv = self.handle_user_exception(e), referer: website-uri
[Wed Feb 17 10:13:10.858040 2021] [wsgi:error] [pid 7746:tid 140653174441728] [client IP] File "/usr/local/lib/python3.8/dist-packages/flask/app.py", line 1821, in handle_user_exception, referer: website-uri>
[Wed Feb 17 10:13:10.858045 2021] [wsgi:error] [pid 7746:tid 140653174441728] [client IP] reraise(exc_type, exc_value, tb), referer: website-uri
[Wed Feb 17 10:13:10.858050 2021] [wsgi:error] [pid 7746:tid 140653174441728] [client IP] File "/usr/local/lib/python3.8/dist-packages/flask/_compat.py", line 39, in reraise, referer: website-uri
[Wed Feb 17 10:13:10.858055 2021] [wsgi:error] [pid 7746:tid 140653174441728] [client IP] raise value, referer: website-uri
[Wed Feb 17 10:13:10.858060 2021] [wsgi:error] [pid 7746:tid 140653174441728] [client IP] File "/usr/local/lib/python3.8/dist-packages/flask/app.py", line 1950, in full_dispatch_request, referer: website-uri>
[Wed Feb 17 10:13:10.858066 2021] [wsgi:error] [pid 7746:tid 140653174441728] [client IP] rv = self.dispatch_request(), referer: website-uri
[Wed Feb 17 10:13:10.858071 2021] [wsgi:error] [pid 7746:tid 140653174441728] [client IP] File "/usr/local/lib/python3.8/dist-packages/flask/app.py", line 1936, in dispatch_request, referer: website-uri>
[Wed Feb 17 10:13:10.858076 2021] [wsgi:error] [pid 7746:tid 140653174441728] [client IP] return self.view_functionsrule.endpoint, referer: website-uri
[Wed Feb 17 10:13:10.858081 2021] [wsgi:error] [pid 7746:tid 140653174441728] [client IP] File "/var/www/FlaskApp/App/app.py", line 99, in cdnControlpanel, referer: website-uri
[Wed Feb 17 10:13:10.858086 2021] [wsgi:error] [pid 7746:tid 140653174441728] [client IP] for filename in os.listdir(directory):, referer: website-uri
[Wed Feb 17 10:13:10.858130 2021] [wsgi:error] [pid 7746:tid 140653174441728] [client IP] FileNotFoundError: [Errno 2] No such file or directory: './static/images/', referer: website-uri
Figured it out. When stating directory-paths in python, the "." does not mean relative to current file, but relative to current working directory which can be changed with os.chdir('path/to/dir')
I'm trying to deploy Django project on Apache and wsgi_mod in Windows 10. What I did so far:
1. Added following in httpd.conf of Apache conf folder
WSGIScriptAlias / "C:/Bitnami/djangostack-1.10.1-0/apps/django/django_projects/project/egeirn/wsgi.py"
WSGIPythonPath "C:/Bitnami/djangostack-1.10.1-0/apps/django/django_projects/project;C:/Bitnami/djangostack-1.10.1-0/python/Lib/site-packages"
<Directory "C:/Bitnami/djangostack-1.10.1-0/apps/django/django_projects/project/egeirn">
<Files wsgi.py>
Require all granted
</Files>
</Directory>
2. Following is the content of my wsgi.py file:
import os
import sys
from django.core.wsgi import get_wsgi_application
sys.path.append('C:/Bitnami/djangostack-1.10.1-0/apps/django/django_projects/project')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "egeirn.settings")
application = get_wsgi_application()
When I start the Apache server, it gives the following error in logs file:
[Thu Nov 03 17:47:11.328063 2016] [:error] [pid 916:tid 1236] [client ::1:63236] mod_wsgi (pid=916): Target WSGI script 'C:/Bitnami/djangostack-1.10.1-0/apps/django/django_projects/project/egeirn/wsgi.py' cannot be loaded as Python module., referer: http://localhost:82/
[Thu Nov 03 17:47:11.328063 2016] [:error] [pid 916:tid 1236] [client ::1:63236] mod_wsgi (pid=916): Exception occurred processing WSGI script 'C:/Bitnami/djangostack-1.10.1-0/apps/django/django_projects/project/egeirn/wsgi.py'., referer: http://localhost:82/
[Thu Nov 03 17:47:11.328063 2016] [:error] [pid 916:tid 1236] [client ::1:63236] Traceback (most recent call last):, referer: http://localhost:82/
[Thu Nov 03 17:47:11.328063 2016] [:error] [pid 916:tid 1236] [client ::1:63236] File "C:/Bitnami/djangostack-1.10.1-0/apps/django/django_projects/project/egeirn/wsgi.py", line 18, in <module>, referer: http://localhost:82/
[Thu Nov 03 17:47:11.328063 2016] [:error] [pid 916:tid 1236] [client ::1:63236] application = get_wsgi_application(), referer: http://localhost:82/
[Thu Nov 03 17:47:11.328063 2016] [:error] [pid 916:tid 1236] [client ::1:63236] File "C:\\Bitnami\\djangostack-1.10.1-0\\apps\\django\\django-1.10.1-py2.7.egg\\django\\core\\wsgi.py", line 13, in get_wsgi_application, referer: http://localhost:82/
[Thu Nov 03 17:47:11.328063 2016] [:error] [pid 916:tid 1236] [client ::1:63236] django.setup(set_prefix=False), referer: http://localhost:82/
[Thu Nov 03 17:47:11.328063 2016] [:error] [pid 916:tid 1236] [client ::1:63236] File "C:\\Bitnami\\djangostack-1.10.1-0\\apps\\django\\django-1.10.1-py2.7.egg\\django\\__init__.py", line 22, in setup, referer: http://localhost:82/
[Thu Nov 03 17:47:11.328063 2016] [:error] [pid 916:tid 1236] [client ::1:63236] configure_logging(settings.LOGGING_CONFIG, settings.LOGGING), referer: http://localhost:82/
[Thu Nov 03 17:47:11.328063 2016] [:error] [pid 916:tid 1236] [client ::1:63236] File "C:\\Bitnami\\djangostack-1.10.1-0\\apps\\django\\django-1.10.1-py2.7.egg\\django\\conf\\__init__.py", line 53, in __getattr__, referer: http://localhost:82/
[Thu Nov 03 17:47:11.328063 2016] [:error] [pid 916:tid 1236] [client ::1:63236] self._setup(name), referer: http://localhost:82/
[Thu Nov 03 17:47:11.328063 2016] [:error] [pid 916:tid 1236] [client ::1:63236] File "C:\\Bitnami\\djangostack-1.10.1-0\\apps\\django\\django-1.10.1-py2.7.egg\\django\\conf\\__init__.py", line 41, in _setup, referer: http://localhost:82/
[Thu Nov 03 17:47:11.328063 2016] [:error] [pid 916:tid 1236] [client ::1:63236] self._wrapped = Settings(settings_module), referer: http://localhost:82/
[Thu Nov 03 17:47:11.328063 2016] [:error] [pid 916:tid 1236] [client ::1:63236] File "C:\\Bitnami\\djangostack-1.10.1-0\\apps\\django\\django-1.10.1-py2.7.egg\\django\\conf\\__init__.py", line 116, in __init__, referer: http://localhost:82/
[Thu Nov 03 17:47:11.328063 2016] [:error] [pid 916:tid 1236] [client ::1:63236] raise ImproperlyConfigured("The SECRET_KEY setting must not be empty."), referer: http://localhost:82/
[Thu Nov 03 17:47:11.328063 2016] [:error] [pid 916:tid 1236] [client ::1:63236] ImproperlyConfigured: The SECRET_KEY setting must not be empty., referer: http://localhost:82/
I am using django-environ to read .env file and keep configurations in environment variables. Although I have my SECRET_KEY in the base.py file
I have used it like this in base.py file:
import environ
# three folder back (/project/egeirn/settings/ - 3 = /)
root = environ.Path(__file__) - 3
# set default values and casting
env = environ.Env(DEBUG=(bool, False),)
# reading .env file
environ.Env.read_env(os.path.join(BASE_DIR, '../../config/.env'))
SITE_ROOT = root()
# Raises ImproperlyConfigured exception if SECRET_KEY not in os.environ
SECRET_KEY = env('SECRET_KEY')
This is my .env file:
SECRET_KEY=<MY_SECRET_KEY_HERE>
Please tell me what I am doing wrong here, how come it cannot read the SECRET_KEY?
Thanks
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')
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"