Zappa icon indicating copy to clipboard operation
Zappa copied to clipboard

Tensorflow and Zappa: No module named 'tensorflow' ModuleNotFoundError

Open azagovora opened this issue 9 years ago • 11 comments

I am exploring the possibility of migration our prediction services to Lambda. I am configuring a simple Flask application which depends on tensorflow to run with Zappa and Python 3.6. Unfortunately, I got the error below. The application works with Python 2.7 without any errors.

Actual Behavior

Call fails with internal server errors

Steps to Reproduce

app.py

import sys

from flask import Flask
import tensorflow as tf

app = Flask(__name__)
app.debug = True

print(sys.maxunicode)

@app.route('/')
def index():
    x = tf.ones(5) * 2
    with tf.Session():
        y = x.eval()
    return "Hello, world!" + str(y), 200

# We only need this for local development.
if __name__ == '__main__':
    app.run()

zappa_settings.json

{
    "dev": {
        "app_function": "app.app",
        "profile_name": "default",
        "s3_bucket": "zappa-tensorflow",
        "runtime": "python3.6",
        "slim_handler": true,
        "debug" : true
    }
}

Create the 2 files listed above 1 Set up virtualenv 2 zappa deploy dev

After the application deploys it will respond to all requests with internal sever errors for any requests

Taking a look at what is going on zappa tail dev

1495821395423] Instancing..
[1495821395424] Failed to find library...right filename?
[1495821396121] No module named 'tensorflow': ModuleNotFoundError
Traceback (most recent call last):
  File "/var/task/handler.py", line 484, in lambda_handler
  return LambdaHandler.lambda_handler(event, context)
  File "/var/task/handler.py", line 240, in lambda_handler
  handler = cls()
  File "/var/task/handler.py", line 129, 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 678, in exec_module
  File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
  File "/tmp/lambda-tensorfl/app.py", line 7, in <module>
  import tensorflow as tf
ModuleNotFoundError: No module named 'tensorflow'
[1495821396121] Traceback (most recent call last):
[1495821396121] File "/var/runtime/awslambda/bootstrap.py", line 226, in handle_event_request
[1495821396121] result = request_handler(json_input, context)
[1495821396121] File "/var/task/handler.py", line 484, in lambda_handler
[1495821396121] return LambdaHandler.lambda_handler(event, context)
[1495821396121] File "/var/task/handler.py", line 240, in lambda_handler
[1495821396121] handler = cls()
[1495821396121] File "/var/task/handler.py", line 129, in __init__
[1495821396122] self.app_module = importlib.import_module(self.settings.APP_MODULE)
[1495821396122] File "/var/lang/lib/python3.6/importlib/__init__.py", line 126, in import_module
[1495821396122] return _bootstrap._gcd_import(name[level:], package, level)
[1495821396122] File "<frozen importlib._bootstrap>", line 978, in _gcd_import
[1495821396122] File "<frozen importlib._bootstrap>", line 961, in _find_and_load
[1495821396122] File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
[1495821396122] File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
[1495821396122] File "<frozen importlib._bootstrap_external>", line 678, in exec_module
[1495821396122] File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
[1495821396122] File "/tmp/lambda-tensorfl/app.py", line 7, in <module>
[1495821396122] import tensorflow as tf
[1495821396122] ModuleNotFoundError: No module named 'tensorflow'
[1495821396122] During handling of the above exception, another exception occurred:
[1495821396122] Traceback (most recent call last):
[1495821396122] File "/var/runtime/awslambda/bootstrap.py", line 463, in <module>
[1495821396122] main()
[1495821396122] File "/var/runtime/awslambda/bootstrap.py", line 459, in main
[1495821396123] handle_event_request(request_handler, invokeid, event_body, context_objs, invoked_function_arn)
[1495821396123] File "/var/runtime/awslambda/bootstrap.py", line 240, in handle_event_request
[1495821396123] result = to_json(result)
[1495821396123] File "/var/runtime/awslambda/bootstrap.py", line 215, in to_json
[1495821396123] return json.dumps(obj, default=decimal_serializer)
[1495821396123] File "/var/lang/lib/python3.6/json/__init__.py", line 238, in dumps
[1495821396123] **kw).encode(obj)
[1495821396123] File "/var/lang/lib/python3.6/json/encoder.py", line 199, in encode
[1495821396123] chunks = self.iterencode(o, _one_shot=True)
[1495821396123] File "/var/lang/lib/python3.6/json/encoder.py", line 257, in iterencode
[1495821396123] return _iterencode(o, 0)
[1495821396123] File "/var/runtime/awslambda/bootstrap.py", line 104, in decimal_serializer
[1495821396123] raise TypeError(repr(o) + " is not JSON serializable")
[1495821396123] TypeError: <FrameSummary file /var/task/handler.py, line 484 in lambda_handler> is not JSON serializable
[1495821396253] No module named 'tensorflow'
ModuleNotFoundError

Your Environment

appdirs==1.4.3 argcomplete==1.8.2 base58==0.2.4 boto3==1.4.4 botocore==1.5.40 click==6.7 docutils==0.13.1 durationpy==0.4 Flask==0.12.2 future==0.16.0 futures==3.1.1 hjson==2.0.2 itsdangerous==0.24 Jinja2==2.9.6 jmespath==0.9.2 kappa==0.6.0 lambda-packages==0.15.0 MarkupSafe==1.0 numpy==1.12.1 packaging==16.8 placebo==0.8.1 protobuf==3.3.0 pyparsing==2.2.0 python-dateutil==2.6.0 python-slugify==1.2.4 PyYAML==3.12 requests==2.14.2 s3transfer==0.1.10 six==1.10.0 tensorflow==1.1.0 toml==0.9.2 tqdm==4.11.2 troposphere==1.9.3 Unidecode==0.4.20 Werkzeug==0.12.2 wsgi-request-logger==0.4.6 zappa==0.42.0

  • Zappa version used: 42.0
  • Operating System and Python version: macOS Sierra 10.12.5, Python 3.6.1

azagovora avatar May 26 '17 19:05 azagovora

I get tensorflow to work with "use_precompiled_packages": false, have you tried that?

jwkvam avatar May 26 '17 20:05 jwkvam

Yes, I have tried that but I got the same error.

azagovora avatar May 26 '17 20:05 azagovora

Sorry I mean to put false, but I had a copy paste error.

jwkvam avatar May 26 '17 20:05 jwkvam

Now, numpy raises the exception in additional to tensorflow import error.

ImportError
[1495859729685] Instancing..
[1495859730866] Importing the multiarray numpy extension module failed.  Most
likely you are trying to import a failed build of numpy.
If you're working with a numpy git repo, try `git clean -xdf` (removes all
files not under version control).  Otherwise reinstall numpy.
: ImportError
Traceback (most recent call last):
  File "/var/task/handler.py", line 484, in lambda_handler
  return LambdaHandler.lambda_handler(event, context)
  File "/var/task/handler.py", line 240, in lambda_handler
  handler = cls()
  File "/var/task/handler.py", line 129, 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 678, in exec_module
  File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
  File "/var/task/app.py", line 7, in <module>
  import tensorflow as tf
  File "/var/task/tensorflow/__init__.py", line 24, in <module>
  from tensorflow.python import *
  File "/var/task/tensorflow/python/__init__.py", line 49, in <module>
  import numpy as np
  File "/var/task/numpy/__init__.py", line 142, in <module>
  from . import add_newdocs
  File "/var/task/numpy/add_newdocs.py", line 13, in <module>
  from numpy.lib import add_newdoc
  File "/var/task/numpy/lib/__init__.py", line 8, in <module>
  from .type_check import *
  File "/var/task/numpy/lib/type_check.py", line 11, in <module>
  import numpy.core.numeric as _nx
  File "/var/task/numpy/core/__init__.py", line 24, in <module>
  raise ImportError(msg)
ImportError: 
Importing the multiarray numpy extension module failed.  Most
likely you are trying to import a failed build of numpy.
If you're working with a numpy git repo, try `git clean -xdf` (removes all
files not under version control).  Otherwise reinstall numpy.
[1495859730885] Traceback (most recent call last):
[1495859730885] File "/var/task/numpy/core/__init__.py", line 16, in <module>
[1495859730885] from . import multiarray
[1495859730885] ImportError: cannot import name 'multiarray'
[1495859730885] During handling of the above exception, another exception occurred:
[1495859730885] Traceback (most recent call last):
[1495859730885] File "/var/runtime/awslambda/bootstrap.py", line 226, in handle_event_request
[1495859730885] result = request_handler(json_input, context)
[1495859730885] File "/var/task/handler.py", line 484, in lambda_handler
[1495859730885] return LambdaHandler.lambda_handler(event, context)
[1495859730885] File "/var/task/handler.py", line 240, in lambda_handler
[1495859730885] handler = cls()
[1495859730885] File "/var/task/handler.py", line 129, in __init__
[1495859730885] self.app_module = importlib.import_module(self.settings.APP_MODULE)
[1495859730885] File "/var/lang/lib/python3.6/importlib/__init__.py", line 126, in import_module
[1495859730885] return _bootstrap._gcd_import(name[level:], package, level)
[1495859730885] File "<frozen importlib._bootstrap>", line 978, in _gcd_import
[1495859730885] File "<frozen importlib._bootstrap>", line 961, in _find_and_load
[1495859730885] File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
[1495859730885] File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
[1495859730885] File "<frozen importlib._bootstrap_external>", line 678, in exec_module
[1495859730885] File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
[1495859730885] File "/var/task/app.py", line 7, in <module>
[1495859730885] import tensorflow as tf
[1495859730885] File "/var/task/tensorflow/__init__.py", line 24, in <module>
[1495859730885] from tensorflow.python import *
[1495859730885] File "/var/task/tensorflow/python/__init__.py", line 49, in <module>
[1495859730885] import numpy as np
[1495859730885] File "/var/task/numpy/__init__.py", line 142, in <module>
[1495859730885] from . import add_newdocs
[1495859730885] File "/var/task/numpy/add_newdocs.py", line 13, in <module>
[1495859730885] from numpy.lib import add_newdoc
[1495859730885] File "/var/task/numpy/lib/__init__.py", line 8, in <module>
[1495859730885] from .type_check import *
[1495859730885] File "/var/task/numpy/lib/type_check.py", line 11, in <module>
[1495859730885] import numpy.core.numeric as _nx
[1495859730885] File "/var/task/numpy/core/__init__.py", line 24, in <module>
[1495859730885] raise ImportError(msg)
[1495859730885] ImportError:
[1495859730885] Importing the multiarray numpy extension module failed.  Most
[1495859730885] likely you are trying to import a failed build of numpy.
[1495859730885] If you're working with a numpy git repo, try `git clean -xdf` (removes all
[1495859730885] files not under version control).  Otherwise reinstall numpy.
[1495859730885] During handling of the above exception, another exception occurred:
[1495859730885] Traceback (most recent call last):
[1495859730885] File "/var/runtime/awslambda/bootstrap.py", line 463, in <module>
[1495859730885] main()
[1495859730885] File "/var/runtime/awslambda/bootstrap.py", line 459, in main
[1495859730885] handle_event_request(request_handler, invokeid, event_body, context_objs, invoked_function_arn)
[1495859730885] File "/var/runtime/awslambda/bootstrap.py", line 240, in handle_event_request
[1495859730885] result = to_json(result)
[1495859730885] File "/var/runtime/awslambda/bootstrap.py", line 215, in to_json
[1495859730885] return json.dumps(obj, default=decimal_serializer)
[1495859730885] File "/var/lang/lib/python3.6/json/__init__.py", line 238, in dumps
[1495859730885] **kw).encode(obj)
[1495859730885] File "/var/lang/lib/python3.6/json/encoder.py", line 199, in encode
[1495859730885] chunks = self.iterencode(o, _one_shot=True)
[1495859730885] File "/var/lang/lib/python3.6/json/encoder.py", line 257, in iterencode
[1495859730885] return _iterencode(o, 0)
[1495859730885] File "/var/runtime/awslambda/bootstrap.py", line 104, in decimal_serializer
[1495859730885] raise TypeError(repr(o) + " is not JSON serializable")
[1495859730885] TypeError: <FrameSummary file /var/task/handler.py, line 484 in lambda_handler> is not JSON serializable
[1495859731054] Importing the multiarray numpy extension module failed.  Most
likely you are trying to import a failed build of numpy.
If you're working with a numpy git repo, try `git clean -xdf` (removes all
files not under version control).  Otherwise reinstall numpy.

azagovora avatar May 27 '17 04:05 azagovora

I'm getting the same error, but without tensorflow, and just "import numpy as np" in my python source code

man-james avatar Jun 06 '17 10:06 man-james

Numpy works fine with option "use_precompiled_packages": true.

azagovora avatar Jun 09 '17 08:06 azagovora

use_precompiled_packages pulls from https://github.com/Miserlou/lambda-packages, so only these packages will be supported.

It looks like there's an issue (https://github.com/Miserlou/lambda-packages/issues/31) underway to add the tensorflow package to lambda-packages.

bcongdon avatar Jun 17 '17 17:06 bcongdon

@azagovora you might be experiencing this https://github.com/Miserlou/Zappa/issues/783

jwkvam avatar Jun 17 '17 19:06 jwkvam

Same here.. I tried "use_precompiled_packages": false but I end up with a failure requesting some *.so files.

grafael avatar Jan 23 '18 01:01 grafael

For me following this thread from the Zappa slack, I have been able to resolve this issue using "use_precomipled_packages" : false.

Hello, before I go out and create a minimal reproducible version for GH issues: has anyone come across a situation (using pipenv) that they install a package (in this case tensorflow==1.6.0) using slim_handler. Zappa packages it and uploads but then the Lambda can't find it?

I am on Ubuntu, which I assume the Lambda container runs on as well? AFAIK, TF does have binary dependencies but even if they are mismatched it usually figures out a way. (Sorry was trying to Google for a better answer but I could not find anything more concrete), so I just went with what I've observed from experience E.g. I've come across 2018-09-20 14:37:04.036629: I tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA (edited) but the output is correct

Error:

Traceback (most recent call last):
File "/var/task/handler.py", line 567, in lambda_handler
return LambdaHandler.lambda_handler(event, context)
File "/var/task/handler.py", line 237, in lambda_handler
handler = cls()
File "/var/task/handler.py", line 129, 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 678, in exec_module
File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
File "/tmp/lambdas/predict.py", line 6, in <module>
import tensorflow as tf
ModuleNotFoundError: No module named 'tensorflow'```
config:
```{
    "dev": {
        "app_function": "test.app",
        "aws_region": "eu-west-1",
        "profile_name": "sandbox",
        "project_name": "tf-lambda",
        "runtime": "python3.6",
        "s3_bucket": "zappa-5w2m3g9a6",
        "slim_handler" : true
    }
}```

jakubLangr avatar Sep 20 '18 15:09 jakubLangr

Hello, I have come across this error as well. Here is the output:

[ERROR] ModuleNotFoundError: No module named 'tensorflow'
Traceback (most recent call last):
  File "/var/task/handler.py", line 609, in lambda_handler
    return LambdaHandler.lambda_handler(event, context)
  File "/var/task/handler.py", line 240, in lambda_handler
    handler = cls()
  File "/var/task/handler.py", line 134, in __init__
    self.app_module = importlib.import_module(self.settings.APP_MODULE)
  File "/var/lang/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/var/task/app.py", line 8, in <module>
    from main import predict
  File "/var/task/main.py", line 1, in <module>
    from tensorflow.keras.models import load_model

I noticed the majority of the posts surrounding tensorflow and zappa are a few years old. I tried the "use_precompiled_packages": false solution and it did not work. Has anybody found a fix for this? I may move onto another service if this can't be resolved using zappa. I'm using python 3.7, Flask 1.1.2 and tensorflow 2.2.0

farbodg avatar Jun 06 '20 23:06 farbodg