Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bad magic number in 'application': b'\x03\xf3\r\n': ImportError #854

Closed
texonidas opened this issue May 15, 2017 · 19 comments
Closed

bad magic number in 'application': b'\x03\xf3\r\n': ImportError #854

texonidas opened this issue May 15, 2017 · 19 comments

Comments

@texonidas
Copy link
Contributor

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"] } }
@texonidas
Copy link
Contributor Author

I'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 :)

@jakul
Copy link
Contributor

jakul commented Jul 7, 2017

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.

@ansarada-developers
Copy link

thanks, @jakul this really helped me out!

@jordanambra
Copy link

This can happen if you move a directory into another directory without deleting the .pyc cache.

@amiashraf
Copy link

find . -name \*.pyc -delete
Absolutely solved my problem.
I have seen the error when I pull my friends code and trying to migrate.

@jojo5716
Copy link

@amiashraf it was my solution too! i has same error when importing celery library on Django 2.0.2

@nosarthur
Copy link

nosarthur commented Feb 16, 2018

XXX EDIT FROM MAINTAINTER: THIS IS BAD ADVICE DONT DO THIS XXX

Or git clean -fdx if it's a git repo

@einSelbst
Copy link

einSelbst commented Apr 4, 2018

uh, git clean -fdx did remove more than the pycache. Would have been nice to mention this @nosarthur https://stackoverflow.com/questions/6267180/can-i-undo-a-git-clean-fdx

Running git clean -fdxnfirst to do a dry-run will be a good idea.

@ThadeuLuz
Copy link

ThadeuLuz commented May 27, 2018

@nosarthur your comment made me lose work..
but it did fix the problem

@Kipngetich33
Copy link

Kipngetich33 commented Jun 27, 2018

the issue is caused by differences conflicts in python versions within the cache. The cache is stored under file name as ' pycach' try finding these file and deleting them then trying running the project with the correct version of python again.

@ysYvonne
Copy link

thanks!!!!!!!, @jakul you save my life!!!!

@cmdedj
Copy link

cmdedj commented May 7, 2019

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.

thank you, its solve my question

@asa-odoo
Copy link

asa-odoo commented May 22, 2019

Thanks @jakul . It works !!! ;)

@prantzos
Copy link

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.

Thanks a lot @jakul !!

@hamley241
Copy link

Thanks @jakul and @texonidas , you guys a re lifesavers!

@Panant4
Copy link

Panant4 commented Sep 30, 2019

+1 Thank you! Worked for me!

@rlele5
Copy link

rlele5 commented Feb 24, 2020

Which directory should I run find . -name \*.pyc -delete in? I've run it in my application code directory, but that didn't fix the issue.

@EyasuLingerih
Copy link

I run into this problem on windows machine while I was trying to create a new project in PyCharm.
I was able to fix it by removing a software for finger print scanner called Bio 7.

@habimanaflorbert
Copy link

habimanaflorbert commented Mar 22, 2023

Move to your home directory and run in your terminal this
$sudo find . -name "*.pyc" -exc rm -f {} ;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests