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
Related
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.
I am trying to use Bottle.py and mod_wsgi together but I am getting a strange error that I cannot fix.
When I try to make a curl request ("curl http://localhost/myapi/hello1") I get the error: "ImportError: No module named server". However, if I take out the "import server" line in my app.wsgi file and try "curl http://localhost/myapi/hello2" it works just fine. I have tried adding to the WSGIPythonPath, WSGIDaemonProcess, etc. and nothing has worked. Does anyone see anything I may be doing wrong?
Note: The "/usr/local/www/wsgi-script" path is successfully being added to sys.path, so that isn't the issue. I also do have a init.py file in the "/usr/local/www/wsgi-scripts" directory and the permissions are all 777.
[root#my_vm httpd]# ls -lt /usr/local/www/wsgi-scripts/
total 8
-rwxrwxrwx. 1 root root 95 Mar 21 14:42 server.py
-rwxrwxrwx. 1 root root 318 Mar 21 14:37 app.wsgi
-rwxrwxrwx. 1 root root 0 Mar 20 19:53 __init__.py
Using Versions:
Apache/2.4.6 (Red Hat Enterprise Linux)
Python 2.7.5
Bottle 0.12.9
/usr/local/www/wsgi-scripts/server.py
import bottle
from bottle import route
#route('/hello1')
def hello():
return "Hello World!"
/usr/local/www/wsgi-scripts/app.wsgi
import os
import sys
import bottle
from bottle import route
server_loc = "/usr/local/www/wsgi-scripts"
if not server_loc in sys.path:
sys.path.insert(0, server_loc)
os.chdir(os.path.dirname(__file__))
print(sys.path)
#route('/hello2')
def hello():
return "Hello World!\n"
import server
application = bottle.default_app()
/etc/httpd/conf.d/myapi.conf
<VirtualHost *:80>
ServerName localhost
WSGIScriptAlias /myapi /usr/local/www/wsgi-scripts/app.wsgi
<Directory /usr/local/www/wsgi-scripts>
Require all granted
</Directory>
</VirtualHost>
EDIT: Adding python traceback
/etc/httpd/logs/error_log
[Thu Mar 22 13:39:18.234573 2018] [core:notice] [pid 18172] AH00094: Command line: '/usr/sbin/httpd -D FOREGROUND'
[Thu Mar 22 13:39:29.628352 2018] [:error] [pid 18174] ['/usr/local/www/wsgi-scripts', '/usr/lib64/python27.zip', '/usr/lib64/python2.7', '/usr/lib64/python2.7/plat-linux2', '/usr/lib64/python2.7/lib-tk', '/usr/lib64/python2.7/lib-old', '/usr/lib64/python2.7/lib-dynload', '/usr/lib64/python2.7/site-packages', '/usr/lib64/python2.7/site-packages/gtk-2.0', '/usr/lib/python2.7/site-packages']
[Thu Mar 22 13:39:29.628514 2018] [:error] [pid 18174] [client ::1:35838] mod_wsgi (pid=18174): Target WSGI script '/usr/local/www/wsgi-scripts/app.wsgi' cannot be loaded as Python module.
[Thu Mar 22 13:39:29.628525 2018] [:error] [pid 18174] [client ::1:35838] mod_wsgi (pid=18174): Exception occurred processing WSGI script '/usr/local/www/wsgi-scripts/app.wsgi'.
[Thu Mar 22 13:39:29.628539 2018] [:error] [pid 18174] [client ::1:35838] Traceback (most recent call last):
[Thu Mar 22 13:39:29.628556 2018] [:error] [pid 18174] [client ::1:35838] File "/usr/local/www/wsgi-scripts/app.wsgi", line 18, in <module>
[Thu Mar 22 13:39:29.628625 2018] [:error] [pid 18174] [client ::1:35838] import server
[Thu Mar 22 13:39:29.628645 2018] [:error] [pid 18174] [client ::1:35838] ImportError: No module named server
Adding the solution that I found worked for me:
When I moved the server.py into another directory inside the directory (aka /usr/local/www/wsgi-scripts/server.py --> /usr/local/www/wsgi-scripts/inner_dir/server.py), and do "import inner_dir.server" instead of "import server", it started working.
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
I am getting "Internal Server Error" when I try to access the django website.
I am using Django 1.8, Python 2.7.10, centos 6.5 and apache.
In apache log I am getting the following error:
mod_wsgi (pid=23866): Target WSGI script '/abc/abc/abc/wsgi.py' cannot be loaded as Python module.
[Mon May 25 14:40:47 2015] [error] [client xyz] mod_wsgi (pid=23866): Exception occurred processing WSGI script '/abc/abc/abc/wsgi.py'.
[Mon May 25 14:40:47 2015] [error] [client xyz] Traceback (most recent call last):
[Mon May 25 14:40:47 2015] [error] [client xyz] File "/abc/abc/abc/wsgi.py", line 12, in <module>
[Mon May 25 14:40:47 2015] [error] [client xyz] from django.core.wsgi import get_wsgi_application
[Mon May 25 14:40:47 2015] [error] [client xyz] File "/abc/lib/python2.7/site-packages/django/__init__.py", line 1, in <module>
[Mon May 25 14:40:47 2015] [error] [client xyz] from django.utils.version import get_version
[Mon May 25 14:40:47 2015] [error] [client xyz] File "/abc/lib/python2.7/site-packages/django/utils/version.py", line 7, in <module>
[Mon May 25 14:40:47 2015] [error] [client xyz] from django.utils.lru_cache import lru_cache
[Mon May 25 14:40:47 2015] [error] [client xyz] File "/abc/lib/python2.7/site-packages/django/utils/lru_cache.py", line 28
[Mon May 25 14:40:47 2015] [error] [client xyz] fasttypes = {int, str, frozenset, type(None)},
[Mon May 25 14:40:47 2015] [error] [client xyz] ^
[Mon May 25 14:40:47 2015] [error] [client xyz] SyntaxError: invalid syntax
Thanks
Although you say you are running Python 2.7, this error indicates that in fact you are using 2.6, as set literals were only introduced in 2.7.
The issue is that mod_wsgi is compiled against a specific Python version, which in your case is the one that comes with Centos 6.5. You will need to recompile it for 2.7, or find a version already compiled against that.
You need to start using virtualenv for your django project. Here's a nice tutorial. For a makeshift solution, you can just run django with python 2.7 by using python2.7 manage.py runserver. However, it's highly recommended to look into virtualenv. Makes life much easier.
With regards to making mod_wsgi use python 2.7, you need to recompile it. For that, just download the source and execute:
$ ./configure
$ make
$ sudo make install
They have a quick installation guide that you can access here.