[21.6.x on pyinstaller builds crash] ModuleNotFoundError: No module named 'websockets.legacy.protocol'
See original GitHub issueDescribe the bug
We build our server into a single macOS executable with pyinstaller.
sanic works perfectly up until version 21.3.4
After 21.6.0
(including both 21.6.1
, 21.6.2
)
When the app is run and it’s going through the sanic
imports we get the following crash and stacktrace:
Traceback (most recent call last):
File "telluride.py", line 28, in <module>
import server
File "PyInstaller/loader/pyimod03_importers.py", line 546, in exec_module
File "server.py", line 23, in <module>
from sanic import Sanic
File "PyInstaller/loader/pyimod03_importers.py", line 546, in exec_module
File "sanic/__init__.py", line 2, in <module>
File "PyInstaller/loader/pyimod03_importers.py", line 546, in exec_module
File "sanic/app.py", line 43, in <module>
File "PyInstaller/loader/pyimod03_importers.py", line 546, in exec_module
File "sanic/asgi.py", line 11, in <module>
File "PyInstaller/loader/pyimod03_importers.py", line 546, in exec_module
File "sanic/models/asgi.py", line 6, in <module>
File "PyInstaller/loader/pyimod03_importers.py", line 546, in exec_module
File "sanic/websocket.py", line 13, in <module>
File "websockets/imports.py", line 81, in __getattr__
File "websockets/imports.py", line 26, in import_name
ModuleNotFoundError: No module named 'websockets.legacy.protocol'
[32682] Failed to execute script 'telluride' due to unhandled exception!
Code snippet Our tiny http server code here
Expected behavior
Not crash, the same way it works with all previous versions of sanic
up to 21.3.4
Environment (please complete the following information):
- OS: macOS (so far, still need to test in windows and linux, will follow up on this thread in a few minutes)
- Version
21.6.0
,21.6.1
,21.6.2
- The error comes up with both python
3.7
and3.9.6
Issue Analytics
- State:
- Created 2 years ago
- Comments:9 (4 by maintainers)
Top Results From Across the Web
No module named 'websocket' - python - Stack Overflow
First, you need to install pip if you don't have it. Type pip in your terminal or cmd to see if it is...
Read more >Modulenotfounderror: No Module Named 'Bybitwebsocket' - ADocLib
[21.6.x on pyinstaller builds crash] ModuleNotFoundError: No module named 21.6.1 21.6.2; The error comes up with both python 3.7 and 3.9.6.
Read more >pyinstaller modulenotfounderror - You.com | The search engine you ...
Pyinstaller ; ModuleNotFoundError: No module named 'sklearn.utils._cython_blas' ... I deleted the old dist, build and pycache folders in Finder.
Read more >sanic [21.6.x on pyinstaller builds crash] ModuleNotFoundError: No ...
sanic [21.6.x on pyinstaller builds crash] ModuleNotFoundError: No module named 'websockets.legacy.protocol' Python. Describe the bug We build our server ...
Read more >conda-forge - :: Anaconda.org
acme, 2.1.0, Apache-2.0, X, ACME protocol implementation in Python ... X, Extends click.Group to invoke a command without explicit subcommand name.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I am sorry you have been struggling. You never messaged me about this so I would have assumed it was resolved. I wish you would have reached out sooner and I could have helped you through this rather than struggling with frustration. Your dependency issue seems to be resolved, and I think this new error is potentially easy to overcome.
You are running Sanic in a capacity that is not standard. PyInstaller attempts to do a lot of code stripping. Sanic itself tries to do this at startup to optimize your performance. So, what is happening is that PyInstaller is removing parts of Sanic. When the Sanic server starts, it throws an
OSError
because the code that it thinks should be available does not exist.I just went through and put the following snippet into a fresh venv with Sanic and PyInstaller.
Then, I created a simple script that approximates the code you shared:
I was able to recreate your issue:
A quick look at the PyInstaller docs showed me this:
A quick rebuild:
Sure enough, when I run the executable now, it runs as expected and I can ping the endpoint:
I hope this helps. If there are more people looking to make a PyInstaller executable with Sanic perhaps we can add it to the
How to...
section of the documentation.Wow, thank you Adam for taking the time to look at this in detail, wish I’d found that
--collect-all
flag for PyInstaller earlier!Hopefully this will help someone that might have a similar problem in the future.