Wsgi script and the newest django with Python 3.5 - python

We are trying to deploy one more web-application on our VPS.
There is one flask application in production already, which is held by Python 3.5 interpreter.
Now we need another one - django app. We have configured our apache2 to host both of then (django is working on subdomain, whereas flask is on the 'root' domain. Everything is okay here.
But, since flask application is using global python interpreter (version 3.5) we cannot run django since it requires version 3.6 or newer.
Here is django WSGI script:
import os
import sys
print('Python version is ... ') # 3.5
python_home = '/var/www/mysite/venv'
activate_this = python_home + '/bin/activate_this.py'
exec( open(activate_this).read() )
print(sys.executable) # in case of virtualenv it refers to /usr/bin/python3.5
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/mysite/mysite")
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
application = get_wsgi_application()
We could have upgraded our interpreter to 3.6, but since established flask app is in production we are not allowed to do so by our managers.. And it is okay probably
If we go with virtualenv nevertheless it created virtual environment with existing global interpreter , as is written above version 3.5.
And this is where we are stuck now. Apache2 log is constanlty saying to us:
[wsgi:error] mod_wsgi (pid=10081): Target WSGI script '/var/www/mysite/mysite/wsgi.py' cannot be loaded as Python module.
[wsgi:error] mod_wsgi (pid=10081): Exception occurred processing WSGI script '/var/www/mysite/mysite/wsgi.py'.
[wsgi:error] Traceback (most recent call last):
[wsgi:error] File "/var/www/mysite/mysite/wsgi.py", line 18, in <module>
[wsgi:error] from django.core.wsgi import get_wsgi_application
[wsgi:error] ImportError: No module named 'django'
If we run python3 in terminal and then from django.core.wsgi import get_wsgi_application
we are going to git this:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/ivan/.local/lib/python3.5/site-packages/django/core/wsgi.py", line 2, in <module>
from django.core.handlers.wsgi import WSGIHandler
File "/home/ivan/.local/lib/python3.5/site-packages/django/utils/crypto.py", line 6, in <module>
import secrets
ImportError: No module named 'secrets'
And we assume that upgrading would solved our problem since django requires version 3.6
https://github.com/quolc/neural-collage/issues/2 and https://docs.djangoproject.com/en/3.0/intro/tutorial01/ ("This tutorial is written for Django 3.0, which supports Python 3.6 and later ... ")
What will be the best step here? Trying to download and establish python 3.6 or newer and refer our wsgi to that or try to use another version of django ...?
Thank you in advance!

In my case that was pretty easy. I was foolish to install and configured all my python services with sudo privileges , that is why when I did
pip3 install <package-name>
that all went okay, but since all services have been run under root I could not get package in usage, so I did
sudo pip3 install <package-name>
And have managed to localize the issue

Related

Problems with fastcgi when installing Django on Justhost

I am following the following tutorial at http://flailingmonkey.com/install-django-justhost/ to install Django on my Justhost web server. So far I have managed to install Django and Python on my Justhost shared web server.
However, I am now stuck when trying to configure my new site. Every time I run the command: python mysite.fcgi I keep getting the following error message:
Traceback (most recent call last):
File "mysite.fcgi", line 9, in <module>
from django.core.servers.fastcgi import runfastcgi
ImportError: No module named fastcgi
Content of mysite.fcgi
#!/home4/xxxxx/python/bin/python
import sys, os
# Where /home/your_username is the path to your home directory
sys.path.insert(0, "/home4/xxxxx/python")
sys.path.insert(13, "/home4/xxxxx/public_html/django-project/admin")
os.environ['DJANGO_SETTINGS_MODULE'] = 'admin.settings'
from django.core.servers.fastcgi import runfastcgi
runfastcgi(method="threaded", daemonize="false")
How do I fix it?
I had the exact same issue. Heres how to solve it
load up your ssh client
cd ~/
pip install django==1.8.7
pip install flup==1.0.2
and you should be good
If you need FastCGI support for Django version 1.9 or higher, see https://github.com/NetAngels/django-fastcgi. You still need to install flup separately as #dvicemuse advised.
Also, official Django version 1.8 FastCGI docs may help, see https://docs.djangoproject.com/en/1.8/howto/deployment/fastcgi/#running-django-on-a-shared-hosting-provider-with-apache

Import Error When Switchin from Python 2.7 to Python 3.4 on Heroku running django-oscar on django 1.7.6

Somewhat similar issue to this one although not quite. Installation was running fine on Heroku and Python 2.7 and also fine with Python 3.4 on my dev machine (OS X). Regretfully switched over to Python 3.4 on Heroku as well and am now getting
Django Version: 1.7.6
Exception Type: ImportError
Exception Value: cannot import name 'shop' in urls.py in <module>, line 5
Oscar is installed in the virtual env and imports without a problem from the shell
wsgi.py looks as follows:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "super_secret_project.settings")`
from django.core.wsgi import get_wsgi_application
# from whitenoise.django import DjangoWhiteNoise
application = get_wsgi_application()
# application = DjangoWhiteNoise(application)
I tried force loading models with an added dummy import in urls.py:
from oscar.apps.order import models
Starting to think it might be an issue with Heroku/wsgi files
I fixed it by importing this way:
from oscar.app import application as shop

ImportError: cannot import name NullHandler

I'm trying to deploy a hello-world type app on Elastic Beanstalk. Just about everything seems to work, packages are installed, etc. up to the point where mod_wsgi attempts to retrieve the "application" object from wsgi.py. At that point, the following appears in the logs (once in the logs for each unsuccessfuly HTTP request):
mod_wsgi (pid=6114): Target WSGI script '/opt/python/current/app/myapp/wsgi.py' cannot be loaded as Python module.
mod_wsgi (pid=6114): Exception occurred processing WSGI script '/opt/python/current/app/myapp/wsgi.py'.
Traceback (most recent call last):
File "/opt/python/current/app/caserails/wsgi.py", line 20, in <module>
application = get_wsgi_application()
File "/opt/python/run/venv/lib/python2.7/site-packages/django/core/wsgi.py", line 14, in get_wsgi_application
django.setup()
File "/opt/python/run/venv/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
from django.utils.log import configure_logging
File "/opt/python/run/venv/lib/python2.7/site-packages/django/utils/log.py", line 16, in <module>
from logging import NullHandler # NOQA
ImportError: cannot import name NullHandler
Link to concurrent AWS Forum Post.
The NullHandler is only available on Python version 2.7+. You could create the NullHandler yourself on an ImportError:
import logging
try:
from logging import NullHandler
except ImportError:
class NullHandler(logging.Handler):
def emit(self, record):
pass
logging.getLogger(__name__).addHandler(NullHandler())
More information about logging.NullHandler: https://docs.python.org/3/library/logging.handlers.html#logging.NullHandler.
I had similar problem and in my case the issue was that for some unrelated project I created logging.py file in home folder and when I ran something in home, it was importing this file instead of the real module.
You can check which file is being imported like this:
import logging
print(logging.__file__)
I fixed it by deleting logging.py I created previously.
NullHandler was introduced in version 2.7. Are you sure you are running 2.7 on your server?
After much trial and error, the (immediate) problem was solved by removing python logging from requirements.txt and rebuilding the environment.
I do not yet fully understand why this is a problem. On my local machine, I'm able to install logging and run Django without error. I suspect as vikramls pointed out that something weird is happening in the intersection between mod_wsgi, the baselineenv in which mod_wsgi executes, and the virtualenv in which my app operates.
But, at least for now, I'm fixing this error by not including "logging" in requirements.txt when deploying Django 1.7 on Elastic Beanstalk.
I had this issue while creating a build for python 2.7 with PyInstaller.So, I uninstalled logger from my ENV.else you can also remove the package name from the requirement.txt.
This approach solves my issue [Happy Coding:)]

Error when importing WSGIHandler with django

Without any code changing, my django app started throwing an exception while loading the WSGI script. I'm using django 1.3 with python 2.7, and the top-level .wsgi is essentially unmodified from the default:
import os
import sys
from django.core.handlers.wsgi import WSGIHandler
os.environ['DJANGO_SETTINGS_MODULE'] = 'api.settings'
application = WSGIHandler()
It started producing these errors on any request, as reported by 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 it now works normally (no errors). I'm at a loss for what to do - I want to make sure this doesn't happen again, but it's not happening now, and I have no idea why that import error appeared.
Are you executing Django inside a virtualenv? Did you updated or upgraded your system? If so and you upgraded to Python 2.7 from Python 2.6 for example you need to regenerate the virtualenv:
$ virtualenv [your-options] [your-django-project-directory]
For anyone using webfaction like Jesse and myself to run his django app but is not using virtualenv, it can happen that after the upgrade, the custom apache installation for the webapp was never restarted. This means that the stdlib changed but apache still uses python 2.7.2 because its the version it has loaded into memory.
The solution in this case is simple: login to your account via ssh and execute:
[username#webXX ~]$ ~/webapps/<webapp-name>/apache2/bin/restart
This restarts the web server and causes apache to reload the new interpreter binary.
I also found the same error when I updated Python 2.6.5 to 2.6.8, building from source. I also had built mod_wsgi from source, and initially forgot to recompile mod_wsgi for the new version of Python, and that led to the same error concerning urandom.

Django installation has no __init__.py in django.core

I have a ubuntu lucid VPS server where I try to run my django development server.
Running python manage.py runserver gives me the following error:
Traceback (most recent call last):
File "manage.py", line 2, in <module>
from django.core.management import execute_manager
ImportError: No module named core.management
I can import django without any problem in a python shell. I looked in the django install directory and noticed there is no \__init__.py in the django/core folder. Which I beleive is the source of the problem for python to register django.core as a module.
It then looks like an installation issue. I installed django using apt-get.
FYI the django install worked perfectly on my home computer with same OS.
Any ideas?
solved.
Thank you for all the suggestions. I installed django using the tar ball as instructed in the django website.
apt-get is not a good idea to install django. I also had to manually remove the left over django folder in /usr/lib/pymodules/python2.6 after using apt-get remove.

Categories

Resources