问题
I had installed anaconda3.5.2 which installs python 3.6.10. I then installed tensorflow2.0. After installation, from cmd console I get into python and import tensorflow as tf. I typed tf.version and all looks good. Then I launch spyder and do the same, the import does not give any errors, however tf.version says tf is not defined. I looked at sys.path and in both cases the paths are exactly the same.
Appreciate any help and an answer.
回答1:
This can sometimes be the case if we install Tensorflow
in the Base Environment
.
Recommended way is to create a Virtual Environment
in Anaconda
and install the Tensorflow
in that Virtual Environment
, which works in most of the cases.
Using Virtual Environments has advantages like
- We can maintain multiple versions of
Tensorflow
in multipleVirtual Environments
with eachVirtual Environment
comprising eachversion
like1.14, 1.15, 2.0, 2.1, 2.2,etc..
- We can use different
Python Versions
(2.x, 3.6, 3.7
) in eachVirtual Environment
- If we want to modify the
source code
of any of the Tensorflow API, we can do it within our Virtual Environment, without impacting its functionality in otherVirtual Environments
.
Steps for Creating a New Virtual Environment and installing Tensorflow
in Anaconda
, for different Operating Systems, is shown below:
# Create a New Virtual Environment
conda create --name TF_2_VE
# When conda asks you to proceed, type y:
proceed ([y]/n)?
# Activate the Virtual Environment. Conda Version > 4.6
conda activate TF_2_VE
# Activating Virtual Environment, Conda Version < 4.6 and Windows OS
activate TF_2_VE
# Activating Virtual Environment, Conda Version < 4.6 and Linux and Mac OS
source activate TF_2_VE
# Install the TF Version you need
conda install tensorflow
The above command will install the Latest Version
of Tensorflow
(2.2
as of now). If you want an older version like 2.0
, you can replace the last step of the above set of commands with
conda install tensorflow==2.0
.
Hope this information helps. Happy Learning!
来源:https://stackoverflow.com/questions/60239990/tensorflow2-0-isnt-working-with-spyder-with-python-3-6