Search code examples
pythondjangopyinstallerdjango-channels

Pyinstaller Django Channels issue


I'm trying to pack a django app into an .exe, after a multiple tries (hooks etc...) Everything works fine, except django-channels. When I run mysite.exe runserver, the app starts but the routing are not found and channels are not working.

Here is my .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')

FYI : I had an issue with daphne saying that another application is using the server, I solved it by modifiying the pyi_rth_twisted.py file into :

from twisted.internet import asyncioreactor

This creates module: sys.modules['twisted.internet.reactor'] asyncioreactor.install()

when I run the server , I got thit :

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

Please any help ? thanks


Solution

  • Please, check your build. If you will see that the Django launches the "development server" instead of "ASGI/Channels", it means that the PyInstaller runs the server using the "runserver.py" of "django.core", but you need launch the "runserver.py" of the "channels".

    For this you need in the file PyInstaller/hooks/rthooks/pyi_rth_django.py change this string:
    'runserver': 'django.core',
    with this:
    'runserver': 'channels',