首页 > 解决方案 > Pyinstaller Django 频道问题

问题描述

我正在尝试将 django 应用程序打包到 .exe 中,经过多次尝试(钩子等...)一切正常,除了 django-channels。当我运行 mysite.exe runserver 时,应用程序启动,但找不到路由并且通道不起作用。

这是我的 .spec

-- mode: python ; coding: utf-8 --
block_cipher = None

a = Analysis(['mysite\manage.py'],
pathex=['C:\Users\SFadili\Documents\_Never Backup\Django\help2'],
binaries=[],
datas=[
('mysite/templates', 'templates'),
('mysite/static', 'static'),
('C:\Users\SFadili\Documents\_Never Backup\Django\help2\my_env_0\Lib\site-packages\plotly\', 'plotly'),
('C:\Users\SFadili\Documents\_Never Backup\Django\help2\my_env_0\Lib\site-packages\daphne\', 'daphne'),
('C:\Users\SFadili\Documents\_Never Backup\Django\help2\my_env_0\Lib\site-packages\daphne-2.5.0.dist-info\', 'daphne-2.5.0.dist-info'),
('C:\Users\SFadili\Documents\_Never Backup\Django\help2\my_env_0\Lib\site-packages\raven\', 'raven'),
('C:\Users\SFadili\Documents\_Never Backup\Django\help2\my_env_0\Lib\site-packages\raven-6.10.0.dist-info\', 'raven-6.10.0.dist-info'),
],
hiddenimports=[
'mysite.routing',
'mysite.urls',
'myapp.apps',
'myapp.urls',
'myapp.routing',
'myapp.consumers',
'channels.apps',
'channels_redis',
],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
[],
name='mysite',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
upx_exclude=[],
runtime_tmpdir=None,
console=True )
coll = COLLECT(exe,
a.binaries,
a.zipfiles,
a.datas,
strip=False,
upx=True,
upx_exclude=[],
name='manage')

仅供参考:我遇到了 daphne 的问题,说另一个应用程序正在使用服务器,我通过将 pyi_rth_twisted.py 文件修改为:

从 twisted.internet 导入异步反应器

这将创建模块: sys.modules['twisted.internet.reactor'] asyncioreactor.install()

当我运行服务器时,我明白了:

System check identified no issues (0 silenced).
September 14, 2020 - 10:13:24
Django version 2.2.16, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
[14/Sep/2020 10:13:36] "GET /login/?next=/myapp/repository/solvency_ii/home/ HTTP/1.1" 200 6979
[14/Sep/2020 10:13:36] "GET /static/css/swiper.min.css HTTP/1.1" 200 13676
[14/Sep/2020 10:13:36] "GET /static/css/bootstrap.min.css HTTP/1.1" 200 155764
[14/Sep/2020 10:13:36] "GET /static/css/mdb.min.css HTTP/1.1" 200 555314
[14/Sep/2020 10:13:36] "GET /static/fonts/css/all.min.css HTTP/1.1" 200 58582
[14/Sep/2020 10:13:36] "GET /static/Loader/imgcustom-fallback.css HTTP/1.1" 200 631
[14/Sep/2020 10:13:36] "GET /static/Loader/imgcustom-loader.css HTTP/1.1" 200 626
[14/Sep/2020 10:13:36] "GET /static/css/styles.css HTTP/1.1" 200 23805
[14/Sep/2020 10:13:36] "GET /static/Loader/loader1.css HTTP/1.1" 200 560
[14/Sep/2020 10:13:36] "GET /static/js/script.js HTTP/1.1" 200 1214
[14/Sep/2020 10:13:36] "GET /static/js/jquery-3.4.1.min.js HTTP/1.1" 200 88147
[14/Sep/2020 10:13:37] "GET /static/js/bootstrap.min.js HTTP/1.1" 200 58037
[14/Sep/2020 10:13:37] "GET /static/js/popper.min.js HTTP/1.1" 200 20542
[14/Sep/2020 10:13:37] "GET /static/js/reconnecting-websocket.min.js HTTP/1.1" 200 3099
[14/Sep/2020 10:13:37] "GET /static/js/comments_websocket.js HTTP/1.1" 200 7681
[14/Sep/2020 10:13:37] "GET /static/img/ey-logo.png HTTP/1.1" 200 24420
[14/Sep/2020 10:13:37] "GET /static/js/mdb.min.js HTTP/1.1" 200 409155
[14/Sep/2020 10:13:37] "GET /static/fonts/webfonts/fa-solid-900.woff2 HTTP/1.1" 200 79444
Not Found: /ws/comments/
[14/Sep/2020 10:13:38] "GET /ws/comments/ HTTP/1.1" 404 2736

请问有什么帮助吗?谢谢

标签: pythondjangopyinstallerdjango-channels

解决方案


请检查您的构建。如果您会看到 Django 启动的是“开发服务器”而不是“ASGI/Channels”,这意味着 PyInstaller 使用“django.core”的“runserver.py”运行服务器,但您需要启动“runserver” .py”的“频道”。

为此,您需要在文件中PyInstaller/hooks/rthooks/pyi_rth_django.py 更改此字符串
'runserver': 'django.core',

'runserver': 'channels',


推荐阅读