首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >tensorflow-hub:当文件位于特定文件夹中时,Python导入错误

tensorflow-hub:当文件位于特定文件夹中时,Python导入错误
EN

Stack Overflow用户
提问于 2019-06-21 08:54:36
回答 1查看 1.6K关注 0票数 0

所以我遇到了一个非常奇怪的问题。我正在尝试运行这个网页上给出的tensorflow-hub示例代码:https://www.tensorflow.org/hub (我删除了这两条!pip语句)。

我想在我的应用程序中使用一小块python代码来进行机器学习,否则它是用go编写的。但是,当我在应用程序的项目目录中执行示例代码时,它会给出一个导入错误。

因此,当我在~/中创建一个文件tf-hub.py并执行它时,它会运行,打印一些警告并得到结果:

代码语言:javascript
复制
$python3 tf-hub.py
WARNING: Logging before flag parsing goes to stderr.
W0621 10:35:21.367268 140170246772224 deprecation_wrapper.py:118] From tf-hub.py:4: The name tf.enable_eager_execution is deprecated. Please use tf.compat.v1.enable_eager_execution instead.

2019-06-21 10:35:21.407732: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2019-06-21 10:35:21.427007: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 2793545000 Hz
2019-06-21 10:35:21.428085: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x563567fc7a90 executing computations on platform Host. Devices:
2019-06-21 10:35:21.428143: I tensorflow/compiler/xla/service/service.cc:175]   StreamExecutor device (0): <undefined>, <undefined>
2019-06-21 10:35:21.505990: W tensorflow/compiler/jit/mark_for_compilation_pass.cc:1541] (One-time warning): Not using XLA:CPU for cluster because envvar TF_XLA_FLAGS=--tf_xla_cpu_global_jit was not set.  If you want XLA:CPU, either set that envvar, or use experimental_jit_scope to enable XLA:CPU.  To confirm that XLA is active, pass --vmodule=xla_compilation_cache=1 (as a proper command-line flag, not via TF_XLA_FLAGS) or set the envvar XLA_FLAGS=--xla_hlo_profile.
2019-06-21 10:35:21.513612: W tensorflow/core/framework/cpu_allocator_impl.cc:81] Allocation of 498570752 exceeds 10% of system memory.
2019-06-21 10:35:22.109499: W tensorflow/core/framework/cpu_allocator_impl.cc:81] Allocation of 498570752 exceeds 10% of system memory.
(3, 128)

但是,当我将同一个文件复制到go项目的项目文件夹时,会得到以下导入错误:

代码语言:javascript
复制
$ python3 go/src/MyProject/tagger/imageClassifier/tf-hub.py 
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/tensorflow_hub/tf_v1.py", line 29, in <module>
    from tensorflow.compat.v1 import *  # pylint: disable=wildcard-import
ModuleNotFoundError: No module named 'tensorflow.compat'; 'tensorflow' is not a package

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "go/src/EmbeddingImageTagger/tagger/imageClassifier/tf-hub.py", line 1, in <module>
    import tensorflow as tf
  File "/home/***/go/src/EmbeddingImageTagger/tagger/imageClassifier/tensorflow.py", line 2, in <module>
    import tensorflow_hub as hub
  File "/usr/local/lib/python3.7/site-packages/tensorflow_hub/__init__.py", line 30, in <module>
    from tensorflow_hub.estimator import LatestModuleExporter
  File "/usr/local/lib/python3.7/site-packages/tensorflow_hub/estimator.py", line 25, in <module>
    from tensorflow_hub import tf_utils
  File "/usr/local/lib/python3.7/site-packages/tensorflow_hub/tf_utils.py", line 28, in <module>
    from tensorflow_hub import tf_v1
  File "/usr/local/lib/python3.7/site-packages/tensorflow_hub/tf_v1.py", line 33, in <module>
    from tensorflow import add_to_collection
ImportError: cannot import name 'add_to_collection' from 'tensorflow' (/home/***/go/src/MyProject/tagger/imageClassifier/tensorflow.py)

我必须每晚使用tf,否则我会遇到以下问题:https://github.com/tensorflow/hub/issues/289

我正在使用:

  • Fedora 29 64位
  • Python 3.7.2
  • tf-每晚1.14.1.dev20190621
  • tensorflow-轮毂0.4.0

有什么想法可以导致文件在一个目录中运行而不是在另一个目录中运行这种奇怪的行为呢?我还试着从同一个终端运行。但它仍然不起作用。

EN

回答 1

Stack Overflow用户

发布于 2019-06-21 09:02:26

哦天啊。所以我在清理了调试程序并将我的开发转移到我的主文件夹后找到了罪魁祸首。

问题是我有一个名为tensorflow.py的python文件,因为它应该包含一个带有tensorflow的分类器解决方案。这似乎混淆了python,而python现在在导入tensorflow时导入这个文件。

我不得不重新命名这个文件,现在它起作用了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56700022

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档