You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TensorFlow与Transformers联用出现'NoneType'无Rlocation属性错误求助

问题:TensorFlow与Transformers Pipelines联用时导入报错

我在多个多智能体系统项目中同时使用TensorFlow和Transformers pipelines,连续多天遇到导入错误,仅在两者联用时出现,找不到解决办法,我是这些库的新手,求帮助。

错误日志

from transformers import pipeline

I tensorflow/core/util/port.cc:153] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
2024-09-03 23:40:00.379521: I tensorflow/core/util/port.cc:153] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
Traceback (most recent call last):
  File "C:\Users\Preston Creed\AppData\Local\Programs\Python\Python312\Lib\site-packages\transformers\utils\import_utils.py", line 1603, in _get_module
    return importlib.import_module("." + module_name, self.__name__)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Preston Creed\AppData\Local\Programs\Python\Python312\Lib\importlib\__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 995, in exec_module
  File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
  File "C:\Users\Preston Creed\AppData\Local\Programs\Python\Python312\Lib\site-packages\transformers\pipelines\__init__.py", line 26, in <module>
    from ..image_processing_utils import BaseImageProcessor
  File "C:\Users\Preston Creed\AppData\Local\Programs\Python\Python312\Lib\site-packages\transformers\image_processing_utils.py", line 21, in <module>
    from .image_transforms import center_crop, normalize, rescale
  File "C:\Users\Preston Creed\AppData\Local\Programs\Python\Python312\Lib\site-packages\transformers\image_transforms.py", line 49, in <module>
    import tensorflow as tf
  File "C:\Users\Preston Creed\AppData\Local\Programs\Python\Python312\Lib\site-packages\tensorflow\__init__.py", line 53, in <module>
    from tensorflow._api.v2 import compat
  File "C:\Users\Preston Creed\AppData\Local\Programs\Python\Python312\Lib\site-packages\tensorflow\_api\v2\compat\__init__.py", line 8, in <module>
    from tensorflow._api.v2.compat import v1
  File "C:\Users\Preston Creed\AppData\Local\Programs\Python\Python312\Lib\site-packages\tensorflow\_api\v2\compat\v1\__init__.py", line 30, in <module>
    from tensorflow._api.v2.compat.v1 import compat
  File "C:\Users\Preston Creed\AppData\Local\Programs\Python\Python312\Lib\site-packages\tensorflow\_api\v2\compat\v1\compat\__init__.py", line 8, in <module>
    from tensorflow._api.v2.compat.v1.compat import v1
  File "C:\Users\Preston Creed\AppData\Local\Programs\Python\Python312\Lib\site-packages\tensorflow\_api\v2\compat\v1\compat\v1\__init__.py", line 47, in <module>
    from tensorflow._api.v2.compat.v1 import lite
  File "C:\Users\Preston Creed\AppData\Local\Programs\Python\Python312\Lib\site-packages\tensorflow\_api\v2\compat\v1\lite\__init__.py", line 9, in <module>
    from tensorflow._api.v2.compat.v1.lite import experimental
  File "C:\Users\Preston Creed\AppData\Local\Programs\Python\Python312\Lib\site-packages\tensorflow\_api\v2\compat\v1\lite\experimental\__init__.py", line 8, in <module>
    from tensorflow._api.v2.compat.v1.lite.experimental import authoring
  File "C:\Users\Preston Creed\AppData\Local\Programs\Python\Python312\Lib\site-packages\tensorflow\_api\v2\compat\v1\lite\experimental\authoring\__init__.py", line 8, in <module>
    from tensorflow.lite.python.authoring.authoring import compatible # line: 265
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Preston Creed\AppData\Local\Programs\Python\Python312\Lib\site-packages\tensorflow\lite\python\authoring\authoring.py", line 43, in <module>
    from tensorflow.lite.python import convert
  File "C:\Users\Preston Creed\AppData\Local\Programs\Python\Python312\Lib\site-packages\tensorflow\lite\python\convert.py", line 151, in <module>
    _deprecated_conversion_binary = _resource_loader.get_path_to_datafile(
                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Preston Creed\AppData\Local\Programs\Python\Python312\Lib\site-packages\tensorflow\python\platform\resource_loader.py", line 118, in get_path_to_datafile
    new_fpath = r.Rlocation(
                ^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'Rlocation'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "D:\DMNSIONS\General_AI_Projects\AI_Agents\MegaAgent_Architecture\test.py", line 1, in <module>
    from transformers import pipeline
  File "<frozen importlib._bootstrap>", line 1412, in _handle_fromlist
  File "C:\Users\Preston Creed\AppData\Local\Programs\Python\Python312\Lib\site-packages\transformers\utils\import_utils.py", line 1593, in __getattr__
    module = self._get_module(self._class_to_module[name])
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Preston Creed\AppData\Local\Programs\Python\Python312\Lib\site-packages\transformers\utils\import_utils.py", line 1605, in _get_module
    raise RuntimeError(
RuntimeError: Failed to import transformers.pipelines because of the following error (look up to see its traceback):
'NoneType' object has no attribute 'Rlocation'

依赖版本列表

astunparse==1.6.3
certifi==2024.8.30
charset-normalizer==3.3.2
click==8.1.7
colorama==0.4.6
docx==0.2.4
filelock==3.15.4
flatbuffers==24.3.25
fsspec==2024.9.0
gast==0.6.0
google-pasta==0.2.0
grpcio==1.66.1
h5py==3.11.0
huggingface-hub==0.24.6
idna==3.8
joblib==1.4.2
keras==3.5.0
libclang==18.1.1
lxml==5.3.0
Markdown==3.7
markdown-it-py==3.0.0
MarkupSafe==2.1.5
mdurl==0.1.2
ml-dtypes==0.4.0
namex==0.0.8
nltk==3.9.1
numpy==1.26.4
opt-einsum==3.3.0
optree==0.12.1
packaging==24.1
pillow==10.4.0
protobuf==4.25.4
Pygments==2.18.0
PyYAML==6.0.2
regex==2024.7.24
requests==2.32.3
rich==13.8.0
safetensors==0.4.5
setuptools==74.1.2
six==1.16.0
tensorboard==2.17.1
tensorboard-data-server==0.7.2
tensorflow==2.17.0
tensorflow-intel==2.17.0
termcolor==2.4.0
tokenizers==0.19.1
tqdm==4.66.5
transformers==4.44.2
typing_extensions==4.12.2
urllib3==2.2.2
Werkzeug==3.0.4
wheel==0.44.0
wrapt==1.16.0

解决办法

1. 调整导入顺序

先导入TensorFlow,再导入Transformers的pipeline:

import tensorflow as tf
from transformers import pipeline

2. 降级TensorFlow版本

当前TensorFlow 2.17.0可能和Transformers 4.44.2存在兼容问题,尝试降级到2.16.1:

pip install tensorflow==2.16.1 tensorflow-intel==2.16.1

3. 排除TensorFlow Lite相关模块

如果不需要TensorFlow Lite功能,可以在导入前设置环境变量,跳过相关模块加载:

import os
os.environ['TF_ENABLE_LITE_SUPPORT'] = '0'
from transformers import pipeline

4. 重新安装依赖

先卸载现有TensorFlow和Transformers,再重新安装:

pip uninstall -y tensorflow tensorflow-intel transformers
pip install tensorflow==2.16.1 transformers==4.44.2

内容的提问来源于stack exchange,提问作者Preston Creed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 20:37:05