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

拈花ヽ惹草 提交于 2021-02-10 14:51:01

问题


I created virtual environment and installed both tensorflow and tensorflow-gpu. After that I installed keras. And then I checked in my conda terminal by importing keras and I was able to import keras in it. However, using jupyter notebook if I try to import keras then it gives me below error.

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. I'm pretty new to programming so I am not sure how to go around it. Tried looking other threads but not helping. Can any one recommend what can I do to resolve it? Thanks


回答1:


Did you installed the dependencies with 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. Look at your conda dependencies list, to see if the tensorflow and keras are really there using:

$ 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):

(your_env)$ jupyter notebook



回答2:


Doing below solved my issue.

So I removed all the packages that were installed via pip and intstalled packages through 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. If you skip create environment command, type in below as it will scratch off new env and specify python and tensorflow version.

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. 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. And it got resolved like that.



来源:https://stackoverflow.com/questions/62787056/importerror-cannot-import-name-context-from-tensorflow-python-eager-unknow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!