安装频道:Python Django 版本和设置

Posted

技术标签:

【中文标题】安装频道:Python Django 版本和设置【英文标题】:Installing Channels: Python Django version and settings 【发布时间】:2018-02-16 11:35:50 【问题描述】:

我正在尝试按照本教程将Channels 添加到我的应用程序中: https://blog.heroku.com/in_deep_with_django_channels_the_future_of_real_time_apps_in_django

Django 应用程序可以编译,但是当我尝试运行它时:

(virtualenv) root@computer:path# python manage.py runserver 0.0.0.0:8000

报错:

  File "/root/.virtualenvs/codebench/local/lib/python2.7/site-packages/channels/routing.py", line 75, in resolve_routing
    raise ImproperlyConfigured("Cannot import channel routing %r: %s" % (routing, e))
ImproperlyConfigured: Cannot import channel routing 'codebench.routing.channel_routing': cannot import name ProtocolTypeRouter

据此 https://github.com/VincentTide/django-channels-celery-example/issues/1 它可能与Python版本有关,所以我做了:

(virtualenv) root@computer:path# alias python=python3
(virtualenv) root@computer:path# python -V
Python 3.5.2
(virtualenv) root@computer:path# python manage.py runserver 0.0.0.0:8000
Traceback (most recent call last):
  File "manage.py", line 3, in <module>
    import eventlet
ImportError: No module named 'eventlet'
(virtualenv) root@computer:path# pip install eventlet
Requirement already satisfied: eventlet in /root/.virtualenvs/codebench/lib/python2.7/site-packages
Requirement already satisfied: greenlet>=0.3 in /root/.virtualenvs/codebench/lib/python2.7/site-packages (from eventlet)
Requirement already satisfied: enum-compat in /root/.virtualenvs/codebench/lib/python2.7/site-packages (from eventlet)
Requirement already satisfied: enum34 in /root/.virtualenvs/codebench/lib/python2.7/site-packages (from enum-compat->eventlet)

现在我坚持:ImportError: No module named 'eventlet'

我安装了以下软件包:

channels==1.1.6
daphne==1.4.0
Django==1.9
eventlet==0.20.0

如果我按照本教程进行操作:http://channels.readthedocs.io/en/1.x/getting-started.html

System check identified 1 issue (0 silenced).
February 16, 2018 - 23:19:32
Django version 1.9, using settings 'codebench.settings'
Starting Channels development server at http://0.0.0.0:8000/
Channel layer default (asgiref.inmemory.ChannelLayer)
Quit the server with CONTROL-C.
2018-02-16 23:19:32,935 - INFO - worker - Listening on channels http.request, websocket.connect, websocket.disconnect, websocket.receive
2018-02-16 23:19:32,935 - INFO - worker - Listening on channels http.request, websocket.connect, websocket.disconnect, websocket.receive
2018-02-16 23:19:32,935 - INFO - worker - Listening on channels http.request, websocket.connect, websocket.disconnect, websocket.receive
2018-02-16 23:19:32,936 - INFO - worker - Listening on channels http.request, websocket.connect, websocket.disconnect, websocket.receive
Traceback (most recent call last):
  File "/root/.virtualenvs/codebench/local/lib/python2.7/site-packages/eventlet/hubs/hub.py", line 457, in fire_timers
    timer()
  File "/root/.virtualenvs/codebench/local/lib/python2.7/site-packages/eventlet/hubs/timer.py", line 58, in __call__
    cb(*args, **kw)
  File "/root/.virtualenvs/codebench/local/lib/python2.7/site-packages/eventlet/semaphore.py", line 145, in _do_acquire
    waiter.switch()
  File "/root/.virtualenvs/codebench/local/lib/python2.7/site-packages/eventlet/green/thread.py", line 41, in __thread_body
    func(*args, **kwargs)
  File "/root/.virtualenvs/codebench/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/root/.virtualenvs/codebench/local/lib/python2.7/site-packages/channels/management/commands/runserver.py", line 99, in inner_run
    websocket_handshake_timeout=self.websocket_handshake_timeout,
  File "/root/.virtualenvs/codebench/local/lib/python2.7/site-packages/daphne/server.py", line 101, in run
    websocket_handshake_timeout=self.websocket_handshake_timeout
TypeError: __init__() got an unexpected keyword argument 'proxy_forwarded_proto_header'

如果我升级所有东西,除了 Python 2.7.12:

asgi-redis==1.4.3
asgiref==2.1.5
channels==2.0.2
daphne==2.0.3
Django==1.11.10

runserver 会给出:

  File "/root/.virtualenvs/codebench/local/lib/python2.7/site-packages/channels/apps.py", line 6, in <module>
    import daphne.server  # noqa
  File "/root/.virtualenvs/codebench/local/lib/python2.7/site-packages/daphne/server.py", line 192
    async def handle_reply(self, protocol, message):
            ^
SyntaxError: invalid syntax

如果我尝试在 Python 3.5.2 下重新安装所有必需的软件包:

twisted 需要这个:

apt install python3.6-dev

https://askubuntu.com/questions/966939/failed-building-wheel-for-twisted

现在我收到随机错误:

2018-02-18 14:03:51,634 - ERROR - server - Exception inside application: 'NoneType' object has no attribute 'writeSequence'
  File "/usr/lib/python3.5/asyncio/tasks.py", line 241, in _step
    result = coro.throw(exc)
  File "/root/.virtualenvs/python3/lib/python3.5/site-packages/channels/http.py", line 191, in __call__
    await self.handle(body)
  File "/root/.virtualenvs/python3/lib/python3.5/site-packages/asgiref/sync.py", line 108, in __call__
    return await asyncio.wait_for(future, timeout=None)
  File "/usr/lib/python3.5/asyncio/tasks.py", line 373, in wait_for
    return (yield from fut)
  File "/usr/lib/python3.5/asyncio/futures.py", line 361, in __iter__
    yield self  # This tells Task to wait for completion.
  File "/usr/lib/python3.5/asyncio/tasks.py", line 296, in _wakeup
    future.result()
  File "/usr/lib/python3.5/asyncio/futures.py", line 274, in result
    raise self._exception
  File "/usr/lib/python3.5/concurrent/futures/thread.py", line 55, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/root/.virtualenvs/python3/lib/python3.5/site-packages/asgiref/sync.py", line 123, in thread_handler
    return self.func(*args, **kwargs)
  File "/root/.virtualenvs/python3/lib/python3.5/site-packages/channels/http.py", line 227, in handle
    self.send(response_message)
  File "/root/.virtualenvs/python3/lib/python3.5/site-packages/asgiref/sync.py", line 62, in __call__
    return call_result.result()
  File "/usr/lib/python3.5/concurrent/futures/_base.py", line 405, in result
    return self.__get_result()
  File "/usr/lib/python3.5/concurrent/futures/_base.py", line 357, in __get_result
    raise self._exception
  File "/root/.virtualenvs/python3/lib/python3.5/site-packages/asgiref/sync.py", line 76, in main_wrap
    result = await self.awaitable(*args, **kwargs)
  File "/root/.virtualenvs/python3/lib/python3.5/site-packages/daphne/server.py", line 196, in handle_reply
    protocol.handle_reply(message)
  File "/root/.virtualenvs/python3/lib/python3.5/site-packages/daphne/http_protocol.py", line 210, in handle_reply
    http.Request.write(self, message.get("body", b""))
  File "/root/.virtualenvs/python3/lib/python3.5/site-packages/twisted/web/http.py", line 1084, in write
    self.channel.writeSequence(toChunk(data))
  'NoneType' object has no attribute 'writeSequence'

还有:

(python3) root@computer:path# python manage.py shell
Python 3.5.2 (default, Nov 23 2017, 16:37:01) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import channels
>>> print(channels.__file__)
/root/.virtualenvs/python3/lib/python3.5/site-packages/channels/__init__.py
>>> from channels import Group
Traceback (most recent call last):
  File "<console>", line 1, in <module>
ImportError: cannot import name 'Group'

【问题讨论】:

那个教程已经有几年的历史了。你不能指望它再工作了。请注意,频道 2 刚刚发布,并且有 some significant changes。您最好从第 2 频道开始,这样您以后就不必进行更改。 该教程适用于1.8频道,您可以发布您的应用程序的routing.py代码吗? 【参考方案1】:

看起来好像你创建了一个 Python 2 virtualenv。设置alias python=python3 将不起作用。您需要使用 Python 3 创建一个 virtualenv,例如:

python3 -m venv newenv

然后在新的 virtualenv 中重新安装您的要求。

激活新环境后,python 将变为python3。您无需设置alias python=python3

【讨论】:

【参考方案2】:

我最终得到以下版本:

asgi-redis==0.8.3
asgiref==0.9
autobahn==0.12.1
channels==0.9.3
daphne==0.9.1
dj-database-url==0.4.0
Django==1.9.3
haikunator==1.0.1
msgpack-python==0.4.7
psycopg2==2.7.4
redis==2.10.5
six==1.10.0
Twisted==15.5.0
txaio==2.2.1
zope.interface==4.1.3

原来 Python 版本无关紧要,可与 2.7 和 3.5 一起使用。 因为我安装的是https://github.com/jacobian/channels-example, 我还将数据库从 mysql 更改为 PostgreSQL。这个例子就像一个魅力。

【讨论】:

以上是关于安装频道:Python Django 版本和设置的主要内容,如果未能解决你的问题,请参考以下文章

Django 频道 2 和 EC2 / Elasticbeanstalk

使用没有视图的 Django 频道

windows下安装jandgo

Windows错误10061 - Django频道

Django频道使用redis设置在heroku泄漏内存

django 频道 channels.exceptions.Channels Full