[GO] Run different versions of Python (wsgi) apps on one development environment

Introduction

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.

Main characters

django app views.py

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")

Run to manage.py run server

What I put in with apt (Python 2.7 + django 1.4)

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.

Making full use of pyvenv and pip (Python3.4 + django 1.7)

(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)

Launch the wsgi server from the command line using tornado

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)

Those who prepared with apt-get

$ DJANGO_SETTINGS_MODULE=test1.settings python tornado_main.py

Check Python and django versions

For pyvenv and pip

(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

Start both wsgi apps as a daemon with supervisor

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.

Bonus Reverse proxy with Apcahe2

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.

reference

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

Run different versions of Python (wsgi) apps on one development environment
Prepare Python development environment on Ubuntu
Python development environment construction on macOS
Environment construction of python3.8 on mac
Install Python development environment on Windows 10
Addition of local development environment on MacOS
Run python wsgi server on NGINX Unit
I tried putting various versions of Python + OpenCV + FFmpeg environment on Mac
Use multiple versions of python environment with pyenv
Run Python web apps on NGINX + NGINX Unit + Flask
Build a Python development environment on your Mac
python development environment -use of pyenv and virtualenv-
Set up a Python development environment on Marvericks
Let's use different versions of SQLite3 from Python3!
Create a Python virtual development environment on Windows
Build a Python development environment on Raspberry Pi
Unification of Python environment
Notes on creating a python development environment on macOS Catalina
Python development environment construction
Create a comfortable Python 3 (Anaconda) development environment on windows
Build a Python development environment on Mac OS X
About Python development environment
python2.7 development environment construction
Build a Python development environment using pyenv on MacOS
Development environment in Python
Create a Python development environment on OS X Lion
[Python] Chapter 01-02 About Python (Execution and installation of development environment)
Notes on apps and development environment installed on macbook air Mid2013 (php, ruby, node.js, python, etc.)
Using multiple versions of Python on Mac OS X (2) Usage
Create a Python (pyenv / virtualenv) development environment on Mac (Homebrew)
Unify the environment of the Python development team starting with Poetry
Build an Ubuntu python development environment on Google Cloud Platform
Construction of Python local development environment Part 2 (pyenv-virtualenv, pip usage)
Building a development environment for Android apps-creating Android apps in Python
Memo of python + numpy/scipy/pandas/matplotlib/jupyterlab environment construction on M1 macOS (as of 2020/12/24)
Build a GVim-based Python development environment on Windows 10 (2) Basic settings
Until building a Python development environment using pyenv on Ubuntu 20.04
Run Openpose on Python (Windows)
Build Python environment on Windows
Run Tensorflow 2.x on Python 3.7
Install multiple versions of Python
Handling of python on mac
Organize your Python development environment
Django environment development on Windows 10
Run Python CGI on CORESERVER
[ev3dev × Python] Build ev3dev development environment
Run unix command on python
Build python environment on windows
[MEMO] [Development environment construction] Python
[For organizing] Python development environment
Environment construction of python2 & 3 (OSX)
Create another version of Python conda environment with one command line
Build and test a CI environment for multiple versions of Python
Construction of Python local development environment Part 1 (pyenv, pyenv-virtualenv, pip installation)
Using multiple versions of Python on Mac OS X (1) Multiple Ver installation
Continuation ・ Notes on preparing the Python development environment on Mac OS X
Memorandum of understanding when Python is run on EC2 with Apache
Building a Python environment on Mac
Environment construction of python and opencv
Python environment construction memo on Windows 10
Anaconda python environment construction on Windows 10