I had mod_wsgi working fine on RHEL/Apache2.4/python3.4/Django1.11/virtualenv. Then, I used pip within the virtualenv to upgrade numpy from 1.14.0 to 1.14.2, install pandas, and install a couple other modules. Then I restarted Apache and started getting server errors indicating AttributeError: 'module' object has no attribute 'arange' when trying to call numpy.arange, or similar errors when trying to call matplotlib.use('Agg'), etc.
I replaced wsgi.py with the following and it works fine. As soon as I uncomment any of the numpy or matplotlib calls it returns a 500 error and in the apache error log it shows an error similar to AttributeError: 'module' object has no attribute ...
import datetime
import sys
import numpy as np
import matplotlib as mpl
import mod_wsgi
def application(env, start_response):
#mpl.use('Agg')
#a = np.arange(15).reshape(3, 5)
#a = np.array([2,3,4])
status = '200 OK'
output = 'Hello World! This is the wsgi app generated from python!'
output += '\n\ncurrent time is: '+str(datetime.datetime.now())
output += '\n\nsys.executable='+sys.executable
output += '\n\nsys.path='+str(sys.path)
output += '\n\nsys.version=' + str(sys.version)
output += '\n\nsys.prefix=' + str(sys.prefix)
output += '\n\nmod_wsgi.version='+str(mod_wsgi.version)
output += '\n\n\nEnvironment Variables:\n\n'+'\n'.join('%s: %s' % (k, v) for (k, v) in env.items())
output = bytes(output,'utf-8')
response_headers = [('Content-type', 'text/plain'),
('Content-Length', str(len(output)))]
start_response(status, response_headers)
return [output]
Relevant output from this wsgi.py:
sys.executable=/usr/bin/python3
sys.path=['/usr/local/virtualenvs/myapp/wsgiapp', '/usr/local/virtualenvs/myapp/lib64/python34.zip', '/usr/local/virtualenvs/myapp/lib64/python3.4', '/usr/local/virtualenvs/myapp/lib64/python3.4/plat-linux', '/usr/local/virtualenvs/myapp/lib64/python3.4/lib-dynload', '/usr/lib64/python3.4', '/usr/lib/python3.4', '/usr/local/virtualenvs/myapp/lib/python3.4/site-packages']
sys.version=3.4.8 (default, Mar 23 2018, 10:04:27)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]
sys.prefix=/usr/local/virtualenvs/myapp
mod_wsgi.version=(4, 5, 24)
mod_wsgi.process_group: mywsgiapp
mod_wsgi.application_group:
The Apache httpd.conf didn't change. Relevant lines:
LoadModule wsgi_module "/usr/local/virtualenvs/myapp/lib64/python3.4/site-packages/mod_wsgi/server/mod_wsgi-py34.cpython-34m.so"
WSGIDaemonProcess mywsgiapp python-home=/usr/local/virtualenvs/myapp python-path=/usr/local/virtualenvs/myapp/wsgiapp
WSGIProcessGroup mywsgiapp
WSGIApplicationGroup %{GLOBAL}
WSGIScriptAlias /myapp /usr/local/virtualenvs/myapp/wsgiapp/wsgi.py process-group=mywsgiapp application-group=%{GLOBAL}
I'm not sure what caused this to break. I know this usually happens when mod_wsgi is compiled for the wrong version of python. But this was working fine before and everything still appears to be configured correctly: mod_wsgi is functioning, the output from the wsgi.py indicates it is using the correct version of python, and the site-packages directory of the virtualenv is in the python-path.
I have mod_wsgi installed in the main python3.4 location and tried pointing the Apache LoadModule at that instance but still get the same errors.
What else should I check?
Should sys.executable be pointing to the virtualenv instead of the main python install? Should I pip uninstall all the modules and start over?
Also, to install pandas, I had to install Cython, which in turn required that I install gcc-c++ (sudo yum install gcc-c++). Would that affect anything?
Edited to add error traces. I uncommented the line a = np.arange(15).reshape(3, 5) in the above wsgi.py file.
HTTP Error Page from Apache:
Internal Server Error
The server encountered an internal error or
misconfiguration and was unable to complete
your request.
Please contact the server administrator at
root#localhost to inform them of the time this error occurred,
and the actions you performed just before this error.
More information about this error may be available
in the server error log.
Apache error log (note I x'ed out the IP address before posting here):
[Thu Jul 12 23:01:15.664941 2018] [wsgi:error] [pid 21490] [remote 10.x.x.x:59961] mod_wsgi (pid=21490): Exception occurred processing WSGI script '/usr/local/virtualenvs/myapp/wsgiapp/wsgi.py'.
[Thu Jul 12 23:01:15.727450 2018] [wsgi:error] [pid 21490] [remote 10.x.x.x:59961] Traceback (most recent call last):
[Thu Jul 12 23:01:15.727814 2018] [wsgi:error] [pid 21490] [remote 10.x.x.x:59961] File "/usr/local/virtualenvs/myapp/wsgiapp/wsgi.py", line 14, in application
[Thu Jul 12 23:01:15.727831 2018] [wsgi:error] [pid 21490] [remote 10.x.x.x:59961] a = np.arange(15).reshape(3, 5)
[Thu Jul 12 23:01:15.727865 2018] [wsgi:error] [pid 21490] [remote 10.x.x.x:59961] AttributeError: 'module' object has no attribute 'arange'
Error when trying to show np.file:
[Thu Jul 12 23:25:59.196534 2018] [wsgi:error] [pid 22747] [remote 10.x.x.x:60353] mod_wsgi (pid=22747): Exception occurred processing WSGI script '/usr/local/virtualenvs/myapp/wsgiapp/wsgi.py'.
[Thu Jul 12 23:25:59.257214 2018] [wsgi:error] [pid 22747] [remote 10.x.x.x:60353] Traceback (most recent call last):
[Thu Jul 12 23:25:59.257500 2018] [wsgi:error] [pid 22747] [remote 10.x.x:60353] File "/usr/local/virtualenvs/myapp/wsgiapp/wsgi.py", line 23, in application
[Thu Jul 12 23:25:59.257535 2018] [wsgi:error] [pid 22747] [remote 10.x.x.x:60353] output += '\\n\\nnumpy file location='+str(np.__file__)
[Thu Jul 12 23:25:59.257569 2018] [wsgi:error] [pid 22747] [remote 10.x.x.x:60353] AttributeError: 'module' object has no attribute '__file__'
Also, just for reference, importing the modules works just fine if I activate the virtualenv and use the interpreter:
Python 3.4.8 (default, Mar 23 2018, 10:04:27)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> np.__file__
'/usr/local/virtualenvs/myapp/lib/python3.4/site-packages/numpy/__init__.py'
Printing dir(np) from the wsgi.py file results in:
dir(np)=['__doc__', '__loader__', '__name__', '__package__', '__path__', '__spec__']
So it doesn't error out, but it is not the full list of identifiers. Running dir(np) from the virtualenv interpreter results in:
['ALLOW_THREADS', 'AxisError', 'BUFSIZE', 'CLIP', 'ComplexWarning', 'DataSource', 'ERR_CALL', ..... clipped for brevity - there's a full screen of items in the list
Since it does list some identifiers, here are the results of printing each of them from within wsgi.py. It does seem to be picking up the package from the correct path.
np.__path__ =_NamespacePath(['/usr/local/virtualenvs/myapp/lib/python3.4/site-packages/numpy'])
np.doc=None
np.loader=<_frozen_importlib._NamespaceLoader object at 0x7f83b82e0c88>
np.package=numpy
np.name=numpy
np.Spec=ModuleSpec(name='numpy', loader=None, origin='namespace', submodule_search_locations=_NamespacePath(['/usr/local/virtualenvs/myapp/lib/python3.4/site-packages/numpy']))
Just to be sure wsgi/python is truly finding the numpy package, I changed the wsgi.py file to import a non-existent package import foo as np, and when I ran it again it errored out with ImportError: No module named 'foo'. So it does seem to be actually finding the site-packages... but just not fully loading them?
Fun fact: when pip installs a new module it sets the permissions such that the user is able to import and use the modules as expected but "other" permissions allow the modules to be seen but not execute. Thus, mod_wsgi running under Apache was able to import the modules but calling any methods resulted in an error.
Solution: double check permissions after installing or updating python modules! I set all directories to 755 and all files to 644 and that seemed to work. Or, as mentioned in the comment, ensure that your umask is set appropriately before running pip.
Related
I'm getting this error in my logs with django app. I thought such errors occur when using Python 2.x, but in my virtual env Python - 3.6.5. Django - 2.0.7. VESTA Control Panel with wsgi. I will be grateful for the help.
Error log:
[Thu Jul 26 19:02:48 2018] [error] [client 86.32.36.143] File "/home/project/private/django/project/env/lib/python3.6/site-packages/django/utils/version.py", line 61, in
[Thu Jul 26 19:02:48 2018] [error] [client 86.32.36.143] #functools.lru_cache()
[Thu Jul 26 19:02:48 2018] [error] [client 86.32.36.143] AttributeError: 'module' object has no attribute 'lru_cache'
django.wsgi
import os, sys
sys.path.insert(0, '/home/user/web/project/private/django/project/env/lib/python3.6/site-packages')
sys.path.insert(0, '/home/user/web/project/private/django/project/project/src/shared/')
sys.path.insert(0, '/home/user/web/project/private/django/project/project/src/')
os.environ['DJANGO_SETTINGS_MODULE'] = 'main.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
I had the same issue but then found out that my site was built with python3 and apache was configured for python2.7. The following link helped:
Target WSGI script cannot be loaded as Python module
I've set up my Python/Django virtual environment, and mod_wsgi in daemon mode, and am pretty sure (done this before) it's "mostly correct" except I get the following error...
[Thu Jul 06 00:35:26.986363 2017] [mpm_event:notice] [pid 11442:tid 140557758930432] AH00493: SIGUSR1 received. Doing graceful restart
Exception ignored in: <object repr() failed>
Traceback (most recent call last):
File "/home/jamin/www/dev.tir.com/py361ve/lib/python3.6/site-packages/PIL/Image.py", line 572, in __del__
NameError: name 'hasattr' is not defined
[Thu Jul 06 00:35:27.194483 2017] [mpm_event:notice] [pid 11442:tid 140557758930432] AH00489: Apache/2.4.25 (Ubuntu) mod_wsgi/4.5.15 Python/3.6 configured -- resuming normal operations
[Thu Jul 06 00:35:27.194561 2017] [core:notice] [pid 11442:tid 140557758930432] AH00094: Command line: '/usr/sbin/apache2'
My django app itself is loading fine through wsgi.py but it seems something to do with core python (error with my setup likely) is going wrong as per: NameError: name 'hasattr' is not defined
In the browser - I get a plain "Server Error (500)" page and not the standard Apache "Internal Server Error" page.
Leaving out my VirtualHost and steps beyond here are the basic steps I put together for myself if you can spot anything... (I've tried all the different python packages as well not just -venv)
Install Python 3.6 and virtualenv
sudo apt-get update
sudo apt-get install python3.6-venv
sudo apt-get install virtualenv
(or find the latest and greatest python package that includes pip https://packages.ubuntu.com/ )
Install Apache2
sudo apt-get install apache2 apache2-dev
Make and enter a folder for your project - then build a Virtual Environment in it
mkdir ~/example.com
cd ~/example.com
virtualenv --python=/usr/bin/python3.6 py361ve
Enter your new Virtual Environment to install packages to it
source py361ve/bin/activate
Install Django, mod_wsgi, and any other needed packages
pip install django
pip install mod_wsgi
pip install ...
(no need for pip3 in virtual environment - django should be the latest release)
Run following command and place output in apache config file ( in /etc/apache2/ )
mod_wsgi-express module-config
Exit your virtual environment
deactivate
(You can re-enter your virtual environment any time using the source method in step 8)
Here's what happens when I stop/start/restart apache2...
apache2 stop...
[Thu Jul 06 06:01:34.190940 2017] [mpm_event:notice] [pid 2015:tid 140157449797120] AH00491: caught SIGTERM, shutting down
_______________________________________________________________
apache2 start...
[Thu Jul 06 06:02:39.076741 2017] [mpm_event:notice] [pid 2181:tid 140553545080320] AH00489: Apache/2.4.25 (Ubuntu) mod_wsgi/4.5.15 Python/3.6 configured -- resuming $
[Thu Jul 06 06:02:39.076890 2017] [core:notice] [pid 2181:tid 140553545080320] AH00094: Command line: '/usr/sbin/apache2'
_______________________________________________________________
apache2 restart...
Exception ignored in: <object repr() failed>
Traceback (most recent call last):
File "/home/jamin/www/dev.tir.com/py361ve/lib/python3.6/site-packages/PIL/Image.py", line 572, in __del__
NameError: name 'hasattr' is not defined
[Thu Jul 06 06:05:43.307877 2017] [mpm_event:notice] [pid 2181:tid 140553545080320] AH00491: caught SIGTERM, shutting down
[Thu Jul 06 06:05:43.492499 2017] [mpm_event:notice] [pid 2301:tid 140353155558912] AH00489: Apache/2.4.25 (Ubuntu) mod_wsgi/4.5.15 Python/3.6 configured -- resuming $
[Thu Jul 06 06:05:43.492705 2017] [core:notice] [pid 2301:tid 140353155558912] AH00094: Command line: '/usr/sbin/apache2'
This is likely due to code still running in background threads when the Python interpreter is being destroyed on process shutdown. What happens during interpreter destruction is that all modules get wiped out and attribute access of things often returns None as fallback. In this case looks like builtins module was wiped before the PIL object got destroyed and so it couldn't find hasattr.
Can you confirm this only happens when Apache is being restarted and processes shutdown?
I encountered the same problem as well.
[Wed Sep 12 16:30:19 2018] [notice] Apache/2.2.15 (Unix) DAV/2 mod_wsgi/4.6.4
Python/3.6 configured -- resuming normal operations
Exception ignored in: <object repr() failed>
Traceback (most recent call last):
File "/mnt/workshop/py36env/lib/python3.6/site-packages/PIL/Image.py", line 600, in __del__
NameError: name 'hasattr' is not defined
Exception ignored in: <object repr() failed>
Traceback (most recent call last):
File "/mnt/workshop/py36env/lib/python3.6/site-packages/PIL/Image.py", line 600, in __del__
NameError: name 'hasattr' is not defined
Exception ignored in: <object repr() failed>
Traceback (most recent call last):
File "/mnt/workshop/py36env/lib/python3.6/site-packages/PIL/Image.py", line 600, in __del__
NameError: name 'hasattr' is not defined
Exception ignored in: <object repr() failed>
Traceback (most recent call last):
File "/mnt/workshop/py36env/lib/python3.6/site-packages/PIL/Image.py", line 600, in __del__
NameError: name 'hasattr' is not defined
Exception ignored in: <object repr() failed>
Traceback (most recent call last):
File "/mnt/workshop/py36env/lib/python3.6/site-packages/PIL/Image.py", line 600, in __del__
NameError: name 'hasattr' is not defined
[Wed Sep 12 16:33:57 2018] [notice] caught SIGTERM, shutting down
[Wed Sep 12 16:33:58 2018] [notice] suEXEC mechanism enabled (wrapper: /usr/sbin/suexec)
It maybe happened when you made a new dir but never authorized.
And I fixed it by the following:
chmod -R 755 /mnt/workshop/your_project_path
chown -R apache:apache /mnt/workshop/your_project_path
Try uninstalling PIL completely, and then reinstall Pillow package.
Actually there is conflict in PIL old package and Pillow package which is in conflict with newer versions of django.
You need to install the new Pillow package.
First Uninstall both
sudo pip3 uninstall pil
sudo pip3 uninstall pillow
Reinstall Pillow
sudo pip3 install pillow
I am running an apache server with MAMP in my Macbook pro. And I have added a python file named File.py to the CGI-Executable directory.
Contents of File.py:
#!/usr/bin/python
import web
import json
from xml.etree.ElementTree import Element, SubElement, Comment, tostring
urls = (
'/json', 'get_json'
)
app = web.application(urls, globals())
class get_json:
def GET(self):
pyDict = {'one':1,'two':2}
web.header('Content-Type', 'application/json')
return json.dumps(pyDict)
if __name__ == "__main__":
app.run()
I have installed web.py (by running pip install web.py) in my Mac. Now when I go to browser and enter http://localhost:8888/cgi-bin/File.py/json it is showing error 500 internal error, I checked the log file, log is
[Thu Mar 17 00:31:20 2016] [error] [client ::1] Traceback (most recent call last):
[Thu Mar 17 00:31:20 2016] [error] [client ::1] File " path-to-CGI-Executables/File.py", line 3, in <module>
[Thu Mar 17 00:31:20 2016] [error] [client ::1] import web
[Thu Mar 17 00:31:20 2016] [error] [client ::1] ImportError: No module named web
[Thu Mar 17 00:31:20 2016] [error] [client ::1] Premature end of script headers: File.py
Now I tried running the python file from terminal like
$ python File.py 127.0.0.1
Then in browser http://127.0.0.1:8080/json its working fine and showing {"two": 2, "one": 1} as result.
Whats happening if I add this to MAMP CGI? How to make installed modules available within MAMP?
UPDATE:
MAMP version : Version 3.5 (3.5)
Python version : Python 2.7.11 Mac
OS X version : 10.10.5 (14F1605)
I was getting started with AWS' Elastic Beanstalk.
I am following this tutorial to deploy a Django/PostgreSQL app.
I did everything before the 'Configuring a Database' section. The deployment was also successful but I am getting an Internal Server Error.
Here's the traceback from the logs:
mod_wsgi (pid=30226): Target WSGI script '/opt/python/current/app/polly/wsgi.py' cannot be loaded as Python module.
[Tue Sep 15 12:06:43.472954 2015] [:error] [pid 30226] [remote 172.31.14.126:53947] mod_wsgi (pid=30226): Exception occurred processing WSGI script '/opt/python/current/app/polly/wsgi.py'.
[Tue Sep 15 12:06:43.474702 2015] [:error] [pid 30226] [remote 172.31.14.126:53947] Traceback (most recent call last):
[Tue Sep 15 12:06:43.474727 2015] [:error] [pid 30226] [remote 172.31.14.126:53947] File "/opt/python/current/app/polly/wsgi.py", line 12, in <module>
[Tue Sep 15 12:06:43.474777 2015] [:error] [pid 30226] [remote 172.31.14.126:53947] from django.core.wsgi import get_wsgi_application
[Tue Sep 15 12:06:43.474799 2015] [:error] [pid 30226] [remote 172.31.14.126:53947] ImportError: No module named django.core.wsgi
Any idea what's wrong?
Have you created a requirements.txt in the root of your application? Elastic Beanstalk will automatically install the packages from this file upon deployment. (Note it might need to be checked into source control to be deployed.)
pip freeze > requirements.txt
(You will probably want to do that from within a virtualenv so that you only pick up the packages your app actually needs to run. Doing that with your system Python will pick up every package you've ever installed system-wide.)
The answer (https://stackoverflow.com/a/47209268/6169225) by carl-g is correct. One thing that got me was that requirements.txt was in the wrong directory. Let's say you created a django project called mysite. This is the directory in which you run the eb command(s) --> make sure requirements.txt is in this directory.
If you forget the .ebextensions folder you will get the same error.
I was following along with a good simple (non Elastic Beanstalk) tutorial and missed step 3 & 4 of Elastic Beanstalk.
I was using Django 1.11 and Python 2.7
In a harrowing attempt to get mod_wsgi to run on CentOS 5.4, I've added Python 2.6 as an optional library following the instructions here. The configuration seems fine except that when trying to ping the server the Apache log prints this error:
mod_wsgi (pid=20033, process='otalo', application='127.0.0.1|'): Loading WSGI script '...django.wsgi'.
[Sat Mar 27 16:11:45 2010] [error] [client 171.66.52.218] mod_wsgi (pid=20033): Target WSGI script '...django.wsgi' cannot be loaded as Python module.
[Sat Mar 27 16:11:45 2010] [error] [client 171.66.52.218] mod_wsgi (pid=20033): Exception occurred processing WSGI script '...django.wsgi'.
[Sat Mar 27 16:11:45 2010] [error] [client 171.66.52.218] Traceback (most recent call last):
[Sat Mar 27 16:11:45 2010] [error] [client 171.66.52.218] File "...django.wsgi", line 8, in <module>
[Sat Mar 27 16:11:45 2010] [error] [client 171.66.52.218] import django.core.handlers.wsgi
[Sat Mar 27 16:11:45 2010] [error] [client 171.66.52.218] ImportError: No module named django.core.handlers.wsgi
when I go to my python2.6 install's command line and try 'import Django', the module is not found (ImportError). However, my default Python 2.4 installation (still working fine) is able to import successfully. How do I point Python 2.6 to Django?
You need to install Django specifically with/for the Python version that's meant to use it -- installs for 2.4 and 2.6 are always going to be separate (they have to be -- there are incompatibilities in binary and bytecode formats!). I don't know what, if any, possibilities CentOS offers for that -- I'd get Django's sources and install from sources (which boils down to: download, tar, cd, and finally a simple sudo python2.6 setup.py install -- or however else you run your 2.6 install of Python). Being a much better developer than I'm a system administrator, I always have a bias in favor of installing from sources for stuff that's really at the core of what I'm doing (especially Python versions and extensions).
I created a write up link text of my experiences of getting Django working on Redhat and and CentOS. I upgraded the version of python that I was using and started with init.d scripts.