I have Python 2.7 and Django 1.4 installed in my development Linux environment via OS package management commands. The app I'm making now works in this environment, so there was no problem.
At one point, I decided to combine Django 1.7, which is about to be released, with Python 3.4, to see what the challenges lie.
In short, we want to achieve a situation where we do not destroy the existing development environment while conducting prior art research under the same environment.
However, this "one OS, multiple Python and Django" is unexpectedly difficult. When I tried it last time (http://qiita.com/amedama/items/79994598d9f4daa69d13), it stopped with the problem that I couldn't have multiple types of Apache2 mod_wsgi.
This time, as an alternative, I will try to deal with this problem by setting up an independent wsgi server on the same host and setting a reverse proxy from Apache2 as a bonus.
In particular
We will explore how to start these two environments in parallel on one development environment.
As a bonus, I've actually run into incompatible changes in Django 1.7, so I'll introduce that as well.
In addition, at the end of the article, I will introduce a 7-minute video that actually practiced (a part of) the contents of this article on the terminal. Whether you want to read the article, watch the video, or enjoy both, please do whatever you like.
This time, the content of views.py itself is not essential, so we will use the following. It just returns the Python version and the django version in plain text.
views.py
from django.http import HttpResponse
import django
import sys
def home(request):
version = ('Python: {}\nDjango: {}\n'
.format(sys.version.replace('\n', ' '),
django.get_version()))
return HttpResponse(version, content_type="text/plain")
It's assumed that tornado, django (older one), etc. are ready in advance. I think you can use sudo pip or apt-get (for Debian).
Jabashi: With pip, 1.6 series will be included if you do not specify the version. In this article, Debian wheezy (7.5) apt-get install python-django is the closest to the result of the article. As far as I know, Ubuntu 12.04 LTS contains Django 1.3, and Ubuntu 14.04 LTS contains 1.6. The point is that it depends on the environment. The important thing is that Django 1.7 won't come in. For Python 3.4, Ubuntu 14.04 LTS will enter with apt-get.
django-admin startproject test1
python mangae.py syncdb
python manage.py runserver
(I made venv twice, but is there a better way?)
$ xbuild/python-install 3.4.0 /opt/python3.4.0
$ cd /opt
$ /opt/python3.4.0/bin/pyvenv /tmp/venv
$ source /tmp/venv/bin/activate
(venv)$ pip install https://www.djangoproject.com/download/1.7b3/tarball/
(rehash on zsh)
(venv)$ python --version
Python 3.4.0
(venv)$ django-admin.py --version
1.7b3
(venv)$which pip
/opt/test2/venv/bin/pip
(venv)$ django-admin.py startproject test2
(venv)$ cd test2
(venv)$ deactivate
(rehash on zsh)
$ /opt/python3.4.0/bin/pyvenv venv
$ source venv/bin/activate
(venv) $ pip install https://www.djangoproject.com/download/1.7b3/tarball/
(rehash on zsh)
python mangae.py syncdb
python manage.py runserver
In addition, I referred to the following articles.
This time, `` `tornado_main.py``` is placed in the root of the project and the following files are placed to make the tea muddy.
(The content is almost the same as https://github.com/bdarnell/django-tornado-demo, but one point is that the port number can be changed from the command line. It has nothing to do with this time. HelloHandler just forgot to turn it off)
tornado_main.py
from tornado.options import options, define, parse_command_line
import django.core.handlers.wsgi
import tornado.httpserver
import tornado.ioloop
import tornado.web
import tornado.wsgi
define('port', type=int, default=18000)
class HelloHandler(tornado.web.RequestHandler):
def get(self):
self.write('Hello from tornado')
def main():
parse_command_line()
wsgi_app = tornado.wsgi.WSGIContainer(
django.core.handlers.wsgi.WSGIHandler())
tornado_app = tornado.web.Application(
[('/hello-tornado', HelloHandler),
('.*', tornado.web.FallbackHandler, dict(fallback=wsgi_app)),
])
server = tornado.httpserver.HTTPServer(tornado_app)
server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()
if __name__ == '__main__':
main()
(As we'll see later, this implementation of tornado_main.py doesn't work when running on django 1.7)
$ DJANGO_SETTINGS_MODULE=test1.settings python tornado_main.py
Check Python and django versions
(venv)$ pip install tornado
(venv)$ PYTHONPATH=venv/lib/python3.4/site-packages DJANGO_SETTINGS_MODULE=test2.settings python tornado_main.py
Check Python and django versions
This time, supervisor is put in with apt. I don't think the supervisor has the advantage of managing it separately as described above.
(In addition, 3.0a8 which can be entered as it is with apt-get is fine, but I got the source (3.0r1.1) from unstable and built it. It should not be related to this article, but for reference)
After installing supervisor with apt, put the following settings under `` `/etc/supervisor/conf.d/```.
/etc/supervisor/conf.d/test.conf
[program:test1]
command=python tornado_main.py --port=18101
directory=/opt/test1
autostart=true
autorestart=true
user=www-data
environment = DJANGO_SETTINGS_MODULE="test1.settings"
[program:test2]
command=/opt/test2/venv/bin/python tornado_main.py --port=18102
directory=/opt/test2
autostart=true
autorestart=true
user=www-data
environment = PYTHONPATH="/opt/test2/venv/lib/python3.4/site-packages", DJANGO_SETTINGS_MODULE="test2.settings"
Congratulations on both movements d
App registory isn't ready yet.
2.7 is better, but 3.4 doesn't work.
Specifically, it throws out a long exception and falls. Check the status by looking at /var/log/supervisor/supervisord.log.
The cause was a specification change in django 1.7.
https://docs.djangoproject.com/en/dev/releases/1.7/
If you’re using Django in a plain Python script — rather than a management command — and you rely on the DJANGO_SETTINGS_MODULE environment variable, you must now explicitly initialize Django at the beginning of your script with:
>>> import django
>>> django.setup()
Oh, yes.
tornado_main2.py
from tornado.options import options, define, parse_command_line
import django
import django.core.handlers.wsgi
import tornado.httpserver
import tornado.ioloop
import tornado.web
import tornado.wsgi
define('port', type=int, default=18000)
class HelloHandler(tornado.web.RequestHandler):
def get(self):
self.write('Hello from tornado')
def main():
// For django 1.7
django.setup()
parse_command_line()
wsgi_app = tornado.wsgi.WSGIContainer(
django.core.handlers.wsgi.WSGIHandler())
tornado_app = tornado.web.Application(
[('/hello-tornado', HelloHandler),
('.*', tornado.web.FallbackHandler, dict(fallback=wsgi_app)),
])
server = tornado.httpserver.HTTPServer(tornado_app)
server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()
if __name__ == '__main__':
main()
Now both work in the same development environment.
I can't even remember the port number, so it might be a good idea to do a reverse proxy. (However, supervisord and Apache will manage the port numbers independently, so it may become annoying later.)
Enable mod_proxy etc. and set `ProxyPass``` and
`ProxyPassReverse```. abridgement
Make sure that the versions of Python and django are different.
I can't go to the end subtly, but I actually tried running it on the terminal
https://www.youtube.com/watch?v=l9gvX1oMOF4
Recommended Posts