Zappa: bad magic number in 'application': b'\x03\xf3\r\n': ImportError
I have just made the switch from 2.7 to 3.6, however upon undeploying and redeploying my application, I received the above error, with the stack trace below.
Context
bad magic number in 'application': b'\x03\xf3\r\n': ImportError Traceback (most recent call last): File "/var/task/handler.py", line 514, in lambda_handler return LambdaHandler.lambda_handler(event, context) File "/var/task/handler.py", line 259, in lambda_handler handler = cls() File "/var/task/handler.py", line 148, in __init__ self.app_module = importlib.import_module(self.settings.APP_MODULE) File "/var/lang/lib/python3.6/importlib/__init__.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 978, in _gcd_import File "<frozen importlib._bootstrap>", line 961, in _find_and_load File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 655, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 674, in exec_module File "<frozen importlib._bootstrap_external>", line 888, in get_code File "<frozen importlib._bootstrap_external>", line 455, in _validate_bytecode_header ImportError: bad magic number in 'application': b'\x03\xf3\r\n'
From my research, this error occurs when old .pyc files are kept between version changes of Python. Given that I fully undeployed then redeployed this project, I’m unsure how else I can go about clearing them.
Expected Behavior
Normal deployment
Actual Behavior
Hella crashes
Possible Fix
As mentioned above, there are a few suggestions online that this is caused by Python 2.x .pyc files making their way into the new Python 3.x project.
Steps to Reproduce
Unfortunately this is an error I am unable to reproduce from a fresh install, as it is a long running closed-source project.
Your Environment
- Zappa version used: 0.41.2
- Operating System and Python version: Ubuntu 16.04, Python 3.6.1
- Your
zappa_settings.py:"develop": { "app_function": "application.app", "delete_s3_zip": false, "domain": "api.develop.website.com.au", "http_methods": ["GET", "OPTIONS", "POST"], "keep_warm": false, "lets_encrypt_expression": "rate(15 days)", "lets_encrypt_key": "s3://website-config/develop.key", "method_header_types": [ "Content-Type", "Access-Control-Allow-Origin", "Access-Control-Allow-Headers", "Access-Control-Allow-Methods", "Location", "Status", "X-Frame-Options", "Set-Cookie" ], "remote_env": "s3://website-config/dev-config.json", "s3_bucket": "website-api-develop", "timeout_seconds": 70, "vpc_config": { "SubnetIds": ["secret"], "SecurityGroupIds": ["also secret"] } }
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 19 (2 by maintainers)
I was able to fix this too, by doing
find . -name \*.pyc -delete. Mine definitely wasn’t caused by PY2 -> PY3 upgrade, but seemed to happen spontaneously overnight.find . -name \*.pyc -deleteAbsolutely solved my problem. I have seen the error when I pull my friends code and trying to migrate.XXX EDIT FROM MAINTAINTER: THIS IS BAD ADVICE DONT DO THIS XXX
Or
git clean -fdxif it’s a git repoI’ve fixed this by completely deleting the repository and then re-cloning.
I’m assuming that zappa uses some .pyc file from the project itself, not sure if it is worth adding some recompilation functionality, the 2.7 -> 3.6 use case probably doesn’t occur frequently enough to warrant it.
Hopefully this helps someone else in the same boat though 😃
This can happen if you move a directory into another directory without deleting the .pyc cache.
Thanks @jakul and @texonidas , you guys a re lifesavers!