I've got an AWS EC2 instance running a flask app through apache.
I've installed boto via pip
I've got a wsgi file set up and if my flask app is a simple hello world it works fine:
#This works
from flask import Flask
app = Flask(__name__)
#app.route("/hello")
def hello():
return "Hello World"
if __name__ == "__main__":
app.run(host='0.0.0.0',port=80)
#http://<PUBLIC_IP>.ap-southeast-2.compute.amazonaws.com/hello
#Hello World
But if I try to import boto:
from boto import dynamodb2
from boto.dynamodb2.table import Table
When I visit http://.ap-southeast-2.compute.amazonaws.com/hello
I get an internal server error. Checking the logs reveals this
[Thu Aug 06 03:20:50 2015] [error] [client 203.220.19.142] mod_wsgi (pid=2988): Target WSGI script '/var/www/myservice/myservice.wsgi' cannot be loaded as Python module.
[Thu Aug 06 03:20:50 2015] [error] [client 203.220.19.142] mod_wsgi (pid=2988): Exception occurred processing WSGI script '/var/www/myservice/myservice.wsgi'.
[Thu Aug 06 03:20:50 2015] [error] [client 203.220.19.142] Traceback (most recent call last):
[Thu Aug 06 03:20:50 2015] [error] [client 203.220.19.142] File "/var/www/myservice/myservice.wsgi", line 4, in <module>
[Thu Aug 06 03:20:50 2015] [error] [client 203.220.19.142] from myservice import app as application
[Thu Aug 06 03:20:50 2015] [error] [client 203.220.19.142] File "/var/www/myservice/myservice.py", line 6, in <module>
[Thu Aug 06 03:20:50 2015] [error] [client 203.220.19.142] from boto import dynamodb2
[Thu Aug 06 03:20:50 2015] [error] [client 203.220.19.142] ImportError: No module named boto
Even though I've already installed boto (pip install boto)
How can I fix this?
I searched yum for related stuff and by running:
yum install python26-boto.noarch
I fixed the issue
Related
I try to setup a website based on Flask on a Raspberry Pi 3B+, with Apache2 (2.4.25), and mod_wsgi, but I am unable to import the module flask_cors in Python3.5...
I installed mod_wsgi with the command ssudo apt-get install libapache2-mod-wsgi
Because I only plan on using the Raspberry for hosting my website, I tried to install the packages needed by Flask system-wide, with the version 3.5 of Python, with the following command: pip3 install -r requirements.txt where requirements.txt is the following:
click==6.7
Flask==0.12.3
Flask-Cors==3.0.3
itsdangerous==0.24
Jinja2==2.9.6
MarkupSafe==1.0
six==1.11.0
Werkzeug==0.12.2
Requests==2.20.0
I think I understood that on a Raspi, it is best to import the modules wit the command pip3 install -U <module> because the packages are not installed on the default location for python, which is confirmed in my case by the following command python -m site that yields the following information:
sys.path = [
'/home/pi/.local/lib/python3.5/site-packages',
'/usr/lib/python35.zip',
'/usr/lib/python3.5',
'/usr/lib/python3.5/plat-arm-linux-gnueabihf',
'/usr/lib/python3.5/lib-dynload',
'/usr/local/lib/python3.5/dist-packages',
'/usr/local/lib/python3.5/dist-packages/virtualenv-16.1.0-py3.5.egg',
'/usr/lib/python3/dist-packages',
]
USER_BASE: '/home/pi/.local' (exists)
USER_SITE: '/home/pi/.local/lib/python3.5/site-packages' (exists)
ENABLE_USER_SITE: True
My Flask and dependencies are indeed installed in the /home/pi/.local/lib/python3.5/site-packages folder. But this does not seem to be a problem because when checking for importing on a python shell, everything seems fine systemwide:
pi#raspberrypi:~ $ python3
Python 3.5.3 (default, Sep 27 2018, 17:25:39)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from flask_cors import CORS
>>>
Knowing that, I tried to put the following files to setup my apache server:
flaskregul.conf in the folder /etc/apache2/sites-enabled:
<VirtualHost *:80>
WSGIScriptAlias / /var/www/flaskregul/flaskregul.wsgi
Alias /static/ /var/www/flaskregul/dist/static/
<Directory /var/www/flaskregul/dist/static>
WSGIProcessGroup flaskregul
WSGIApplicationGroup %{GLOBAL}
Order allow,deny
Allow from all
</Directory>
ErrorLog /var/log/apache2/flask.monnomdedomaine.com.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
flaskregul.wsgi in /var/www/flaskregul:
import sys
sys.path.insert(0,"/var/www/flaskregul") # path to my __init__.py file
sys.path.insert(0,"/home/pi/.local/lib/python3.5/site-packages") # path to the site-packages folder where the modules are installed by pip3
from __init__ import app as application
__init__.py in /var/www/flaskregul:
from flask import Flask
from flask_cors import CORS
I encounter an impossibilty to specifically import the flask_cors module, because the launching of the file __init__.py yields the following errors in the file /var/log/apache2/flask.monnomdedomaine.com.log:
[Sat Nov 10 00:37:22.220394 2018] [wsgi:error] [pid 6804:tid 1955591216] [client 192.168.1.67:33550] mod_wsgi (pid=6804): Target WSGI script '/var/www/flaskregul/flaskregul.wsgi' cannot be loaded as Python module.
[Sat Nov 10 00:37:22.220972 2018] [wsgi:error] [pid 6804:tid 1955591216] [client 192.168.1.67:33550] mod_wsgi (pid=6804): Exception occurred processing WSGI script '/var/www/flaskregul/flaskregul.wsgi'.
[Sat Nov 10 00:37:22.221936 2018] [wsgi:error] [pid 6804:tid 1955591216] [client 192.168.1.67:33550] Traceback (most recent call last):
[Sat Nov 10 00:37:22.222365 2018] [wsgi:error] [pid 6804:tid 1955591216] [client 192.168.1.67:33550] File "/var/www/flaskregul/flaskregul.wsgi", line 1, in <module>
[Sat Nov 10 00:37:22.222395 2018] [wsgi:error] [pid 6804:tid 1955591216] [client 192.168.1.67:33550] from __init__ import app as application
[Sat Nov 10 00:37:22.222445 2018] [wsgi:error] [pid 6804:tid 1955591216] [client 192.168.1.67:33550] File "/var/www/flaskregul/__init__.py", line 2, in <module>
[Sat Nov 10 00:37:22.222463 2018] [wsgi:error] [pid 6804:tid 1955591216] [client 192.168.1.67:33550] from flask_cors import CORS
[Sat Nov 10 00:37:22.222557 2018] [wsgi:error] [pid 6804:tid 1955591216] [client 192.168.1.67:33550] ImportError: No module named 'flask_cors'
Whereas the import of Flask seems fine...
I've tried to debug this by changing the WSGI script file by this one:
import sys
sys.path.insert(0,"/var/www/flaskregul")
sys.path.insert(0,"/home/pi/.local/lib/python3.5/site-packages")
def application(environ, start_response):
status = '200 OK'
output = u''
output += u'sys.version = %s\n' % repr(sys.version)
output += u'sys.prefix = %s\n' % repr(sys.prefix)
output += u'sys.path = %s\n' % repr(sys.path)
response_headers = [('Content-type', 'text/plain'),
('Content-Length', str(len(output)))]
start_response(status, response_headers)
return [output.encode('UTF-8')]
This is a piece of advice the advice on this web page from mod_wsgi: https://modwsgi.readthedocs.io/en/develop/user-guides/checking-your-installation.html. When opening the webpage, I obtain this result:
sys.version = '3.5.3 (default, Sep 27 2018, 17:25:39) \n[GCC 6.3.0 20170516]'
sys.prefix = '/usr'
sys.path = ['/home/pi/.local/lib/python3.5/site-packages', '/var/www/flaskregul', '/usr/lib/python35.zip', '/usr/lib/python3.5', '/usr/lib/python3.5/plat-arm-linux-gnueabihf', '/usr/lib/python3.5/lib-dynload', '/usr/local/lib/python3.5/dist-packages', '/usr/local/lib/python3.5/dist-packages/virtualenv-16.1.0-py3.5.egg', '/usr/lib/python3/dist-packages']
I have the impression that the path is correct! I actually succeed in importing my own modules located in the /var/www/flaskregul location...
I am lost, I don't understand why specifically the module flask_cors is unavaiblable, because it is indeed in the path:
pi#raspberrypi:~/.local/lib/python3.5/site-packages $ l | grep flask
drwxr-xr-x 5 pi pi 4096 nov. 9 23:30 flask
drwxr-xr-x 3 pi pi 4096 nov. 10 00:08 flask_cors
pi#raspberrypi:~/.local/lib/python3.5/site-packages $ l flask_cors
total 44
-rw-r--r-- 1 pi pi 13771 nov. 10 00:08 core.py
-rw-r--r-- 1 pi pi 4937 nov. 10 00:08 decorator.py
-rw-r--r-- 1 pi pi 7405 nov. 10 00:08 extension.py
-rw-r--r-- 1 pi pi 924 nov. 10 00:08 __init__.py
drwxr-xr-x 2 pi pi 4096 nov. 10 00:08 __pycache__
-rw-r--r-- 1 pi pi 22 nov. 10 00:08 version.py
Any help would be very much appreciated! I am currently losing my hair at a worriyng rate!
Oh my god I solved it in a desperate try before giving up.
I executed the following commands on a shell: (pip is the pip of the python3.5 /!\)
pip uninstall flask
pip uninstall flask_cors
sudo pip install --upgrade pip
hash -d pip
sudo pip install -U flask
sudo pip install -U flask_cors
Website up and running! :D
I'm currently experimenting with Puppet and have run into an error where it's starting apache2 without the environment variables. I've been following example 1 and it's a very simple manifest but it throws the below errors;
[Mon May 18 23:18:35.725947 2015] [core:warn] [pid 5312] AH00111: Config variable ${APACHE_LOCK_DIR} is not defined
[Mon May 18 23:18:35.726451 2015] [core:warn] [pid 5312] AH00111: Config variable ${APACHE_PID_FILE} is not defined
[Mon May 18 23:18:35.726538 2015] [core:warn] [pid 5312] AH00111: Config variable ${APACHE_RUN_USER} is not defined
[Mon May 18 23:18:35.726611 2015] [core:warn] [pid 5312] AH00111: Config variable ${APACHE_RUN_GROUP} is not defined
[Mon May 18 23:18:35.726696 2015] [core:warn] [pid 5312] AH00111: Config variable ${APACHE_LOG_DIR} is not defined
[Mon May 18 23:18:35.750730 2015] [core:warn] [pid 5312] AH00111: Config variable ${APACHE_LOG_DIR} is not defined
[Mon May 18 23:18:35.751241 2015] [core:warn] [pid 5312] AH00111: Config variable ${APACHE_LOG_DIR} is not defined
[Mon May 18 23:18:35.751331 2015] [core:warn] [pid 5312] AH00111: Config variable ${APACHE_LOG_DIR} is not defined
AH00526: Syntax error on line 74 of /etc/apache2/apache2.conf:
Invalid Mutex directory in argument file:${APACHE_LOCK_DIR}
I know the issue is around it not loading in the envvar file and this can be done if I don't run it from Puppet but i'm looking for a solution so I can use puppet to create servers. Any ideas?
My Puppet manifest is below:
# execute 'apt-get update'
exec { 'apt-update': # exec resource named 'apt-update'
command => '/usr/bin/apt-get update' # command this resource will run
}
# install apache2 package
package { 'apache2':
require => Exec['apt-update'], # require 'apt-update' before installing
ensure => installed,
}
# ensure apache2 service is running
service { 'apache2':
ensure => running,
}
# install mysql-server package
package { 'mysql-server':
require => Exec['apt-update'], # require 'apt-update' before installing
ensure => installed,
}
# ensure mysql service is running
service { 'mysql':
ensure => running,
}
# install php5 package
package { 'php5':
require => Exec['apt-update'], # require 'apt-update' before installing
ensure => installed,
}
# ensure info.php file exists
file { '/var/www/html/info.php':
ensure => file,
content => '<?php phpinfo(); ?>', # phpinfo code
require => Package['apache2'], # require 'apache2' package before creating
}
I have Python 2.7 installed with ampps. I tried to modify python.conf to make it point to another installation of Python 3.3 but it leads to errors in Apache start up.
LoadModule wsgi_module modules/mod_wsgi.so
WSGIPythonPath "C:/Python33/Lib;C:/Python33/Lib/site-packages;C:/Python33/DLLs"
WSGIPythonHome "C:/Python33"
It generates the following error:
[Fri Aug 15 01:48:38.114336 2014] [mpm_winnt:notice] [pid 6084:tid 412] AH00418: Parent: Created child process 5592
File "C:/Python33\lib\site.py", line 173
file=sys.stderr)
^
SyntaxError: invalid syntax
[Fri Aug 15 01:48:39.146395 2014] [mpm_winnt:crit] [pid 6084:tid 412] AH00419: master_main: create child process failed. Exiting.
I am getting an error when I run the apache server through my client after going through the log I understood that the mod_wsgi uses python 2.6 during compiling and uses python 2.7 for running. After some research in the Internet I followed the below steps:
You have to recompile mod-python and/or mod-wsgi.
Remove mods
apt-get remove libapache2-mod-python libapache2-mod-wsgi
Get dependencies
apt-get build-dep libapache2-mod-python libapache2-mod-wsgi
Build mod-python
mkdir /tmp/python
cd /tmp/python
apt-get source libapache2-mod-python
cd libapache2-mod-python-[x.x.x]
dpkg-buildpackage -rfakeroot -b
Build mod-wsgi
mkdir /tmp/wsgi
cd /tmp/wsgi
apt-get source libapache2-mod-wsgi
cd mod-wsgi-[x.x.x]
dpkg-buildpackage -rfakeroot -b
Install newly compiled packages
dpkg -i /tmp/python/libapache2-mod-python-[x.x].deb /tmp/wsgi/libapache2-mod-wsgi-[x.x].deb
It was of no use, now the version has changed to 3.2, I am worried about the space being consumed through the above steps and now the compiling python has changes to python 3.2 from 2.6 but the python used for running is still 2.7. please help me with what to do ? to get back my apache server running successfully.
error.log::::
[Wed Aug 21 11:48:11 2013] [warn] mod_wsgi: Compiled for Python/2.7.2+.
[Wed Aug 21 11:48:11 2013] [warn] mod_wsgi: Runtime using Python/2.7.3.
[Wed Aug 21 11:48:11 2013] [notice] Apache/2.2.22 (Ubuntu) mod_wsgi/3.3 Python/2.7.3 configured -- resuming normal operations
[Wed Aug 21 11:48:36 2013] [notice] caught SIGTERM, shutting down
[Wed Aug 21 22:48:29 2013] [error] child process 1226 still did not exit, sending a SIGKILL
[Wed Aug 21 22:48:30 2013] [notice] caught SIGTERM, shutting down
[Wed Aug 21 22:56:17 2013] [warn] mod_wsgi: Compiled for Python/2.7.2+.
[Wed Aug 21 22:56:17 2013] [warn] mod_wsgi: Runtime using Python/2.7.3.
[Wed Aug 21 22:56:17 2013] [notice] Apache/2.2.22 (Ubuntu) mod_wsgi/3.3 Python/2.7.3 configured -- resuming normal operations
[Thu Aug 22 01:32:12 2013] [notice] caught SIGTERM, shutting down
[Thu Aug 22 01:32:26 2013] [warn] mod_wsgi: Compiled for Python/2.7.2+.
[Thu Aug 22 01:32:26 2013] [warn] mod_wsgi: Runtime using Python/2.7.3.
[Thu Aug 22 01:32:26 2013] [notice] Apache/2.2.22 (Ubuntu) mod_wsgi/3.3 Python/2.7.3 configured -- resuming normal operations
[Thu Aug 22 04:04:48 2013] [notice] child pid 11212 exit signal Segmentation fault (11)
[Thu Aug 22 04:04:48 2013] [notice] caught SIGTERM, shutting down
[Thu Aug 22 04:04:56 2013] [notice] mod_python: Creating 8 session mutexes based on 6 max processes and 25 max threads.
[Thu Aug 22 04:04:56 2013] [notice] mod_python: using mutex_directory /tmp
[Thu Aug 22 04:04:56 2013] [warn] mod_wsgi: Compiled for Python/3.2.3.
[Thu Aug 22 04:04:56 2013] [warn] mod_wsgi: Runtime using Python/2.7.3.
[Thu Aug 22 04:04:56 2013] [notice] Apache/2.2.22 (Ubuntu) mod_python/3.3.1 Python/2.7.3 mod_wsgi/3.3 configured -- resuming normal operations
Thank you
Don't load mod_python and mod_wsgi at the same time if you don't need to. They are likely compiled against different Python versions. See the following for an explanation of the mismatch you are seeing.
http://code.google.com/p/modwsgi/wiki/InstallationIssues#Python_Version_Mismatch
If you do need both, they must both be compiled for the same version.
These days there is generally no good reason to be using mod_python for new projects.
Just to add
I have uninstalled libapache2-mod-python
sudo apt-get remove libapache2-mod-python
which I have installed
then I have overcome the above error
[Thu Aug 22 01:32:26 2013] [warn] mod_wsgi: Compiled for Python/2.7.2+.
[Thu Aug 22 01:32:26 2013] [warn] mod_wsgi: Runtime using Python/2.7.3.
All, I'm trying to deploy a simple page using flask/apache on a digitalocean server. It all works fine locally, but not on the server.
__init__.py contains the statement:
from flask.ext.wtf import Form,TextField
try_me.wsgi is:
#!/usr/bin/python
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/try_me/")
from try_me import app as application
application.secret_key = 'Add your secret key'
Getting the following error (from /var/log/apache2/error.log):
[Mon Jul 29 14:16:50 2013] [warn] mod_wsgi: Compiled for Python/2.7.2+.
[Mon Jul 29 14:16:50 2013] [warn] mod_wsgi: Runtime using Python/2.7.3.
[Mon Jul 29 14:16:50 2013] [notice] Apache/2.2.22 (Debian) mod_wsgi/3.3 Python/2.7.3 configured -- resuming normal operations
[Mon Jul 29 14:16:57 2013] [error] [client 74.66.8.166] mod_wsgi (pid=2920): Target WSGI script '/var/www/try_me/try_me.wsgi' cannot be loaded as Python module.
[Mon Jul 29 14:16:57 2013] [error] [client 74.66.8.166] mod_wsgi (pid=2920): Exception occurred processing WSGI script '/var/www/try_me/try_me.wsgi'.
[Mon Jul 29 14:16:57 2013] [error] [client 74.66.8.166] Traceback (most recent call last):
[Mon Jul 29 14:16:57 2013] [error] [client 74.66.8.166] File "/var/www/try_me/try_me.wsgi", line 7, in <module>
[Mon Jul 29 14:16:57 2013] [error] [client 74.66.8.166] from try_me import app as application
[Mon Jul 29 14:16:57 2013] [error] [client 74.66.8.166] File "/var/www/try_me/try_me/__init__.py", line 4, in <module>
[Mon Jul 29 14:16:57 2013] [error] [client 74.66.8.166] from flask.ext.wtf import Form
[Mon Jul 29 14:16:57 2013] [error] [client 74.66.8.166] File "/usr/local/lib/python2.7/dist-packages/flask/exthook.py", line 87, in load_module
[Mon Jul 29 14:16:57 2013] [error] [client 74.66.8.166] raise ImportError('No module named %s' % fullname)
[Mon Jul 29 14:16:57 2013] [error] [client 74.66.8.166] ImportError: No module named flask.ext.wtf
I was able to import manually in a python interpreter (under virtualenv):
Python 2.7.3 (default, Jan 2 2013, 16:53:07)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from flask.ext.wtf import Form
>>> Form
<class 'flask_wtf.form.Form'>
Any ideas on how to proceed here?
SOLUTION (Following Cathy's advice above, and dAnjou below), virtualenv must be activated from the wsgi script. Adding activate_this execution solved the problem:
#!/usr/bin/python
activate_this = '/var/www/try_me/venv/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))
import sys
..