简体   繁体   English

ImportError:无法从“tensorflow.python.eager”(未知位置)导入名称“context”

[英]ImportError: cannot import name 'context' from 'tensorflow.python.eager' (unknown location)

I created virtual environment and installed both tensorflow and tensorflow-gpu.我创建了虚拟环境并安装了 tensorflow 和 tensorflow-gpu。 After that I installed keras.之后我安装了 keras。 And then I checked in my conda terminal by importing keras and I was able to import keras in it.然后我通过导入 keras 检查了我的 conda 终端,我能够在其中导入 keras。 However, using jupyter notebook if I try to import keras then it gives me below error.但是,如果我尝试导入 keras 使用 jupyter 笔记本,则会出现以下错误。

import keras
ImportError                               Traceback (most recent call last)
<ipython-input-5-88d96843a926> in <module>
----> 1 import keras

~\Anaconda3\lib\site-packages\keras\__init__.py in <module>
      1 from __future__ import absolute_import
      2 
----> 3 from . import utils
      4 from . import activations
      5 from . import applications

~\Anaconda3\lib\site-packages\keras\utils\__init__.py in <module>
      4 from . import data_utils
      5 from . import io_utils
----> 6 from . import conv_utils
      7 from . import losses_utils
      8 from . import metrics_utils

~\Anaconda3\lib\site-packages\keras\utils\conv_utils.py in <module>
      7 from six.moves import range
      8 import numpy as np
----> 9 from .. import backend as K
     10 
     11 

~\Anaconda3\lib\site-packages\keras\backend\__init__.py in <module>
----> 1 from .load_backend import epsilon
      2 from .load_backend import set_epsilon
      3 from .load_backend import floatx
      4 from .load_backend import set_floatx
      5 from .load_backend import cast_to_floatx

~\Anaconda3\lib\site-packages\keras\backend\load_backend.py in <module>
     88 elif _BACKEND == 'tensorflow':
     89     sys.stderr.write('Using TensorFlow backend.\n')
---> 90     from .tensorflow_backend import *
     91 else:
     92     # Try and load external backend.

~\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py in <module>
      4 
      5 import tensorflow as tf
----> 6 from tensorflow.python.eager import context
      7 from tensorflow.python.framework import device as tfdev
      8 from tensorflow.python.framework import ops as tf_ops

ImportError: cannot import name 'context' from 'tensorflow.python.eager' (unknown location)

Already tried uninstalling and installing keras and tensorflow.已经尝试卸载和安装 keras 和 tensorflow。 I'm pretty new to programming so I am not sure how to go around it.我对编程很陌生,所以我不确定如何围绕它进行 go。 Tried looking other threads but not helping.尝试寻找其他线程但没有帮助。 Can any one recommend what can I do to resolve it?任何人都可以推荐我可以做些什么来解决它? Thanks谢谢

Did you installed the dependencies with conda?您是否使用 conda 安装了依赖项? Like this:像这样:

 $ conda install -c conda-forge keras

 $ conda install -c conda-forge tensorflow

 $ conda install -c anaconda tensorflow-gpu

If you installed with pip they will not work inside your virtual env.如果您使用pip安装,它们将无法在您的虚拟环境中工作。 Look at your conda dependencies list, to see if the tensorflow and keras are really there using:查看您的 conda 依赖项列表,看看 tensorflow 和 keras 是否真的存在使用:

$ conda list

If they are, activate your virtual environment:如果是,请激活您的虚拟环境:

$ conda activate 'name_of_your_env'

And run the jupyter inside that, should be something like that (if your env shows in parenthesis the activation worked, and you are now inside the virtual env):并在其中运行 jupyter,应该是这样的(如果您的 env 在括号中显示激活有效,并且您现在在虚拟 env 中):

(your_env)$ jupyter notebook

Doing below solved my issue.在下面做解决了我的问题。

So I removed all the packages that were installed via pip and intstalled packages through conda.所以我删除了所有通过 pip 安装的包并通过 conda 安装包。 I had environment issue and created another environment from the scratch and ran below commands.我遇到了环境问题并从头开始创建了另一个环境并在命令下运行。 Create virtual environment:创建虚拟环境:

conda create -n <env_name>

Install tensorflow-gpu via conda and not pip.通过 conda 而不是 pip 安装 tensorflow-gpu。 If you skip create environment command, type in below as it will scratch off new env and specify python and tensorflow version.如果您跳过创建环境命令,请在下面输入,因为它将刮掉新的环境并指定 python 和 tensorflow 版本。

conda create -n <env_name> python=3.6 tensorflow-gpu=2.2

And then I had to make sure that jupyter notebook is opening with the environment that I want it to open with.然后我必须确保 jupyter notebook 在我希望它打开的环境中打开。 For that below code.对于下面的代码。

C:\Users\Adi(Your user here)\Anaconda3\envs\env_name\python.exe -m ipykernel install --user --name <env_name> --display-name "Python (env_name)"

When you go to Jupyter notebook, on the top right corner you should see your virtual environment and make sure you select that.当您将 go 连接到 Jupyter 笔记本时,您应该在右上角看到您的虚拟环境,并确保您是 select。 And it got resolved like that.它就这样解决了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ImportError:无法从“tensorflow.python.eager.context”导入名称“get_config” - ImportError: cannot import name 'get_config' from 'tensorflow.python.eager.context' ImportError:无法从“tensorflow.python.eager.context”导入名称“get_config” - ANN Visualizer - ImportError: cannot import name 'get_config' from 'tensorflow.python.eager.context' - ANN Visualizer 导入错误:无法从 Raspi-pi 4 上的“tensorflow.python.eager.context”导入错误导入名称“get_config” - ImportError: cannot import name 'get_config' from 'tensorflow.python.eager.context' import error on Raspi-pi 4 ImportError:无法从“tensorflow.python”(未知位置)导入名称“tf2” - ImportError: cannot import name 'tf2' from 'tensorflow.python' (unknown location) 导入错误:无法从 python 中的“包”(未知位置)导入名称“方法” - ImportError: cannot import name 'method' from 'package' (unknown location) in python 我已经安装了 Tensorflow 但我无法在 Python 中导入它:ImportError: cannot import name 'descriptor' from 'google.protobuf'(未知位置) - I have installed Tensorflow but I can not import it in Python: ImportError: cannot import name 'descriptor' from 'google.protobuf' (unknown location) "ImportError:无法从“tensorflow.python.eager.context”导入名称“get_config”[起点:从 keras.layers.core 导入激活]" - ImportError: cannot import name 'get_config' from 'tensorflow.python.eager.context' [start point : from keras.layers.core import Activation] 导入错误:无法从“tensorflow.keras.preprocessing”(未知位置)导入名称“image_dataset_from_directory” - ImportError: cannot import name 'image_dataset_from_directory' from 'tensorflow.keras.preprocessing' (unknown location) Tensorflow 导入错误:无法从“tensorflow.python.eager.def_function”导入名称“experimental_functions_run_eagerly” - Tensorflow ImportError: cannot import name 'experimental_functions_run_eagerly' from 'tensorflow.python.eager.def_function' 无法从“tensorflow.python.eager.context”导入名称“get_config” - cannot import name 'get_config' from 'tensorflow.python.eager.context'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM