ModuleNotFoundError: No module named 'tensorflow' error in vscode terminal while running fine in wsl - tensorflow

I installed tensorflow using conda in wsl, and now I'm using it through Jupyter notebook and it's working fine. I also write codes in vscode and then run it through wsl using python3 filename.py and it works fine too.
the problem is when I try to run the code using the 'running' option of vscode and then I get the error ModuleNotFoundError: No module named 'tensorflow' in terminal.
I checked my environments and I only have 'base' environment so the packages should be installed there and I also tried 'conda ...' in vscode terminal and it didn't recognize conda either.
what can be the problem with my vscode?
also I have to say that numpy and matplotlib packages are running fine using vscode itself but tensorflow and some other modules are not identified

You need to create a new virtual environment in anaconda to install Tensorflow and can access the Visual Studio code from the same virtual environment.
Open anaconda prompt and type below code to create virtual environment
conda create -n tf tensorflow
conda activate tf
install TensorFlow in that "tf" virtual environment
pip install tensorflow
launch VS code by selecting the same "tf" virtual environment in anaconda navigator.
Now, type the below code to check if TensorFlow installed successfully in VS code.
import tensorflow as tf
print(tf.__version__)
Likewise, you can install other required packages in the same virtual environment.
pip install <package-name>

Related

Spyder can't load tensorflow in conda environment although python can

I created a conda environment including Spyder and tensorflow in the dependencies and all installs went without errors.
When I activate the environment and launch python, I can import tensorflow and run code without any issues.
However when launching spyder from within the environment (Spyder shows on the bottom of the GUI that is part of that conda environment), and load tensorflow, it throws an error:
ModuleNotFoundError: No module named 'tensorflow_core.estimator'
and from then the Console gets stuck repeating the same error.
Why does spyder have issues importing a module that python in the same environment can import? How can I fix this?
I have tried re-installing spyder in the conda environment but the same issue prevails.
Try to uninstall tensorflow-estimator pip uninstall tensorflow-estimator and uninstall Tensorflow.
#After fresh installing tensorflow
pip install tensorflow
#Install tensorflow_estimator
pip install -U tensorflow_estimator
Try this workaround as well.

How to install tensorflow in win 10

The tensorflow is installed by using "conda install tensorflow". by importing tensorflow with "import tensorflow" in spyder in same environment which tensorflow was installed, it give an error "ModuleNotFoundError: No module named 'tensorflow'".
For every one who is faced with this problem i suggest to check if they have already installed spyder in desired environment with "conda install spyder". I installed it and the problem fixed

import tensorflow working in terminal but not in jupyter notebook

I used the following guide to install tensorflow-gpu - https://towardsdatascience.com/tensorflow-gpu-installation-made-easy-use-conda-instead-of-pip-52e5249374bc
I created a new environment and installed tensorflow-gpu using the command -
conda create --name tf_gpu tensorflow-gpu
If I activate the environment, start python in terminal, and import tensorflow from the terminal, it works.
BUT
When I activate the environment, run a jupyter notebook and type -
import tensorflow
I get module not found error. How do I resolve this?
Start Command Promt (CMD) as administrator (right click). Do not enter any environment yet.
Install Jupyter (and nb_conda as well as ipykernel) to get your environments listed: conda install jupyter nb_conda ipykernel
Activate the environment you want to add to jupyter kernel: conda activate myenv
Install ipykernel in the environment (do this for all envvironemnts you would like to add): conda install ipykernel
To start Jupyter, cd to root (cd .. until you are at C:) then type (does not need to be inside and env): Jupyter noteboook
You might need to confirm that it shall open in a web browser (I use chrome)
Once open in a browser navigate to the folder of your choice, then make a new python 3 file.
Once inside click Kernel -> Change kernel and select the conda env you would like
You should now be able to change kernel (env) within all conda environments that have ipykernel installed (step 4)

How to make default environment as conda environment in Spyder

I have installed tensorflow in conda environment and managed to connect it to jupyter by installing nb_conda and its working. But in Spyder it still shows "tensorflow module name not found" (perhaps Spyder is not connected to the conda environment). I have two questions :
1. How to enable Spyder to connect with conda environment so that I can run tensorflow
2. How to change default environment from "root" in conda prompt to my own created environment.
Spyder runs in the conda enviroment in which it is installed. With Anaconda install, you get it installed in the base interpreter.
For TensorFlow, create a new conda enviroment.
Install tensorflow in it.
conda install tensorflow
Install Spyder in the same enviroment.
conda install spyder
Or, directly install tensorflow in the base interpreter. Open the Anaconda prompt and enter ,
( base ) $ conda install tensorflow

ipython cannot search matplotlib while using tensorflow and jupyter also has import error

I am using python 2.7 in Ubuntu and recently updated tensorflow 0.12.1.
I installed jupyter today for my sample code of tf and I need to use matplotlib. It does not find module name matplotlib and ipython in tensorflow has same error.
1. How can I set path in virtualenv or ipython or jupyter?
After activate tensorflow, I need to use jupyter notebook.
This below in the script for error does not work.
import sys
sys.path.append('my/path/to/module/folder')
import module-of-interest
2. other information: My environments are below.
mickyefromsd#DEKSTOP~$source ~/tensorflow/bin/activate
When I find matplotlib by python script under TF condition and before TF activation, it has below;
/usr/lib/python2.7/dist-packages/matplotlib/
When I type 'which ipython', it has below (not by /usr/bin/ipython) ;
/home/mickeyfromd/tensorflow/bin/ipython
Btw, /tensorflow/lib/python2.7/site-packages/ it has ipython and jupyter.
(not in the same path of matplotlib)
3. My ipython under TF cannot find my existing matplotlib.
(tensorflow) mickeyfromd#DK-DESKTOP:~$ ipython
Python 2.7.11+ (default, Apr 17 2016, 14:00:29)
In [1]: import matplotlib ImportError: No module named matplotlib
4. I wanted to setup virtualenv, so I just run this
I followed this site. site:http://help.pythonanywhere.com/pages/IPythonNotebookVirtualenvs
(tensorflow) mickeyfromd#ipython kernelspec install-self --user
.....
Installed kernelspec python2 in /home/mickeyfromd/.local/share/jupyter/kernels/python2
(tensorflow) mickeyfromd#DK-DESKTOP:~$
I cannot move the the folder (in the second step)
How can I make ipython to have path for Matplotlib?
That import error is due to change in environment of the jupyter notebook. You might have installed the packages in one environment and you are running the jupyter notebook in another environment.
I have got two environments (envs) in my Anaconda folder ( I have Anaconda3 folder to be specific ).
(windows key+cmd ) -> open the windows command prompt run as administrator.
Activate (name of the environment) -> eg.: activate tensorflow-gpu
Start installing packages using conda install
Note: For each environment you need to install all the packages you want to use, separately using the same process. This solution is for windows users, might work for linux users not sure though.
Additionally to make sure your conda environment is up to date run:
conda update conda
conda update anaconda
check this out : https://pradyumnamajumder.wordpress.com/2017/09/30/solution-to-the-python-packages-import-error-in-jupyter/