Jupyter Notebook导入TensorFlow报错:找不到tensorflow.security模块
TensorFlow导入报错:ModuleNotFoundError: No module named 'tensorflow.security'
问题场景
为让TensorFlow使用GPU而非CPU运行,卸载原有版本后仅安装tensorflow-gpu包,但在Jupyter Notebook中执行import tensorflow as tf时触发以下报错:
ModuleNotFoundError Traceback (most recent call last) Cell In[2], line 1 ----> 1 import tensorflow as tf File ~\AppData\Roaming\Python\Python310\site-packages\tensorflow\__init__.py:38 35 import sys as _sys 36 import typing as _typing ---> 38 from tensorflow.python.tools import module_util as _module_util 39 from tensorflow.python.util.lazy_loader import LazyLoader as _LazyLoader 41 # Make sure code inside the TensorFlow codebase can use tf2.enabled() at import. File ~\AppData\Roaming\Python\Python310\site-packages\tensorflow\python\__init__.py:45 43 from tensorflow.python import distribute 44 # from tensorflow.python import keras ---> 45 from tensorflow.python.feature_column import feature_column_lib as feature_column 46 # from tensorflow.python.layers import layers 47 from tensorflow.python.module import module File ~\AppData\Roaming\Python\Python310\site-packages\tensorflow\python\feature_column\feature_column_lib.py:18 15 """FeatureColumns: tools for ingesting and representing features.""" 17 # pylint: disable=unused-import,line-too-long,wildcard-import,g-bad-import-order ---> 18 from tensorflow.python.feature_column.feature_column import * 19 from tensorflow.python.feature_column.feature_column_v2 import * 20 from tensorflow.python.feature_column.sequence_feature_column import * File ~\AppData\Roaming\Python\Python310\site-packages\tensorflow\python\feature_column\feature_column.py:143 141 from tensorflow.python.framework import sparse_tensor as sparse_tensor_lib 142 from tensorflow.python.framework import tensor_shape --> 143 from tensorflow.python.layers import base 144 from tensorflow.python.ops import array_ops 145 from tensorflow.python.ops import check_ops File ~\AppData\Roaming\Python\Python310\site-packages\tensorflow\python\layers\base.py:16 1 # Copyright 2015 The TensorFlow Authors. All Rights Reserved. 2 # 3 # Licensed under the Apache License, Version 2.0 (the "License"); (...) 13 # limitations under the License. 14 # ============================================================================= 15 """Contains the base Layer class, from which all layers inherit.""" ---> 16 from tensorflow.python.keras.legacy_tf_layers import base 18 InputSpec = base.InputSpec 20 keras_style_scope = base.keras_style_scope File ~\AppData\Roaming\Python\Python310\site-packages\tensorflow\python\keras\__init__.py:25 22 from tensorflow.python.keras import distribute 24 # See b/110718070#comment18 for more details about this import. ---> 25 from tensorflow.python.keras import models 27 from tensorflow.python.keras.engine.input_layer import Input 28 from tensorflow.python.keras.engine.sequential import Sequential File ~\AppData\Roaming\Python\Python310\site-packages\tensorflow\python\keras\models.py:19 16 """Code for model cloning, plus model-related API entries.""" 18 from tensorflow.python.framework import ops ---> 19 from tensorflow.python.keras import backend 20 from tensorflow.python.keras import metrics as metrics_module 21 from tensorflow.python.keras import optimizer_v1 File ~\AppData\Roaming\Python\Python310\site-packages\tensorflow\python\keras\backend.py:50 48 from tensorflow.python.framework import tensor_util 49 from tensorflow.python.keras import backend_config ---> 50 from tensorflow.python.keras.distribute import distribute_coordinator_utils as dc 51 from tensorflow.python.keras.engine import keras_tensor 52 from tensorflow.python.keras.utils import control_flow_util File ~\AppData\Roaming\Python\Python310\site-packages\tensorflow\python\keras\distribute\distribute_coordinator_utils.py:33 31 from tensorflow.python.client import session 32 from tensorflow.python.platform import tf_logging as logging ---> 33 from tensorflow.python.training import monitored_session 34 from tensorflow.python.training import server_lib 36 _worker_context = threading.local() File ~\AppData\Roaming\Python\Python310\site-packages\tensorflow\python\training\monitored_session.py:22 19 import os 21 from tensorflow.core.protobuf import config_pb2 ---> 22 from tensorflow.python.checkpoint import checkpoint as trackable_util 23 from tensorflow.python.checkpoint import graph_view 24 from tensorflow.python.distribute import distribute_coordinator_context File ~\AppData\Roaming\Python\Python310\site-packages\tensorflow\python\checkpoint\checkpoint.py:30 28 from tensorflow.python.checkpoint import checkpoint_options 29 from tensorflow.python.checkpoint import functional_saver ---> 30 from tensorflow.python.checkpoint import graph_view as graph_view_lib 31 from tensorflow.python.checkpoint import restore as restore_lib 32 from tensorflow.python.checkpoint import save_util File ~\AppData\Roaming\Python\Python310\site-packages\tensorflow\python\checkpoint\graph_view.py:19 16 import copy 17 import weakref ---> 19 from tensorflow.python.checkpoint import save_util_v1 20 from tensorflow.python.checkpoint import trackable_view 21 from tensorflow.python.trackable import base File ~\AppData\Roaming\Python\Python310\site-packages\tensorflow\python\checkpoint\save_util_v1.py:25 23 from tensorflow.core.protobuf import trackable_object_graph_pb2 24 from tensorflow.python.checkpoint import saveable_compat ---> 25 from tensorflow.python.checkpoint import util 26 from tensorflow.python.framework import constant_op 27 from tensorflow.python.framework import dtypes File ~\AppData\Roaming\Python\Python310\site-packages\tensorflow\python\checkpoint\util.py:21 19 from tensorflow.python.ops import variables 20 from tensorflow.python.trackable import trackable_utils ---> 21 from tensorflow.python.training import optimizer as optimizer_v1 22 from tensorflow.python.util import object_identity 25 def serialize_slot_variables(trackable_objects, node_ids, object_names): File ~\AppData\Roaming\Python\Python310\site-packages\tensorflow\python\training\optimizer.py:22 19 import abc 21 from tensorflow.python.distribute import distribute_lib ---> 22 from tensorflow.python.distribute import distribute_utils 23 from tensorflow.python.distribute import distribution_strategy_context as distribute_ctx 24 from tensorflow.python.distribute import reduce_util as ds_reduce_util File ~\AppData\Roaming\Python\Python310\site-packages\tensorflow\python\distribute\distribute_utils.py:21 18 import contextlib 19 import threading ---> 21 from tensorflow.python.distribute import tpu_values as tpu_values_lib 22 from tensorflow.python.distribute import values as values_lib 23 from tensorflow.python.eager import context File ~\AppData\Roaming\Python\Python310\site-packages\tensorflow\python\distribute\tpu_values.py:22 15 """Various classes representing TPU distributed values. 16 17 Note that the tests are in values_test.py . 18 19 """ 21 from tensorflow.python.distribute import packed_distributed_variable as packed ---> 22 from tensorflow.python.distribute import tpu_replicated_variable 23 from tensorflow.python.distribute import tpu_util 24 from tensorflow.python.distribute import values File ~\AppData\Roaming\Python\Python310\site-packages\tensorflow\python\distribute\tpu_replicated_variable.py:23 20 from collections import abc 21 import contextlib ---> 23 from tensorflow.python.compiler.xla.experimental import xla_sharding 24 from tensorflow.python.distribute import tpu_util 25 from tensorflow.python.eager import context File ~\AppData\Roaming\Python\Python310\site-packages\tensorflow\python\compiler\xla\__init__.py:19 17 # pylint: disable=unused-import 18 from tensorflow.python.compiler.xla import jit ---> 19 from tensorflow.python.compiler.xla import xla File ~\AppData\Roaming\Python\Python310\site-packages\tensorflow\python\compiler\xla\xla.py:20 15 """xla is an experimental library that provides XLA support APIs.""" 17 import contextlib ---> 20 from tensorflow.compiler.jit.ops import xla_ops 21 from tensorflow.compiler.jit.ops import xla_ops_grad # pylint: disable=unused-import 22 from tensorflow.core.framework import attr_value_pb2 File ~\AppData\Roaming\Python\Python310\site-packages\tensorflow\compiler\jit\ops\xla_ops.py:13 11 from tensorflow.python.eager import execute as _execute 12 from tensorflow.python.framework import dtypes as _dtypes ---> 13 from tensorflow.security.fuzzing.py import annotation_types as _atypes 15 from tensorflow.python.framework import op_def_registry as _op_def_registry 16 from tensorflow.python.framework import ops as _ops ModuleNotFoundError: No module named 'tensorflow.security'
已执行的排查操作:
- 多次重新安装TensorFlow,
pip show tensorflow显示安装状态正常 - 重装Anaconda并清理安装缓存
- 目标是使用GPU运行TensorFlow,但当前连基础导入都无法完成
解决步骤
1. 彻底清理TensorFlow残留
- 卸载所有TensorFlow相关包:
pip uninstall -y tensorflow tensorflow-gpu - 手动删除Python site-packages中的TensorFlow残留文件夹,路径示例:
~\AppData\Roaming\Python\Python310\site-packages\tensorflow,同时删除所有以tensorflow-开头的文件夹 - 清理pip缓存:
pip cache purge
2. 安装正确版本的TensorFlow
TensorFlow 2.x之后已将GPU支持整合到主包,tensorflow-gpu包已被弃用,无需单独安装。需根据本地CUDA和cuDNN版本选择匹配的TensorFlow版本:
- 确认CUDA/cuDNN版本后,执行对应版本的安装命令,例如适配CUDA 11.8的版本:
pip install tensorflow==2.15.0 - 若安装速度慢,可使用国内镜像源:
pip install tensorflow==2.15.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
3. 对齐Jupyter环境与TensorFlow安装环境
确保Jupyter使用的Python环境和安装TensorFlow的环境一致:
- 在Jupyter Notebook中执行以下代码查看当前环境路径:
import sys print(sys.executable) - 在终端中执行
where python(Windows)或which python(Linux/macOS)查看TensorFlow安装环境的路径 - 若路径不一致,需将TensorFlow安装到Jupyter对应的环境,或在Jupyter中切换内核到正确的Python环境
4. 验证安装成功
安装完成后,在终端或Jupyter中执行以下代码验证:
import tensorflow as tf print(tf.__version__) print(tf.config.list_physical_devices('GPU'))
若输出GPU设备信息,则说明GPU配置成功
内容的提问来源于stack exchange,提问作者Ahmad Syauqi
相关产品推荐
相关产品推荐

