Target WSGI script cannot be loaded as a python module - python

I have been trying to get my django app deployed for a while now and no matter what I have followed and tried I keep running into errors.
I am currently getting this error:
Target WSGI script '/home/brickmane/djangoapp/myserver/backend/core/wsgi.py' cannot be loaded as Python module.
and
ModuleNotFoundError: No module named 'contextvars'
which is not something I used myself in my project.
I have tried some solutions like using:
sudo chmod a+x wsgi.py
and trying libapache2-mod-wsgi-py3 vs libapache2-mod-wsgi
my apache2.conf file where I placed my virtual host and the settings for my app:
WSGIPythonHome /home/brickmane/djangoapp/myserver/venv
WSGIPythonPath /home/brickmane/djangoapp/myserver/backend
<VirtualHost *:80>
ServerAlias www.d8pricecheck.tk
WSGIProcessGroup backend
Alias /static/ /home/brickmane/djangoapp/myserver/backend/static/
<Directory /home/brickmane/djangoapp/myserver/backend/static>
Require all granted
</Directory>
WSGIScriptAlias / /home/brickmane/djangoapp/myserver/backend/core/wsgi.py
<Directory /home/brickmane/djangoapp/myserver/backend/core>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess backend python-path=/home/brickmane/djangoapp/myserver/backend/core python-home=/home/brickmane/djangoapp/myserver/venv
WSGIProcessGroup backend
</VirtualHost>
my wsgi.py file:
import os, sys
# add the hellodjango project path into the sys.path
sys.path.append('/home/brickmane/djangoapp/myserver/backend/')
sys.path.append('/home/brickmane/djangoapp/myserver/backend/core/')
# add the virtualenv site-packages path to the sys.path
sys.path.append('/home/brickmane/djangoapp/myserver/venv/lib/python3.10/site-packages')
# poiting to the project settings
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "core.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
This is my full apache error log from restarting the service and trying to access my website:
[Mon Jan 31 12:28:54.228176 2022] [mpm_prefork:notice] [pid 19551] AH00173: SIGHUP received. Attempting to restart
[Mon Jan 31 12:28:54.292016 2022] [ssl:warn] [pid 19551] AH01916: Init: (brickmaneserver.comcast.net:443) You configured HTTP(80) on the standard HTTPS(443) port!
[Mon Jan 31 12:28:54.296542 2022] [mpm_prefork:notice] [pid 19551] AH00163: Apache/2.4.29 (Ubuntu) OpenSSL/1.1.1g mod_wsgi/4.5.17 Python/3.6 configured -- resuming normal operations
[Mon Jan 31 12:28:54.296588 2022] [core:notice] [pid 19551] AH00094: Command line: '/usr/sbin/apache2'
[Mon Jan 31 12:29:00.082063 2022] [wsgi:error] [pid 19618] [remote 108.162.238.175:59436] mod_wsgi (pid=19618): Target WSGI script '/home/brickmane/djangoapp/myserver/backend/core/wsgi.py' cannot be loaded as Python module.
[Mon Jan 31 12:29:00.082120 2022] [wsgi:error] [pid 19618] [remote 108.162.238.175:59436] mod_wsgi (pid=19618): Exception occurred processing WSGI script '/home/brickmane/djangoapp/myserver/backend/core/wsgi.py'.
[Mon Jan 31 12:29:00.082830 2022] [wsgi:error] [pid 19618] [remote 108.162.238.175:59436] Traceback (most recent call last):
[Mon Jan 31 12:29:00.082874 2022] [wsgi:error] [pid 19618] [remote 108.162.238.175:59436] File "/home/brickmane/djangoapp/myserver/backend/core/wsgi.py", line 12, in <module>
[Mon Jan 31 12:29:00.082880 2022] [wsgi:error] [pid 19618] [remote 108.162.238.175:59436] from django.core.wsgi import get_wsgi_application
[Mon Jan 31 12:29:00.082890 2022] [wsgi:error] [pid 19618] [remote 108.162.238.175:59436] File "/home/brickmane/djangoapp/myserver/venv/lib/python3.10/site-packages/django/core/wsgi.py", line 2, in <module>
[Mon Jan 31 12:29:00.082895 2022] [wsgi:error] [pid 19618] [remote 108.162.238.175:59436] from django.core.handlers.wsgi import WSGIHandler
[Mon Jan 31 12:29:00.082920 2022] [wsgi:error] [pid 19618] [remote 108.162.238.175:59436] File "/home/brickmane/djangoapp/myserver/venv/lib/python3.10/site-packages/django/core/handlers/wsgi.py", line 3, in <module>
[Mon Jan 31 12:29:00.082925 2022] [wsgi:error] [pid 19618] [remote 108.162.238.175:59436] from django.conf import settings
[Mon Jan 31 12:29:00.082933 2022] [wsgi:error] [pid 19618] [remote 108.162.238.175:59436] File "/home/brickmane/djangoapp/myserver/venv/lib/python3.10/site-packages/django/conf/__init__.py", line 19, in <module>
[Mon Jan 31 12:29:00.082938 2022] [wsgi:error] [pid 19618] [remote 108.162.238.175:59436] from django.utils.deprecation import RemovedInDjango50Warning
[Mon Jan 31 12:29:00.082946 2022] [wsgi:error] [pid 19618] [remote 108.162.238.175:59436] File "/home/brickmane/djangoapp/myserver/venv/lib/python3.10/site-packages/django/utils/deprecation.py", line 5, in <module>
[Mon Jan 31 12:29:00.082951 2022] [wsgi:error] [pid 19618] [remote 108.162.238.175:59436] from asgiref.sync import sync_to_async
[Mon Jan 31 12:29:00.082959 2022] [wsgi:error] [pid 19618] [remote 108.162.238.175:59436] File "/home/brickmane/djangoapp/myserver/venv/lib/python3.10/site-packages/asgiref/sync.py", line 2, in <module>
[Mon Jan 31 12:29:00.082963 2022] [wsgi:error] [pid 19618] [remote 108.162.238.175:59436] import contextvars
[Mon Jan 31 12:29:00.082981 2022] [wsgi:error] [pid 19618] [remote 108.162.238.175:59436] ModuleNotFoundError: No module named 'contextvars'
[Mon Jan 31 12:29:00.343820 2022] [wsgi:error] [pid 19618] [remote 162.158.187.159:38614] mod_wsgi (pid=19618): Target WSGI script '/home/brickmane/djangoapp/myserver/backend/core/wsgi.py' cannot be loaded as Python module.
[Mon Jan 31 12:29:00.343880 2022] [wsgi:error] [pid 19618] [remote 162.158.187.159:38614] mod_wsgi (pid=19618): Exception occurred processing WSGI script '/home/brickmane/djangoapp/myserver/backend/core/wsgi.py'.
[Mon Jan 31 12:29:00.344018 2022] [wsgi:error] [pid 19618] [remote 162.158.187.159:38614] Traceback (most recent call last):
[Mon Jan 31 12:29:00.344063 2022] [wsgi:error] [pid 19618] [remote 162.158.187.159:38614] File "/home/brickmane/djangoapp/myserver/backend/core/wsgi.py", line 12, in <module>
[Mon Jan 31 12:29:00.344069 2022] [wsgi:error] [pid 19618] [remote 162.158.187.159:38614] from django.core.wsgi import get_wsgi_application
[Mon Jan 31 12:29:00.344079 2022] [wsgi:error] [pid 19618] [remote 162.158.187.159:38614] File "/home/brickmane/djangoapp/myserver/venv/lib/python3.10/site-packages/django/core/wsgi.py", line 2, in <module>
[Mon Jan 31 12:29:00.344084 2022] [wsgi:error] [pid 19618] [remote 162.158.187.159:38614] from django.core.handlers.wsgi import WSGIHandler
[Mon Jan 31 12:29:00.344092 2022] [wsgi:error] [pid 19618] [remote 162.158.187.159:38614] File "/home/brickmane/djangoapp/myserver/venv/lib/python3.10/site-packages/django/core/handlers/wsgi.py", line 3, in <module>
[Mon Jan 31 12:29:00.344097 2022] [wsgi:error] [pid 19618] [remote 162.158.187.159:38614] from django.conf import settings
[Mon Jan 31 12:29:00.344105 2022] [wsgi:error] [pid 19618] [remote 162.158.187.159:38614] File "/home/brickmane/djangoapp/myserver/venv/lib/python3.10/site-packages/django/conf/__init__.py", line 19, in <module>
[Mon Jan 31 12:29:00.344109 2022] [wsgi:error] [pid 19618] [remote 162.158.187.159:38614] from django.utils.deprecation import RemovedInDjango50Warning
[Mon Jan 31 12:29:00.344117 2022] [wsgi:error] [pid 19618] [remote 162.158.187.159:38614] File "/home/brickmane/djangoapp/myserver/venv/lib/python3.10/site-packages/django/utils/deprecation.py", line 5, in <module>
[Mon Jan 31 12:29:00.344122 2022] [wsgi:error] [pid 19618] [remote 162.158.187.159:38614] from asgiref.sync import sync_to_async
[Mon Jan 31 12:29:00.344130 2022] [wsgi:error] [pid 19618] [remote 162.158.187.159:38614] File "/home/brickmane/djangoapp/myserver/venv/lib/python3.10/site-packages/asgiref/sync.py", line 2, in <module>
[Mon Jan 31 12:29:00.344135 2022] [wsgi:error] [pid 19618] [remote 162.158.187.159:38614] import contextvars
[Mon Jan 31 12:29:00.344164 2022] [wsgi:error] [pid 19618] [remote 162.158.187.159:38614] ModuleNotFoundError: No module named 'contextvars'

You have configured Apache with python3.6 but run python3.10 in your virt.env (contextvars is part of python since version 3.7)
You should always run Apache with the same python version as in the virt.env

Related

Permission denied when importing paramiko from flask app -apache wsgi mod

I've been trying to deploy my flask app using apache as a server(wsgi_mod). I created all the necessary files (.wsgi and .conf file for apache).
But when i request the page i get a 500 error. Checking the logs shows a
[Fri Jul 05 12:39:18.746453 2019] [wsgi:error] [pid 74305] [remote 10.1.1.50:41450] mod_wsgi (pid=74305): Target WSGI script '/var/www/html/ExampleFlask/ExampleFlask/my_flask_app.wsgi' cannot be loaded as Python module.
[Fri Jul 05 12:39:18.746912 2019] [wsgi:error] [pid 74305] [remote 10.1.1.50:41450] mod_wsgi (pid=74305): Exception occurred processing WSGI script '/var/www/html/ExampleFlask/ExampleFlask/my_flask_app.wsgi'.
[Fri Jul 05 12:39:18.750486 2019] [wsgi:error] [pid 74305] [remote 10.1.1.50:41450] Traceback (most recent call last):
[Fri Jul 05 12:39:18.750808 2019] [wsgi:error] [pid 74305] [remote 10.1.1.50:41450] File "/var/www/html/ExampleFlask/ExampleFlask/my_flask_app.wsgi", line 6, in <module>
[Fri Jul 05 12:39:18.750995 2019] [wsgi:error] [pid 74305] [remote 10.1.1.50:41450] from my_flask_app import app as application
[Fri Jul 05 12:39:18.751224 2019] [wsgi:error] [pid 74305] [remote 10.1.1.50:41450] File "/var/www/html/ExampleFlask/ExampleFlask/my_flask_app.py", line 2, in <module>
[Fri Jul 05 12:39:18.751411 2019] [wsgi:error] [pid 74305] [remote 10.1.1.50:41450] import paramiko
[Fri Jul 05 12:39:18.751600 2019] [wsgi:error] [pid 74305] [remote 10.1.1.50:41450] File "/usr/lib/python3/dist-packages/paramiko/__init__.py", line 30, in <module>
[Fri Jul 05 12:39:18.751819 2019] [wsgi:error] [pid 74305] [remote 10.1.1.50:41450] from paramiko.transport import SecurityOptions, Transport
[Fri Jul 05 12:39:18.752023 2019] [wsgi:error] [pid 74305] [remote 10.1.1.50:41450] File "/usr/lib/python3/dist-packages/paramiko/transport.py", line 32, in <module>
[Fri Jul 05 12:39:18.752224 2019] [wsgi:error] [pid 74305] [remote 10.1.1.50:41450] from cryptography.hazmat.backends import default_backend
[Fri Jul 05 12:39:18.752446 2019] [wsgi:error] [pid 74305] [remote 10.1.1.50:41450] File "/usr/lib/python3/dist-packages/cryptography/hazmat/backends/__init__.py", line 7, in <module>
[Fri Jul 05 12:39:18.752625 2019] [wsgi:error] [pid 74305] [remote 10.1.1.50:41450] import pkg_resources
[Fri Jul 05 12:39:18.752834 2019] [wsgi:error] [pid 74305] [remote 10.1.1.50:41450] File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 3019, in <module>
[Fri Jul 05 12:39:18.753009 2019] [wsgi:error] [pid 74305] [remote 10.1.1.50:41450] #_call_aside
[Fri Jul 05 12:39:18.753215 2019] [wsgi:error] [pid 74305] [remote 10.1.1.50:41450] File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 3003, in _call_aside
[Fri Jul 05 12:39:18.753405 2019] [wsgi:error] [pid 74305] [remote 10.1.1.50:41450] f(*args, **kwargs)
[Fri Jul 05 12:39:18.753597 2019] [wsgi:error] [pid 74305] [remote 10.1.1.50:41450] File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 3032, in _initialize_master_working_set
[Fri Jul 05 12:39:18.753806 2019] [wsgi:error] [pid 74305] [remote 10.1.1.50:41450] working_set = WorkingSet._build_master()
[Fri Jul 05 12:39:18.754000 2019] [wsgi:error] [pid 74305] [remote 10.1.1.50:41450] File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 646, in _build_master
[Fri Jul 05 12:39:18.754175 2019] [wsgi:error] [pid 74305] [remote 10.1.1.50:41450] ws = cls()
[Fri Jul 05 12:39:18.754390 2019] [wsgi:error] [pid 74305] [remote 10.1.1.50:41450] File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 639, in __init__
[Fri Jul 05 12:39:18.754590 2019] [wsgi:error] [pid 74305] [remote 10.1.1.50:41450] self.add_entry(entry)
[Fri Jul 05 12:39:18.754782 2019] [wsgi:error] [pid 74305] [remote 10.1.1.50:41450] File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 695, in add_entry
[Fri Jul 05 12:39:18.754981 2019] [wsgi:error] [pid 74305] [remote 10.1.1.50:41450] for dist in find_distributions(entry, True):
[Fri Jul 05 12:39:18.755178 2019] [wsgi:error] [pid 74305] [remote 10.1.1.50:41450] File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2012, in find_on_path
[Fri Jul 05 12:39:18.755382 2019] [wsgi:error] [pid 74305] [remote 10.1.1.50:41450] if len(os.listdir(fullpath)) == 0:
[Fri Jul 05 12:39:18.755643 2019] [wsgi:error] [pid 74305] [remote 10.1.1.50:41450] PermissionError: [Errno 13] Permission denied: '/usr/local/lib/python3.5/dist-packages/Werkzeug-0.15.4.dist-info'
I'm trying to deploy this application in a debian 9 server. I've tested it on my kali box and it worked fine just by installing flask and paramiko and configuring apache.
I've tried rinstalling some packages like paramiko, flask and Werkzeug but with no success.
I even created a new app with a simple hello world message. It works fine until i import paramiko and then i get the "permission denined" error.
My test app contains three files.
my_flask_app.py
from flask import Flask
import paramiko
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello world!"
if __name__ == "__main__":
app.run()
my_flask_app.wsgi
import logging
import sys
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0, '/var/www/html/ExampleFlask/ExampleFlask/')
from my_flask_app import app as application
application.secret_key = 'thisisasecretkey'
my_flask_app.conf
<VirtualHost *:80>
# Add machine's IP address (use ifconfig command)
ServerName 10.1.1.50
# Give an alias to to start your website url with
WSGIScriptAlias /testFlask /var/www/html/ExampleFlask/ExampleFlask/my_flask_app.wsgi
WSGIDaemonProcess my_flask_app user=www-data group=www-data threads=5
WSGIProcessGroup my_flask_app
#WSGIScriptAlias /testFlask /var/www/html/benchmarkerPHP/benchmarkerPHP.wsgi
#<Directory /var/www/html/benchmarkerPHP/>
<Directory /var/www/html/ExampleFlask/ExampleFlask/>
# set permissions as per apache2.conf file
Options FollowSymLinks
AllowOverride None
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
The apache user didn't have the needed permission to read from that directory. We only to add the read permission for the other group with the following command :
chmod o+r /usr/local/lib/python3.5/dist-packages/*

Django populate() isn't reentrant CANNOT BE RESLOVED

I keep getting this when I try to load my Django application on production . I tried all the stackoverflow answers but nothing has fixed it. Any other ideas.
Here is my wsgi.py
import os
import time
import traceback
import signal
import sys
from django.core.wsgi import get_wsgi_application
try:
application = get_wsgi_application()
print 'WSGI without exception'
except Exception:
print 'handling WSGI exception'
# Error loading applications
if 'mod_wsgi' in sys.modules:
traceback.print_exc()
os.kill(os.getpid(), signal.SIGINT)
time.sleep(2.5)
and What I got
[Mon May 27 18:09:41.654452 2019] [wsgi:error] [pid 15704:tid 1300] handling WSGI exception\r
[Mon May 27 18:09:41.655450 2019] [wsgi:error] [pid 15704:tid 1300] Traceback (most recent call last):\r
[Mon May 27 18:09:41.655450 2019] [wsgi:error] [pid 15704:tid 1300] File "D:/soft_design/project_soft/project_soft/wsgi.py", line 26, in <module>\r
[Mon May 27 18:09:41.655450 2019] [wsgi:error] [pid 15704:tid 1300] application = get_wsgi_application()\r
[Mon May 27 18:09:41.655450 2019] [wsgi:error] [pid 15704:tid 1300] File "C:\\Users\\Scott\\Anaconda3\\envs\\py37\\lib\\site-packages\\django\\core\\wsgi.py", line 12, in get_wsgi_application\r
[Mon May 27 18:09:41.655450 2019] [wsgi:error] [pid 15704:tid 1300] django.setup(set_prefix=False)\r
[Mon May 27 18:09:41.655450 2019] [wsgi:error] [pid 15704:tid 1300] File "C:\\Users\\Scott\\Anaconda3\\envs\\py37\\lib\\site-packages\\django\\__init__.py", line 24, in setup\r
[Mon May 27 18:09:41.655450 2019] [wsgi:error] [pid 15704:tid 1300] apps.populate(settings.INSTALLED_APPS)\r
[Mon May 27 18:09:41.655450 2019] [wsgi:error] [pid 15704:tid 1300] File "C:\\Users\\Scott\\Anaconda3\\envs\\py37\\lib\\site-packages\\django\\apps\\registry.py", line 83, in populate\r
[Mon May 27 18:09:41.655450 2019] [wsgi:error] [pid 15704:tid 1300] raise RuntimeError("populate() isn't reentrant")\r
[Mon May 27 18:09:41.655450 2019] [wsgi:error] [pid 15704:tid 1300] RuntimeError: populate() isn't reentrant\r
[Mon May 27 18:09:41.676394 2019] [mpm_winnt:crit] [pid 3488:tid 620] AH00427: Parent: child process 15704 exited with status 2 -- Aborting.

pgAdmin4 install - Target WSGI script cannot be loaded as Python module

This question has been asked before here but the solution does not work for me. My python venv configuration is not playing well with the apache2 mod_wsgi and I cannot root-cause the issue. At present, I cannot comment on this question so I'm posting another one
Here are the apache2 error logs when trying to log into the site.
[Fri Dec 07 20:32:25.476491 2018] [wsgi:warn] [pid 25982:tid 140573456193408] mod_wsgi: Compiled for Python/3.5.1+.
[Fri Dec 07 20:32:25.476529 2018] [wsgi:warn] [pid 25982:tid 140573456193408] mod_wsgi: Runtime using Python/3.5.2.
[Fri Dec 07 20:32:25.477326 2018] [mpm_event:notice] [pid 25982:tid 140573456193408] AH00489: Apache/2.4.18 (Ubuntu) mod_wsgi/4.3.0 Python/3.5.2 configured -- resuming normal operations
[Fri Dec 07 20:32:25.477362 2018] [core:notice] [pid 25982:tid 140573456193408] AH00094: Command line: '/usr/sbin/apache2'
[Fri Dec 07 20:34:24.401510 2018] [wsgi:error] [pid 25983:tid 140573343934208] [remote 10.2.5.243:15047] mod_wsgi (pid=25983): Target WSGI script '/home/awashburn/anaconda3/envs/pgadmin4/lib/python3.7/site-packages/pgadmin4/pgAdmin4.wsgi' cannot be loaded as Python module.
[Fri Dec 07 20:34:24.401642 2018] [wsgi:error] [pid 25983:tid 140573343934208] [remote 10.2.5.243:15047] mod_wsgi (pid=25983): Exception occurred processing WSGI script '/home/awashburn/anaconda3/envs/pgadmin4/lib/python3.7/site-packages/pgadmin4/pgAdmin4.wsgi'.
[Fri Dec 07 20:34:24.402195 2018] [wsgi:error] [pid 25983:tid 140573343934208] [remote 10.2.5.243:15047] Traceback (most recent call last):
[Fri Dec 07 20:34:24.402229 2018] [wsgi:error] [pid 25983:tid 140573343934208] [remote 10.2.5.243:15047] File "/home/awashburn/anaconda3/envs/pgadmin4/lib/python3.7/site-packages/pgadmin4/pgAdmin4.wsgi", line 25, in <module>
[Fri Dec 07 20:34:24.402236 2018] [wsgi:error] [pid 25983:tid 140573343934208] [remote 10.2.5.243:15047] import config
[Fri Dec 07 20:34:24.402244 2018] [wsgi:error] [pid 25983:tid 140573343934208] [remote 10.2.5.243:15047] File "/home/awashburn/anaconda3/envs/pgadmin4/lib/python3.7/site-packages/pgadmin4/config.py", line 29, in <module>
[Fri Dec 07 20:34:24.402248 2018] [wsgi:error] [pid 25983:tid 140573343934208] [remote 10.2.5.243:15047] from pgadmin.utils import env, IS_PY2, IS_WIN, fs_short_path
[Fri Dec 07 20:34:24.402256 2018] [wsgi:error] [pid 25983:tid 140573343934208] [remote 10.2.5.243:15047] File "/home/awashburn/anaconda3/envs/pgadmin4/lib/python3.7/site-packages/pgadmin4/pgadmin/__init__.py", line 18, in <module>
[Fri Dec 07 20:34:24.402260 2018] [wsgi:error] [pid 25983:tid 140573343934208] [remote 10.2.5.243:15047] from flask import Flask, abort, request, current_app, session, url_for
[Fri Dec 07 20:34:24.402280 2018] [wsgi:error] [pid 25983:tid 140573343934208] [remote 10.2.5.243:15047] ImportError: No module named 'flask'
Here is the Vhost configuration:
<VirtualHost *>
ServerName shinyapp01t
WSGIDaemonProcess pgadmin processes=1 threads=25 python-home=/home/awashburn/anaconda3/envs/pgadmin4 display-name=pgadmin4
WSGIScriptAlias /pgadmin /home/awashburn/anaconda3/envs/pgadmin4/lib/python3.7/site-packages/pgadmin4/pgAdmin4.wsgi
<Directory "/home/awashburn/anaconda3/envs/pgadmin4/lib/python3.7/site-packages/pgadmin4/">
WSGIProcessGroup pgadmin
WSGIApplicationGroup %{GLOBAL}
Require all granted
</Directory>
</VirtualHost>
I referenced these instructions from DigitalOcean trying to install pgAdmin4 on Ubuntu 16.04.
Edit: I used a python3 venv instead of anaconda virtual env and everything worked fine. So the root cause is the anaconda system. There's a discussion on the mod_wsgi mailing list about this

Flask app deployment - ImportError: No module named 'app'

Strange one - I've tried and tried to get this Flask app going - something seems to be going wrong.
$ pwd inside the Flask app gives:
/home/winduplordvexxos/winduplordvexxos.com/windupflaskvexxos
$ ls inside the Flask app gives:
__init__.py __pycache__ vexxos.py windupflaskvexxos.wsgi
__init__.py is empty.
vexxos.py:
from flask import Flask
app = Flask(__name__)
#app.route('/')
def api():
return 'Wind Up Lord Vexxos API'
if __name__ == "__main__":
app.run(host='0.0.0.0')
windupflaskvexxos.wsgi:
activate_this = '/home/winduplordvexxos/winduplordvexxos.com/winduplordvexxos_env/bin/activate_this.py'
with open(activate_this) as file_:
exec(file_.read(), dict(__file__=activate_this))
import os, sys
cwd = os.getcwd()
sys.path.insert(0, cwd)
try:
from .vexxos import app as application
except Exception: #ImportError
from vexxos import app as application
However, all I see on the front end is:
/etc/apache2/site-available/000-default.conf:
<VirtualHost *:80>
ServerAlias flask.winduplordvexxos.com
ServerAdmin admin#winduplordvexxos.com
WSGIDaemonProcess windupflaskvexxos.com user=winduplordvexxos group=winduplordvexxos threads=5
WSGIScriptAlias / /home/winduplordvexxos/winduplordvexxos.com/windupflaskvexxos/windupflaskvexxos.wsgi
<Directory /home/winduplordvexxos/winduplordvexxos.com/windupflaskvexxos/>
WSGIProcessGroup windupflaskvexxos.com
WSGIApplicationGroup %{GLOBAL}
WSGIScriptReloading On
Require all granted
</Directory>
</VirtualHost>
Checking the apache error logs has given me:
[Fri Oct 13 23:05:14.650945 2017] [wsgi:error] [pid 15561:tid 140103054321408] [remote 109.149.163.9:59859] mod_wsgi (pid=15561): Target WSGI script '/home/winduplordve$
[Fri Oct 13 23:05:14.651088 2017] [wsgi:error] [pid 15561:tid 140103054321408] [remote 109.149.163.9:59859] mod_wsgi (pid=15561): Exception occurred processing WSGI scr$
[Fri Oct 13 23:05:14.657996 2017] [wsgi:error] [pid 15561:tid 140103054321408] [remote 109.149.163.9:59859] Traceback (most recent call last):
[Fri Oct 13 23:05:14.658060 2017] [wsgi:error] [pid 15561:tid 140103054321408] [remote 109.149.163.9:59859] File "/home/winduplordvexxos/winduplordvexxos.com/windupfl$
[Fri Oct 13 23:05:14.658071 2017] [wsgi:error] [pid 15561:tid 140103054321408] [remote 109.149.163.9:59859] from .vexxos import app as application
[Fri Oct 13 23:05:14.658098 2017] [wsgi:error] [pid 15561:tid 140103054321408] [remote 109.149.163.9:59859] SystemError: Parent module '' not loaded, cannot perform rel$
[Fri Oct 13 23:05:14.658131 2017] [wsgi:error] [pid 15561:tid 140103054321408] [remote 109.149.163.9:59859]
[Fri Oct 13 23:05:14.658146 2017] [wsgi:error] [pid 15561:tid 140103054321408] [remote 109.149.163.9:59859] During handling of the above exception, another exception oc$
[Fri Oct 13 23:05:14.658156 2017] [wsgi:error] [pid 15561:tid 140103054321408] [remote 109.149.163.9:59859]
[Fri Oct 13 23:05:14.658180 2017] [wsgi:error] [pid 15561:tid 140103054321408] [remote 109.149.163.9:59859] Traceback (most recent call last):
[Fri Oct 13 23:05:14.658229 2017] [wsgi:error] [pid 15561:tid 140103054321408] [remote 109.149.163.9:59859] File "/home/winduplordvexxos/winduplordvexxos.com/windupfl$
[Fri Oct 13 23:05:14.658248 2017] [wsgi:error] [pid 15561:tid 140103054321408] [remote 109.149.163.9:59859] from vexxos import app as application
[Fri Oct 13 23:05:14.658286 2017] [wsgi:error] [pid 15561:tid 140103054321408] [remote 109.149.163.9:59859] ImportError: No module named 'vexxos'
[Fri Oct 13 23:05:14.690252 2017] [wsgi:error] [pid 15561:tid 140103087892224] [remote 109.149.163.9:59858] mod_wsgi (pid=15561): Target WSGI script '/home/winduplordve$
[Fri Oct 13 23:05:14.690318 2017] [wsgi:error] [pid 15561:tid 140103087892224] [remote 109.149.163.9:59858] mod_wsgi (pid=15561): Exception occurred processing WSGI scr$
[Fri Oct 13 23:05:14.690468 2017] [wsgi:error] [pid 15561:tid 140103087892224] [remote 109.149.163.9:59858] Traceback (most recent call last):
[Fri Oct 13 23:05:14.690515 2017] [wsgi:error] [pid 15561:tid 140103087892224] [remote 109.149.163.9:59858] File "/home/winduplordvexxos/winduplordvexxos.com/windupfl$
[Fri Oct 13 23:05:14.690525 2017] [wsgi:error] [pid 15561:tid 140103087892224] [remote 109.149.163.9:59858] from .vexxos import app as application
[Fri Oct 13 23:05:14.690549 2017] [wsgi:error] [pid 15561:tid 140103087892224] [remote 109.149.163.9:59858] SystemError: Parent module '' not loaded, cannot perform rel$
[Fri Oct 13 23:05:14.690566 2017] [wsgi:error] [pid 15561:tid 140103087892224] [remote 109.149.163.9:59858]
[Fri Oct 13 23:05:14.690573 2017] [wsgi:error] [pid 15561:tid 140103087892224] [remote 109.149.163.9:59858] During handling of the above exception, another exception oc$
[Fri Oct 13 23:05:14.690577 2017] [wsgi:error] [pid 15561:tid 140103087892224] [remote 109.149.163.9:59858]
[Fri Oct 13 23:05:14.690589 2017] [wsgi:error] [pid 15561:tid 140103087892224] [remote 109.149.163.9:59858] Traceback (most recent call last):
[Fri Oct 13 23:05:14.690610 2017] [wsgi:error] [pid 15561:tid 140103087892224] [remote 109.149.163.9:59858] File "/home/winduplordvexxos/winduplordvexxos.com/windupfl$
[Fri Oct 13 23:05:14.690619 2017] [wsgi:error] [pid 15561:tid 140103087892224] [remote 109.149.163.9:59858] from vexxos import app as application
[Fri Oct 13 23:05:14.690637 2017] [wsgi:error] [pid 15561:tid 140103087892224] [remote 109.149.163.9:59858] ImportError: No module named 'vexxos'
I've tried a number of things, try except on the module import etc but nothing seems to work...can anyone help with this?
You likely need to tell mod_wsgi where your code is using the python-path option to WSGIDaemonProcess. Your import in the WSGI script file also looks wrong.
For python-path you likely want:
python-path=/home/winduplordvexxos/winduplordvexxos.com
The import should then be:
from windupflaskvexxos import app as application
or if app not pulled in by __init__.py, use:
from windupflaskvexxos.vexxos import app as application
Also read up on the preferred way of setting up a virtual environment with mod_wsgi. The way you are using is not the recommended way.
http://modwsgi.readthedocs.io/en/develop/user-guides/virtual-environments.html

How to set environ variables in local development with virtualenv

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"

Categories