uWSGI, ImportError: No module named site on Ubuntu - python

I'm trying to follow the tutorial at http://uwsgi-docs.readthedocs.org/en/latest/tutorials/Django_and_nginx.html. I've gotten everything working down tohttp://uwsgi-docs.readthedocs.org/en/latest/tutorials/Django_and_nginx.html#install-uwsgi-system-wide. I am working with an ubuntu 14.4 instance on amazon EC2:
Without going into my virtualenv , I ran:
sudo pip install uwsgi
This led to
############## end of uWSGI configuration #############
total build time: 24 seconds
*** uWSGI is ready, launch it with /usr/local/bin/uwsgi ***
Successfully installed uwsgi
Cleaning up...
Then I ran:
ubuntu#ip-172-31-28-196:~$ uwsgi --ini /home/ubuntu/tproxy/tp/mysite_uwsgi.ini
[uWSGI] getting INI configuration from /home/ubuntu/tproxy/tp/mysite_uwsgi.ini
*** Starting uWSGI 2.0.9 (64bit) on [Fri Mar 6 16:15:07 2015] ***
compiled with version: 4.8.2 on 06 March 2015 16:10:06
os: Linux-3.13.0-44-generic #73-Ubuntu SMP Tue Dec 16 00:22:43 UTC 2014
nodename: ip-172-31-28-196
machine: x86_64
clock source: unix
detected number of CPU cores: 1
current working directory: /home/ubuntu
detected binary path: /usr/local/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
chdir() to /home/ubuntu/tproxy/tp
your processes number limit is 7862
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to UNIX address /tmp/mysite.sock fd 3
Python version: 2.7.6 (default, Mar 22 2014, 23:03:41) [GCC 4.8.2]
Set PythonHome to /home/ubuntu/.virtualenvs/env1
ImportError: No module named site
VACUUM: unix socket /tmp/mysite.sock removed.
Where is
ImportError: No module named site on Ubuntu
coming from and how can I fix this?

Try no-site = True in mysite_uwsgi.ini file.

Related

Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding

I have hosted a webapp on a linux server using django-uwsgi,I was trying to directly call the --ini file from uwsgi as uwsgi --ini .ini file but I'm getting a weird error like this.
[uWSGI] getting INI configuration from /etc/uwsgi/sites/salesproject.ini
*** Starting uWSGI 2.0.18 (64bit) on [Tue May 19 23:37:24 2020] ***
compiled with version: 9.3.0 on 18 May 2020 16:02:04
os: Linux-5.4.0-26-generic #30-Ubuntu SMP Mon Apr 20 16:58:30 UTC 2020
nodename: django-server
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 2
current working directory: /var/uwsgi
detected binary path: /home/ubaid/.local/bin/uwsgi
chdir() to /home/ubaid/salesproject
your processes number limit is 15402
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to UNIX address /var/uwsgi/salesproject.sock fd 3
Python version: 3.8.2 (default, Apr 27 2020, 15:53:34) [GCC 9.3.0]
!!! Python Home is not a directory: /home/ubaid/Env/salesproject !!!
Set PythonHome to /home/ubaid/Env/salesproject
Python path configuration:
PYTHONHOME = '/home/ubaid/Env/salesproject'
PYTHONPATH = (not set)
program name = '/home/ubaid/Env/salesproject/bin/python'
isolated = 0
environment = 1
user site = 1
import site = 1
sys._base_executable = '/home/ubaid/Env/salesproject/bin/python'
sys.base_prefix = '/home/ubaid/Env/salesproject'
sys.base_exec_prefix = '/home/ubaid/Env/salesproject'
sys.executable = '/home/ubaid/Env/salesproject/bin/python'
sys.prefix = '/home/ubaid/Env/salesproject'
sys.exec_prefix = '/home/ubaid/Env/salesproject'
sys.path = [
'/home/ubaid/Env/salesproject/lib/python38.zip',
'/home/ubaid/Env/salesproject/lib/python3.8',
'/home/ubaid/Env/salesproject/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'
Current thread 0x00007f966c94e680 (most recent call first):
<no Python frame>
VACUUM: unix socket /var/uwsgi/salesproject.sock removed.
Salesproject is my webapp and I definetly don't have a path like this /home/ubaid/Env/salesproject,
my path is like this /home/ubaid/salesproject,
Can you please help me resolve this? And yeah I have python3 installed

Syntax error while running application with uwsgi file directive for sample application

I am trying to run one sample application with uWSGI file directive using two files for it testService.ini(config file) & testService.py (application file).
**testService.ini**
[uwsgi]
wsgi-file = '/opt/testService/testService.py'
callable = app
**testService.py**
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return [b"Hello World"]
running the above file using : uwsgi --http :9090 --wsgi-file testService.ini
Output:
Starting uWSGI 2.0.17 (64bit) on [Thu Jun 21 15:52:33 2018]
compiled with version: 5.4.0 20160609 on 14 June 2018 10:09:32
os: Linux-4.13.0-45-generic #50~16.04.1-Ubuntu SMP Wed May 30 11:18:27 UTC 2018
nodename: ubuntu
machine: x86_64
clock source: unix
detected number of CPU cores: 8
current working directory: /opt/testService
detected binary path: /home/jmallick/.local/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 15481
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uWSGI http bound on :9090 fd 4
spawned uWSGI http 1 (pid: 7848)
uwsgi socket 0 bound to TCP address 127.0.0.1:45781 (port auto-assigned) fd 3
Python version: 2.7.12 (default, Dec 4 2017, 14:50:18) [GCC 5.4.0 20160609]
Python threads support is disabled. You can enable it with --enable-threads
Python main interpreter initialized at 0x824400
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 72920 bytes (71 KB) for 1 cores
Operational MODE: single process
File "testService.ini", line 2
**wsgi-file = '/opt/testService/testService.py'
SyntaxError: can't assign to operator
failed to compile python file testService.ini**
unable to load app 0 (mountpoint='') (callable not found or import error)
no app loaded. going in full dynamic mode
uWSGI is running in multiple interpreter mode
spawned uWSGI worker 1 (and the only) (pid: 7847, cores: 1)
And over the browser I am getting "Internal Server Error". Can someone help me understanding what I am doing wrong here ?

Flask Server: ImportError: No module named site

I'm trying to run a Flask application using a ubuntu 12.04 server with nginx and uWSGI however after starting the server, specifying the wsgi.ini file and then starting nginx, I try to access the page but I get a 502 bad gateway error with nginx. I know that it is an issue with the flask application and uWSGI. I checked the uWSGI logs and found this:
*** Starting uWSGI 2.0.11.1 (64bit) on [Thu Sep 17 19:38:03 2015] ***
compiled with version: 4.8.4 on 17 September 2015 15:58:04
os: Linux-3.13.0-48-generic #80-Ubuntu SMP Thu Mar 12 11:16:15 UTC 2015
nodename: ip-172-31-30-153
machine: x86_64
clock source: unix
detected number of CPU cores: 1
current working directory: /home/ubuntu/myapp
writing pidfile to /home/ubuntu/myapp/myapp.pid
detected binary path: /usr/local/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 7862
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to TCP address 127.0.0.1:5000 fd 3
Python version: 2.7.6 (default, Jun 22 2015, 18:01:27) [GCC 4.8.2]
Set PythonHome to /home/ubuntu/myapp
ImportError: No module named site
I found this question's top answer but saw received the same error. If it is something to do with PYTHONHOME, I'm following this guide and it states:
The $PYTHONPATH variable seems to get unset randomly, and causes
errors for later steps. To avoid this, add the following line to the
top of your ~/.bashrc file:
export
PYTHONPATH='/usr/lib/python2.7:/usr/lib/python2.7/plat-x86_64-linux-gnu:/usr/lib/python2.7/lib-tk:/usr/lib/python2.7/lib-old:/usr/lib/python2.7/lib-dynload:/usr/local/lib/python2.7/dist-packages:/usr/lib/python2.7/dist-packages:/usr/lib/python2.7/dist-packages/PILcompat'
So what could be the issue? Is it PYTHONHOME or another factor affecting it? Thanks.
Edit:
My uwsgi.ini:
[uwsgi]
home = /home/ubuntu/myapp
wsgi-file = %(home)/run.py
socket = 127.0.0.1:5000
callable = app
module = app
pythonpath = %(home)
daemonize = %(home)/myapp.log
pidfile = %(home)/myapp.pid
To run it, I use uwsgi uwsgi.ini.

ImportError: No module named site. Flask + uWSGI +virtualenv

I am having trouble getting my flask app to run on a new server running CentOS 6.5.using uWSGI. (I have a similar CentOS box that it runs fine on)
I installed uWSGI within a virtual environment. I was getting the following error (in stdout) when calling uwsgi binary from the virtualenv
$ /path/to/venv/bin/uwsgi --emperor /etc/uwsgi/vassals
*** Starting uWSGI 2.0.8 (64bit) on [Mon Nov 17 21:18:13 2014] ***
compiled with version: 4.4.7 20120313 (Red Hat 4.4.7-11) on 16 November 2014 22:19:09
os: Linux-2.6.32-431.1.2.0.1.el6.x86_64 #1 SMP Fri Dec 13 13:06:13 UTC 2013
nodename: boxname
machine: x86_64
clock source: unix
detected number of CPU cores: 1
current working directory: /home/username
detected binary path: /home/username/mydevelopment/venvs/ecodev_do/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 1024
your memory page size is 4096 bytes
detected max file descriptor number: 1024
*** starting uWSGI Emperor ***
*** has_emperor mode detected (fd: 6) ***
[uWSGI] getting INI configuration from ers_portal_uwsgi.ini
Mon Nov 17 21:18:14 2014 - [emperor] curse the uwsgi instance ers_portal_uwsgi.ini (pid: 22724)
Mon Nov 17 21:18:17 2014 - [emperor] removed uwsgi instance ers_portal_uwsgi.ini
The uwsgi.log file shows:
*** Starting uWSGI 2.0.8 (64bit) on [Mon Nov 17 21:25:40 2014] ***
compiled with version: 4.4.7 20120313 (Red Hat 4.4.7-11) on 16 November 2014 22:19:09
os: Linux-2.6.32-431.1.2.0.1.el6.x86_64 #1 SMP Fri Dec 13 13:06:13 UTC 2013
nodename: boxname
machine: x86_64
clock source: unix
detected number of CPU cores: 1
current working directory: /etc/uwsgi/vassals
detected binary path: /home/username/mydevelopment/venvs/ecodev_do/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
chdir() to /home/username/mydevelopment/git/ers_portal
your processes number limit is 1024
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to UNIX address /home/username/mydevelopment/git/ers_portal/ers_portal_uwsgi.sock fd 3
Python version: 2.7.8 (default, Nov 12 2014, 23:36:57) [GCC 4.4.7 20120313 (Red Hat 4.4.7-11)]
Set PythonHome to /home/username/mydevelopment/venvs/ecovapor_dev
ImportError: No module named site
Then, reading the official uWSGI documentation they describe a scenario where uwsgi is installed in a venv, but then it has to be installed system-wide after that. So, I used
pip2.7 install uwsgi (which created the binary in /usr/local/bin/uwsgi)
I have tried all of the following with no change in the error or the output (with and without venv activated)..
$ /usr/local/bin/uwsgi --emperor /etc/uwsgi/vassals
$ /home/username/mydevelopment/venvs/ecodev_do/bin/uwsgi --emperor /etc/uwsgi/vassals
$ /home/username/mydevelopment/venvs/ecodev_do/bin/uwsgi --no-site
--emperor /etc/uwsgi/vassals
$ uwsgi --emperor /etc/uwsgi/vassals
$ uwsgi --no-site --emperor /etc/uwsgi/vassals
$ /path/to/venv/bin/uwsgi ~/mydevelopment/git/ers_portal/ers_portal_uwsgi.ini (the .ini file is symlinked in /etc/uwsgi/vassals )
Here is emperor.ini;
[uwsgi]
emperor = /etc/uwsgi/vassals
uid = username
gid = username
logto = /home/username/mydevelopment/git/ers_portal/logs/uwsgiEmperor.log
Here is ers_portal_uwsgi.ini;
[uwsgi]
master = true
#user info
uid = username
gid = username
#application's base folder
base = /home/username/mydevelopment/git/ers_portal
#chdir to this folder when starting
chdir = /home/username/mydevelopment/git/ers_portal
#python module to import
app = run_web
module = %(app)
home = /home/username/mydevelopment/venvs/ecovapor_dev
pythonpath = %(base)
#socket file's location
socket = /home/username/mydevelopment/git/ers_portal/%n.sock
#permissions for the socket file
chmod-socket = 666
#the variable that holds a flask application inside the module imported at line #6
#uwsgi varible only, does not relate to your flask application
callable = app
#location of log files
logto = /home/username/mydevelopment/git/ers_portal/logs/%n.log
This helped me.
Type it in your terminal.
unset PYTHONHOME
unset PYTHONPATH
In case someone uses python 3.x, installing uwsgi with pip3 solved this problem for me:
sudo pip3 install uwsgi
The virtualenv in question and uwsgi must be using the same python version.
In case you want to use multiple python versions on the same system, you can get the uwsgi source and follow the instructions here: http://projects.unbit.it/uwsgi/wiki/MultiPython
Then add a "plugin=/path/to/plugin" to the .ini file
Take note that plugins dont work for the packaged Ubuntu versions (built monolithic), but you need to compile the source directly for a fully modular binary

Flask Pygal Installation - No module named pygal

I want to be able to create dynamic graphs within my Flask application. I am running this application within a virtual environment on a Linux box.
I looked around for different libraries that would help me with this, and identified Pygal as the one which I would like to use.
So, I activated my virtual environment using source venv/bin/activate and installed using pip install pygal. Everything installed correctly and I can import it perfectly in using the regular Python interface.
However, when I put import pygal into my Flask application and run the application, I get the following error in the logs.
Traceback (most recent call last):
File "/usr/share/nginx/www/mydir/run.py", line 2, in <module>
from app import app
File "/usr/share/nginx/www/mydir/app/__init__.py", line 23, in <module>
from app import views, models
File "/usr/share/nginx/www/mydir/views.py", line 9, in <module>
import datetime, locale, pygal
ImportError: No module named pygal
Does anyone know how I can resolve this?
Many thanks.
-- EDIT --
RE: Sean Vieira
Results of adding from sys import path; print(path) to my run script:
*** Starting uWSGI 1.9.20 (32bit) on [Tue Feb 25 14:55:16 2014] ***
compiled with version: 4.6.3 on 04 December 2013 05:11:40
os: Linux-3.8.0-29-generic #42~precise1-Ubuntu SMP Wed Aug 14 15:31:16 UTC 2013
nodename: **hidden**
machine: i686
clock source: unix
detected number of CPU cores: 1
current working directory: /etc/uwsgi/vassals
detected binary path: /usr/share/nginx/www/mydir/venv/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
your processes number limit is 3840
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to UNIX address /usr/share/nginx/www/mydir/mydir_uwsgi.sock fd 3
Python version: 2.7.3 (default, Sep 26 2013, 20:26:19) [GCC 4.6.3]
Set PythonHome to /usr/share/nginx/www/mydir/venv
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x93e80a8
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 169864 bytes (165 KB) for 1 cores
*** Operational MODE: single process ***
added /usr/share/nginx/www/mydir/ to pythonpath.
['/usr/share/nginx/www/mydir/', '.', '', '/usr/share/nginx/www/mydir/venv/lib/python2.7/site-packages/distribute-0.6.24-py2.7.egg', '/usr/share/nginx/www/mydir/venv/lib/python2.7/site-packages/pip-1.1-py2.7.egg', '/usr/share/nginx/www/mydir/venv/lib/python2.7', '/usr/share/nginx/www/mydir/venv/lib/python2.7/plat-linux2', '/usr/share/nginx/www/mydir/venv/lib/python2.7/lib-tk', '/usr/share/nginx/www/mydir/venv/lib/python2.7/lib-old', '/usr/share/nginx/www/mydir/venv/lib/python2.7/lib-dynload', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2', '/usr/lib/python2.7/lib-tk', '/usr/share/nginx/www/mydir/venv/lib/python2.7/site-packages']
WSGI app 0 (mountpoint='') ready in 1 seconds on interpreter 0x93e80a8 pid: 20964 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 20964)
spawned uWSGI worker 1 (pid: 20981, cores: 1)
Issue resolved.
I had to add the path to the package into the sys.path as described in an answer for this question. As I couldn't get Pygal to install within my virtual environment for some reason.

Categories