我使用 pip 安装tensorflow,但是tensorflow import 不起作用

发布于 2025-01-11 23:05:17 字数 5959 浏览 0 评论 0原文

我已经安装了tensorflow,如下所示:pip3.8 install tensorflow(我使用pip3.8命令,因为我在我的EC2实例上安装了多个python版本)。当我这样做时,我会得到以下输出:

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

每次安装时都会得到相同的输出。我有一个 django 项目在我的本地计算机上运行得很好,但是当我在 EC2 实例上使用 python3.8 manage.py runserver --insecure 测试它时

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'

: 8 冻结 尝试 pip3.8 install tensorflow 几次后,我得到:

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

当我运行 pip3.8 --version 时,我得到:

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

我也尝试过 sudo pip3.8安装tensorflow,它给出了与我在没有sudo的情况下运行相同命令时相同的输出。

我正在使用 python 3.8.12 (我已经从源代码手动安装了它)。

有谁知道我该如何解决这个问题?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文