Error importing WSGIHandler with django

Without changing the code, my django application started throwing an exception when loading the WSGI script. I am using django 1.3 with python 2.7, and the top level .wsgi is essentially not changed by default:

 import os import sys from django.core.handlers.wsgi import WSGIHandler os.environ['DJANGO_SETTINGS_MODULE'] = 'api.settings' application = WSGIHandler() 

He began to produce these errors on any request, according to Apache:

 mod_wsgi (pid=3283): Target WSGI script '/home/beder/webapps/api/api.wsgi' cannot be loaded as Python module. mod_wsgi (pid=3283): Exception occurred processing WSGI script '/home/beder/webapps/api/api.wsgi'. Traceback (most recent call last): File "/home/beder/webapps/api/api.wsgi", line 4, in <module> from django.core.handlers.wsgi import WSGIHandler File "/home/beder/webapps/api/lib/python2.7/django/core/handlers/wsgi.py", line 10, in <module> from django import http File "/home/beder/webapps/api/lib/python2.7/django/http/__init__.py", line 122, in <module> from django.utils.http import cookie_date File "/home/beder/webapps/api/lib/python2.7/django/utils/http.py", line 7, in <module> from email.Utils import formatdate File "/usr/local/lib/python2.7/email/__init__.py", line 79, in __getattr__ __import__(self.__name__) File "/usr/local/lib/python2.7/email/utils.py", line 27, in <module> import random File "/usr/local/lib/python2.7/random.py", line 47, in <module> from os import urandom as _urandom ImportError: cannot import name urandom 

I restarted the server and now it is working fine (no errors). I am at a loss what to do - I want to make sure that this does not happen again, but this is not happening now, and I do not know why the import error appeared.

+4
source share
3 answers

Is Django running inside virtualenv? Have you updated or updated your system? If so, and you upgraded to Python 2.7 from Python 2.6, then you need to restore virtualenv:

$ virtualenv [your-options] [your-django-project-directory]

+3
source

For those who use webfaction, like Jesse and I, to run his django application but not use virtualenv, it may happen that after updating the custom apache installation for webapp never restarted. This means that stdlib has changed, but apache is still using python 2.7.2 because its version is loaded into memory.

The solution in this case is simple: log in to your account via ssh and run:

 [ username@webXX ~]$ ~/webapps/<webapp-name>/apache2/bin/restart 

This restarts the web server and causes apache to restart the new interpreter.

+2
source

I also found the same error when I upgraded Python 2.6.5 to 2.6.8 by building the source code. I also created mod_wsgi from the source code and initially forgot to recompile mod_wsgi for the new version of Python, and this led to the same error regarding urandom.

0
source

Source: https://habr.com/ru/post/1414631/


All Articles