简体   繁体   中英

module 'tensorflow' has no attribute 'Session' cannot resolve

Please please please help me out here! I cannot resolve it!

so... my python version is Python 3.5.6 and Anaconda version is Conda 4.8.3. I am working with Jupyter Notebook.

I am working in MacOS.

I have created an environment tfdeeplearning:

conda create -n tfdeeplearning python=3.5

which I have installed the following:

1. conda install jupyter
2. conda install numpy
3. conda install pandas
4. conda install scikit-learn
5. conda install matplotlib
6. pip install --upgrade tensorflow 

So, in Jupyter Notebook, I am trying to run this code:

import tensorflow as tf 
hello = tf.constant("Hello world")
sess = tf.Session()
print(sess.run(hello))

I am constantly getting this error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-3-f75057d1d95f> in <module>()
----> 1 sess = tf.Session()

AttributeError: module 'tensorflow' has no attribute 'Session'

I understand that the tf.Session() is not supported in tensorflow 2.2.0 which is the version I have after running the line pip install --upgrade tensorflow in the tfdeeplearning environment.

I have also tried doing:

pip install tensorflow==1.4.0

this broke the environment tfdeeplearning, and I had to reset the env.

I have tried:

pip uninstall tensorflow-gpu
pip install tensorflow-gpu

which again, broke the env and I had to set it up once again.

then I tried this in jupyter notebooks which is replacing import tensorflow as tf:

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

which did not work either, because this time the error said

AttributeError: module 'tensorflow' has no attribute 'compat'

So, I am out of options

PS Here is a list of all the dependencies I have in my virtual env called tfdeeplearning in conda if it helps at all:

absl-py==0.9.0
appnope==0.1.0
astunparse==1.6.3
bleach==3.1.5
cachetools==4.1.1
certifi==2018.8.24
chardet==3.0.4
cycler==0.10.0
Cython==0.29.21
decorator==4.4.2
defusedxml==0.6.0
entrypoints==0.2.3
gast==0.3.3
google-auth==1.19.2
google-auth-oauthlib==0.4.1
google-pasta==0.2.0
grpcio==1.30.0
h5py==2.10.0
idna==2.10
importlib-metadata==1.7.0
ipykernel==4.10.0
ipython==5.8.0
ipython-genutils==0.2.0
ipywidgets==7.4.1
Jinja2==2.11.2
jsonschema==2.6.0
jupyter==1.0.0
jupyter-client==5.3.3
jupyter-console==5.2.0
jupyter-core==4.5.0
Keras-Preprocessing==1.1.2
kiwisolver==1.0.1
Markdown==3.2.2
MarkupSafe==1.0
matplotlib==3.0.0
mistune==0.8.3
mkl-fft==1.0.6
mkl-random==1.0.1
nbconvert==5.5.0
nbformat==5.0.7
notebook==5.6.0
numpy==1.18.5
oauthlib==3.1.0
opt-einsum==3.3.0
packaging==20.4
pandas==0.22.0
pandocfilters==1.4.2
pexpect==4.6.0
pickleshare==0.7.4
prometheus-client==0.8.0
prompt-toolkit==1.0.15
protobuf==3.12.2
ptyprocess==0.6.0
pyasn1==0.4.8
pyasn1-modules==0.2.8
Pygments==2.6.1
pyparsing==2.4.7
python-dateutil==2.8.1
pytz==2020.1
pyzmq==17.1.2
qtconsole==4.7.5
QtPy==1.9.0
requests==2.24.0
requests-oauthlib==1.3.0
rsa==4.6
scikit-learn==0.20.0
scipy==1.4.1
Send2Trash==1.5.0
simplegeneric==0.8.1
six==1.15.0
TBB==0.1
tensorboard==2.2.2
tensorboard-plugin-wit==1.7.0
tensorflow==2.2.0
tensorflow-estimator==2.2.0
termcolor==1.1.0
terminado==0.8.1
testpath==0.4.4
tornado==5.1.1
traitlets==4.3.2
urllib3==1.25.10
wcwidth==0.2.5
webencodings==0.5.1
Werkzeug==1.0.1
widgetsnbextension==3.4.1
wrapt==1.12.1
zipp==1.2.0

Install Tensorflow 1.15.3.

!pip install tensorflow==1.15.3

import tensorflow as tf
msg = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(msg))

Tensorflow 2.x runs on Eager execution by default. Not required to create graph by calling session. Still you can access session by tf.compat.v1.Session() in TF2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM