'I install tensorflow using pip but tensorflow import does not work

I have installed tensorflow like so: pip3.8 install tensorflow (I am using pip3.8 command as I have several python versions installed on my EC2 instance). When I do so I get this output:

Defaulting to user installation because normal site-packages is not writeable
Collecting tensorflow
  Downloading tensorflow-2.8.0-cp38-cp38-manylinux2010_x86_64.whl (497.6 MB)
     |████████████████████████████████| 497.6 MB 37.1 MB/s eta 0:00:01Killed

Everytime I install I get the same output. I have a django project that is working just fine on my local machine but when I test it with python3.8 manage.py runserver --insecure on the EC2 instance:

Performing system checks...




Failed to import tensorflow. Please note that tensorflow is not installed by default when you install tensorflow_hub. This is so that users can decide which tensorflow package to use. To use tensorflow_hub, please install a current version of tensorflow by following the instructions at https://tensorflow.org/install and https://tensorflow.org/hub/installation.




Failed to import tensorflow. Please note that tensorflow is not installed by default when you install tensorflow_hub. This is so that users can decide which tensorflow package to use. To use tensorflow_hub, please install a current version of tensorflow by following the instructions at https://tensorflow.org/install and https://tensorflow.org/hub/installation.


Exception in thread django-main-thread:
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/threading.py", line 932, in _bootstrap_inner
    self.run()
  File "/usr/local/lib/python3.8/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "/home/ec2-user/.local/lib/python3.8/site-packages/django/utils/autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "/home/ec2-user/.local/lib/python3.8/site-packages/django/core/management/commands/runserver.py", line 118, in inner_run
    self.check(display_num_errors=True)
  File "/home/ec2-user/.local/lib/python3.8/site-packages/django/core/management/base.py", line 419, in check
    all_issues = checks.run_checks(
  File "/home/ec2-user/.local/lib/python3.8/site-packages/django/core/checks/registry.py", line 76, in run_checks
    new_errors = check(app_configs=app_configs, databases=databases)
  File "/home/ec2-user/.local/lib/python3.8/site-packages/django/core/checks/urls.py", line 13, in check_url_config
    return check_resolver(resolver)
  File "/home/ec2-user/.local/lib/python3.8/site-packages/django/core/checks/urls.py", line 23, in check_resolver
    return check_method()
  File "/home/ec2-user/.local/lib/python3.8/site-packages/django/urls/resolvers.py", line 412, in check
    for pattern in self.url_patterns:
  File "/home/ec2-user/.local/lib/python3.8/site-packages/django/utils/functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/ec2-user/.local/lib/python3.8/site-packages/django/urls/resolvers.py", line 598, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/home/ec2-user/.local/lib/python3.8/site-packages/django/utils/functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/ec2-user/.local/lib/python3.8/site-packages/django/urls/resolvers.py", line 591, in urlconf_module
    return import_module(self.urlconf_name)
  File "/usr/local/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 843, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/ec2-user/Style-transfer-app/websrc/styletransferapp/urls.py", line 23, in <module>
    path('', include('mainpage.urls')),
  File "/home/ec2-user/.local/lib/python3.8/site-packages/django/urls/conf.py", line 34, in include
    urlconf_module = import_module(urlconf_module)
  File "/usr/local/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 843, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/ec2-user/Style-transfer-app/websrc/mainpage/urls.py", line 2, in <module>
    from . import views
  File "/home/ec2-user/Style-transfer-app/websrc/mainpage/views.py", line 3, in <module>
    import tensorflow_hub as hub
  File "/home/ec2-user/.local/lib/python3.8/site-packages/tensorflow_hub/__init__.py", line 85, in <module>
    _ensure_tf_install()
  File "/home/ec2-user/.local/lib/python3.8/site-packages/tensorflow_hub/__init__.py", line 49, in _ensure_tf_install
    import tensorflow as tf
ModuleNotFoundError: No module named 'tensorflow'

When I run pip3.8 freeze after trying pip3.8 install tensorflow several times I get:

asgiref==3.5.0
Django==3.2.5
numpy==1.22.2
protobuf==3.19.4
pytz==2021.3
sqlparse==0.4.2
tensorflow-hub==0.12.0

When I run pip3.8 --version I get:

pip 21.1.1 from /usr/local/lib/python3.8/site-packages/pip (python 3.8)

I have also tried sudo pip3.8 install tensorflow which has given the same output as when I run the same command without sudo.

I am using python 3.8.12 (I have installed it manually from source).

Does anyone know how I can fix this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source