Python3.8 Flask wsgi on apache2 error: "Fatal Python error: ... No module named 'encodings'" - python-3.x

I am attempting to configure a flask website on Ubuntu 18.04 using python3.8. I am following a few tutorials for configuring everything but I have hit a stopping point when I encounter the following error in /var/log/apache2/error.log
[Sat Feb 08 18:29:08.089321 2020] [wsgi:warn] [pid 16992:tid 140217694870464] (2)No such file or directory: mod_wsgi (pid=16992): Unable to stat Python home Xeplin/venv. Python interpreter may not be able to be initialized correctly. Verify the supplied path and access permissions for whole of the path.
Python path configuration:
PYTHONHOME = 'Xeplin/venv'
PYTHONPATH = (not set)
program name = 'python3'
isolated = 0
environment = 1
user site = 1
import site = 1
sys._base_executable = '/usr/bin/python3'
sys.base_prefix = 'Xeplin/venv'
sys.base_exec_prefix = 'Xeplin/venv'
sys.executable = '/usr/bin/python3'
sys.prefix = 'Xeplin/venv'
sys.exec_prefix = 'Xeplin/venv'
sys.path = [
'Xeplin/venv/lib/python38.zip',
'Xeplin/venv/lib/python3.8',
'Xeplin/venv/lib/python3.8/lib-dynload',
]
Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding
Python runtime state: core initialized
ModuleNotFoundError: No module named 'encodings'
I have found a lot of unique answers for similar situations but none of them seem to eliminate this error.
I have attempted opening all permissions on the virtual env, opening permissions on my entire folder structure, and a few other things that I essentially copy and pasted from various stackoverflow threads.
This is my first time doing all of this so I am not sure what files are relevant for fixing this issue. I will provide a few things here.
tail /etc/apache2/mods-available/wsgi.conf
#The WSGILazyInitialization directives sets whether or not the Python
#interpreter is preinitialised within the Apache parent process or whether
#lazy initialisation is performed, and the Python interpreter only
#initialised in the Apache server processes or mod_wsgi daemon processes
#after they have forked from the Apache parent process.
#WSGILazyInitialization On|Off
</IfModule>
WSGIPythonHome "/home/ubuntu/Xeplin/venv"
(I have added the python home here. Rest of the file is default)
cat /etc/apache2/mods-available/wsgi.load
LoadModule wsgi_module "/usr/lib/apache2/modules/mod_wsgi-py38.cpython-38-x86_64-linux-gnu.so"
If previous debugging efforts may impact my getting of this error, I previously had to install mod_wsgi which failed when my virtual env was active but worked after deactivating. After running python3.8 -m pip install mod_wsgi outside my venv I was able to run pip install mod_wsgi inside the env. I found that peculiar so I am mentioning it here.

Related

Deploying Flask APP with UWSGI,Nginx,direnv and systemd

I have created an API using FLASK which I am trying to deploy on a linux server by creating a systemd service.
I have used direnv to setup input parameters to the app like database connections. Below is what the file looks like :
The uwsgi config is as below :
The systemd file has the following entries:
I get the follwing error in my uwsgi logs whenever I try to reach the service on my browser :
--- no python application found, check your startup logs for errors ---
[pid: 23791|app: -1|req: -1/3] 192.168.9.180 () {44 vars in 719 bytes} [Thu Oct 11 14:35:09 2018] GET / => generated 21 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 83 bytes (1 switches on core 0)
My understanding is the ExecStart command in the systemd file is not able to invoke the direnv set variables , hence i added the ExecStartPre entry but even does not seem to work.
Any hints/ideas are appreciated.
Note: The application is accessible without errors when I run the uwsgi via command line from my python virtual environment :
uwsgi --socket 0.0.0.0:5000 --protocol=http -w app:app
i have a few advises that may help you, probably only the first one is the one you need...
1) Either move all your env variable defined in direnv to the systemd unit as Environment or move them into a special file (similar to the you already have) without the "source activate" line and the export, and then pass that file as EnvironmentFile , this is the doc for that https://www.freedesktop.org/software/systemd/man/systemd.exec.html#Environment
2) Your ExecStartPre does nothing really, even tho you do "cd" into the path, that is lost and is not persistent. you should remove it.
3) By setting your PATH to only that path, you are restricting your self, i would recommend see the value of your current PATH and then set it to that value. but otherwise at least add "/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin". now spoiler alert, you probably dont need to set it.
4) put the socket in /run//socket.socket directory and let systemd manage your /run/<yourapp> with RuntimeDirectory directive.
good luck!

python3 Error with opening csv file when running app with gunicorn

When running my app with a gunicorn upstart, I get:
TypeError: 'newline' is an invalid keyword argument for this function
When I run it from the command line, however, I have no problem.
I've seen solutions that indicate newline should be in the file opening, not with the csv.writer. As you can see though, I do indeed have it in the file opening.
To recreate:
save my_app.py to /home/--your home--/
chmod u+x /home/--your home--/my_app.py
save my_upstart.conf to /etc/init/
edit my_upstart.conf to replace with your home dir
sudo service my_upstart start
curl localhost:5001/vis -H "Content-Type: text/csv"
sudo cat /var/log/upstart/my_upstart.log
In my_upstart.log, you will see theTypeError mentioned above
my_app.py
#!/usr/bin/python3
from flask import Flask, request
app = Flask(__name__)
#app.route('/vis/', strict_slashes=False)
def vis():
with (open('~/test.csv', mode='w', newline='')) as f:
writer = csv.writer(f)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5001)
my_upstart.conf
description "Gunicorn config file for serving the Wellness app"
start on runlevel [2345]
stop on runlevel [!2345]
respawn
setuid ubuntu
setgid ubuntu
script
cd /home/<your home>/
exec gunicorn --bind 0.0.0.0:5001 my_app:app
end script
Compare the documentation of open for Python version 2 and 3 and you will note that there is quite a bit of difference in what parameters can be passed. In particular the parameter newline is not available in Python 2.
So my guess is that when gunicorn runs it will pick up a version 2 Python executable.
See Cannot get gunicorn to use Python 3 for more details.
gunicorn was using python 2 and it's corresponding distribution package, wheras I'm using python 3. Followed these steps to fix:
sudo pip3 install gunicorn
in /usr/bin/gunicorn,
edited first line to read #!/usr/bin/python3 (instead of python) and
changed the gunicorn version anywhere it appeared to match what gunicorn --version says.

OpenMPI: ORTE was unable to reliably start one or more daemons

I've been at it for days but could not solve my problem.
I am running:
mpiexec -hostfile ~/machines -nolocal -pernode mkdir -p $dstpath where $dstpath points to current directory and "machines" is a file containing:
node01
node02
node03
node04
This is the error output:
Failed to parse XML input with the minimalistic parser. If it was not
generated by hwloc, try enabling full XML support with libxml2.
[node01:06177] [[6421,0],0] ORTE_ERROR_LOG: Error in file base/plm_base_launch_support.c at line 891
--------------------------------------------------------------------------
ORTE was unable to reliably start one or more daemons.
This usually is caused by:
* not finding the required libraries and/or binaries on
one or more nodes. Please check your PATH and LD_LIBRARY_PATH
settings, or configure OMPI with --enable-orterun-prefix-by-default
* lack of authority to execute on one or more specified nodes.
Please verify your allocation and authorities.
* the inability to write startup files into /tmp (--tmpdir/orte_tmpdir_base).
Please check with your sys admin to determine the correct location to use.
* compilation of the orted with dynamic libraries when static are required
(e.g., on Cray). Please check your configure cmd line and consider using
one of the contrib/platform definitions for your system type.
* an inability to create a connection back to mpirun due to a
lack of common network interfaces and/or no route found between
them. Please check network connectivity (including firewalls
and network routing requirements).
--------------------------------------------------------------------------
[node01:06177] 1 more process has sent help message help-errmgr-base.txt / failed-daemon-launch
[node01:06177] Set MCA parameter "orte_base_help_aggregate" to 0 to see all help / error messages
Failed to parse XML input with the minimalistic parser. If it was not
generated by hwloc, try enabling full XML support with libxml2.
[node01:06181] [[6417,0],0] ORTE_ERROR_LOG: Error in file base/plm_base_launch_support.c at line 891
I have 4 machines, node01 to node04. In order to log into these 4 nodes, I have to first log in to node00. I am trying to run some distributed graph functions. The graph software is installed in node01 and is supposed to be synchronised to the other nodes using mpiexec.
What I've done:
Made sure all passwordless login are setup, every machine can ssh to any other machine with no issues.
Have a hostfile in the home directory.
echo $PATH gives /home/myhome/bin:/home/myhome/.local/bin:/usr/include/openmpi:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
echo $LD_LIBRARY_PATH gives
/usr/lib/openmpi/lib
This has previously worked before, but it just suddenly started giving these errors. I got my administrator to install fresh machines but it still gave such errors. I've tried doing it one node at a time but it gave the same errors. I'm not entirely familiar with command line at all so please give me some suggestions. I've tried reinstalling OpenMPI from source and from sudo apt-get install openmpi-bin. I'm on Ubuntu 16.04 LTS.
You should focus on fixing:
Failed to parse XML input with the minimalistic parser. If it was not
generated by hwloc, try enabling full XML support with libxml2.
[node01:06177] [[6421,0],0] ORTE_ERROR_LOG: Error in file base/plm_base_launch_support.c at line 891

UNABLE to load uWSGI plugin

I want to use python3.4 in my project, but i was getting error:
!!! UNABLE to load uWSGI plugin: ./python34_plugin.so: undefined symbol: spool_request !!!
I have the file in the dir, but it just don't working.
uwsgi
#mysite_uwsgi.ini file
[uwsgi]
# Django-related settings
# the base directory (full path)
chdir = /data/mysite/project
# Django's wsgi file
wsgi-file = /data/mysite/project/mysite/wsgi.py
# the virtualenv (full path)
home = /data/mysite/venv/pt3
# master
master = True
plugins-dir = /usr/lib/uwsgi/plugins
plugins = python34
# maximum number of worker processes
processes = 5
# the socket
socket = /tmp/mysite.sock
# ... with appropriate permissions - may be needed
chmod-socket = 666
# clear environment on exit
vacuum = true
# account to start process
uid = root
gid = root
# Output messages to log
daemonize=/var/log/uwsgi/mysite.log
PS. Sorry for my english
You've probably used different uWSGI build profiles or source code versions to build your python plugin and uwsgi binary.
To fix that issue, clone source code of uWSGI version that you want to use and compile it and proper plugin:
make buildprofile
PYTHON=python3.4 ./uwsgi --build-plugin "plugins/python python34"
After doing that, stop all uWSGI servers in your system, replace uWSGI binary in your system with freshly build one, replace plugin for python 3.4 with fresh one and start uWSGI again.

pyramid + gunicorn_paster development.ini : Error waitress

I am trying to use gunicorn with pyramid.
I installed gunicorn 18 into pyramid 1.5 dedicated virtualenv,
and after activating it, I start gunicorn_paster, but it stops at once with an error :
(venv) gunicorn_paster development.ini
Error: waitress
What this error means ?
I tried --debug but it did not give me more clues.
--preload does not work neither.
'pserve development.ini' or mod_wsgi works well, so my virtualenv should be OK.
You need a configuration file.
#gunicorn_conf.py
import os
def numCPUs():
if not hasattr(os, "sysconf"):
raise RuntimeError("No sysconf detected.")
return os.sysconf("SC_NPROCESSORS_ONLN")
workers = numCPUs() * 2 + 1
bind = "127.0.0.1:8001"
pidfile = "/tmp/gunicorn-app.pid"
backlog = 2048
logfile = "/var/log/gunicorn-app.log"
loglevel = "info"
Then launch as shown (note gunicorn_conf.py needs to be in same dir as development.ini)
gunicorn --paste development.ini
You can leave your development.ini as it is, no need to edit.
I Found the problem : I just had to deactivate/activate again the virtualenv after gunicorn install to have it work.
What server setting does your development.ini have ? By default, it might be using waitress. Kindly check the ini file configuration.
Try this:
# ini file
[server:main]
use = egg:gunicorn#main
host = 0.0.0.0
port = 5000

Resources