【问题标题】:import through repl is fine but breaks from script通过 repl 导入很好,但会中断脚本
【发布时间】:2021-10-26 00:27:41
【问题描述】:

我有以下test1.py 脚本:

import tfrecorder

如果我运行它,它会失败:

I have no name!@519b05bc3bf9:/test$ python test1.py 
2021-10-26 00:22:56.863971: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'libcudart.so.10.1'; dlerror: libcudart.so.10.1: cannot open shared object file: No such file or directory
2021-10-26 00:22:56.863989: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
Traceback (most recent call last):
  File "test1.py", line 1, in <module>
    import tfrecorder
  File "/usr/local/lib/python3.7/site-packages/tfrecorder/__init__.py", line 19, in <module>
    from tfrecorder import accessor
  File "/usr/local/lib/python3.7/site-packages/tfrecorder/accessor.py", line 28, in <module>
    from tfrecorder import converter
  File "/usr/local/lib/python3.7/site-packages/tfrecorder/converter.py", line 27, in <module>
    import apache_beam as beam
  File "/usr/local/lib/python3.7/site-packages/apache_beam/__init__.py", line 104, in <module>
    from apache_beam import coders
  File "/usr/local/lib/python3.7/site-packages/apache_beam/coders/__init__.py", line 19, in <module>
    from apache_beam.coders.coders import *
  File "/usr/local/lib/python3.7/site-packages/apache_beam/coders/coders.py", line 43, in <module>
    from future.moves import pickle
  File "/usr/local/lib/python3.7/site-packages/future/moves/__init__.py", line 8, in <module>
    import_top_level_modules()
  File "/usr/local/lib/python3.7/site-packages/future/standard_library/__init__.py", line 810, in import_top_level_modules
    with exclude_local_folder_imports(*TOP_LEVEL_MODULES):
  File "/usr/local/lib/python3.7/site-packages/future/standard_library/__init__.py", line 781, in __enter__
    module = __import__(m, level=0)
  File "/test/test.py", line 1, in <module>
    from tfrecorder import input_schema
  File "/usr/local/lib/python3.7/site-packages/tfrecorder/input_schema.py", line 26, in <module>
    from tfrecorder import types
  File "/usr/local/lib/python3.7/site-packages/tfrecorder/types.py", line 24, in <module>
    from tensorflow_transform import beam as tft_beam
  File "/usr/local/lib/python3.7/site-packages/tensorflow_transform/beam/__init__.py", line 20, in <module>
    from tensorflow_transform.beam import analyzer_cache
  File "/usr/local/lib/python3.7/site-packages/tensorflow_transform/beam/analyzer_cache.py", line 163, in <module>
    class _WriteToTFRecordGzip(beam.io.WriteToTFRecord):
AttributeError: module 'apache_beam' has no attribute 'io'

但是,在 REPL 中没问题:

I have no name!@ff017054aef4:/$ python
Python 3.7.12 (default, Oct 12 2021, 03:26:18) 
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tfrecorder
2021-10-26 00:24:33.293181: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'libcudart.so.10.1'; dlerror: libcudart.so.10.1: cannot open shared object file: No such file or directory
2021-10-26 00:24:33.293201: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
>>> 

这对我来说没有任何意义。 有什么想法吗?

【问题讨论】:

  • 看起来你的 repl 的 PYTHONPATH 与脚本不同。你的which python 命令告诉你什么?
  • /usr/local/bin/python
  • 尝试在两者中打印sys.path,看看它们是否不同。
  • 我刚刚从我的错误输出中看到,不知何故我的test1.py 脚本最终运行了我的另一个测试文件test.py,这似乎触发了错误。 test.py 可能是要避免的脚本名称。
  • 看起来像一个循环导入问题。我不确定为什么需要未来的模块。你用的是什么版本的apache_beam

标签: python apache-beam


【解决方案1】:

这似乎是一个错误,并且已在最新版本的apache_beam 中得到纠正。该线程中的解释是描述性的。 Circular Import Issue in apache_beam

问题在于TOP_LEVEL_MODULES 变量定义了一个包含test 的模块列表。您的 test 模块名称与 apache_beam 中的内置模块冲突。您可以尝试重命名 test 模块,然后检查。

您在 shell 中看不到同样的问题,因为 sys.path 在 shell 环境中没有目录 /test

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-01
    • 2021-11-19
    • 2022-09-30
    • 1970-01-01
    • 2014-11-22
    • 2014-11-27
    • 1970-01-01
    • 2018-09-17
    相关资源
    最近更新 更多