No module named tensorflow in jupyter - python

I have some imports in my jupyter notebook and among them is tensorflow:
ImportError Traceback (most recent call last)
<ipython-input-2-482704985f85> in <module>()
4 import numpy as np
5 import six.moves.copyreg as copyreg
----> 6 import tensorflow as tf
7 from six.moves import cPickle as pickle
8 from six.moves import range
ImportError: No module named tensorflow
I have it on my computer, in a special enviroment and all connected stuff also:
Requirement already satisfied (use --upgrade to upgrade): tensorflow in /Users/mac/anaconda/envs/tensorflow/lib/python2.7/site-packages
Requirement already satisfied (use --upgrade to upgrade): six>=1.10.0 in /Users/mac/anaconda/envs/tensorflow/lib/python2.7/site-packages (from tensorflow)
Requirement already satisfied (use --upgrade to upgrade): protobuf==3.0.0b2 in /Users/mac/anaconda/envs/tensorflow/lib/python2.7/site-packages (from tensorflow)
Requirement already satisfied (use --upgrade to upgrade): numpy>=1.10.1 in /Users/mac/anaconda/envs/tensorflow/lib/python2.7/site-packages (from tensorflow)
Requirement already satisfied (use --upgrade to upgrade): wheel in /Users/mac/anaconda/envs/tensorflow/lib/python2.7/site-packages (from tensorflow)
Requirement already satisfied (use --upgrade to upgrade): setuptools in ./setuptools-23.0.0-py2.7.egg (from protobuf==3.0.0b2->tensorflow)
I can import tensorflow on my computer:
>>> import tensorflow as tf
>>>
So I'm confused why this is another situation in notebook?

If you installed a TensorFlow as it said in official documentation: https://www.tensorflow.org/versions/r0.10/get_started/os_setup.html#overview
I mean creating an environment called tensorflow and tested your installation in python, but TensorFlow can not be imported in jupyter, you have to install jupyter in your tensorflow environment too:
conda install jupyter notebook
After that I run a jupyter and it can import TensorFlow too:
jupyter notebook

Jupyter runs under the conda environment where as your tensorflow install lives outside conda. In order to install tensorflow under the conda virtual environment run the following command in your terminal:
conda install -c conda-forge tensorflow

I had the same problem, and solved it by looking at the output of:
jupyter kernelspec list
which outputs the kernel information:
python2 /Users/Username/Library/Jupyter/kernels/python2
python3 /Users/Username/Library/Jupyter/kernels/python3
Notice that the path points to the Jupyter kernel for the user. To use it within the the Anaconda environment, it needs to point to the conda env you are using, and look something like Anaconda3\envs\Env_Name\share\jupyter\kernels\python3.
So, to remove the Jupyter kernelspec, just use:
jupyter kernelspec remove python3
or jupyter kernelspec remove python2 if you're using python 2
Now, the output of jupyter kernelspec list should point to the correct kernel.
See https://github.com/jupyter/notebook/issues/397 for more information about this.

Conda environment fetches the tensorflow package from the main system site-packages.
Step 1: Just deactivate conda environment
conda deactivate
pip install tensorflow
Step 2: Switch back to conda environment
conda activate YOUR_ENV_NAME
jupyter notebook
Step 3: Run the cell with import tensorflow you should be able to import.
Thanks

I also had the same problem for a long time. I wanted to import tensorflow inside the jupyter notebook within windows 10. I followed all the instructions and commands that were suggested and it was not working from the command prompt. Finally, I tried this command with the Anaconda Prompt and it worked successfully. If you are using jupyter notebook within Anaconda then go goto the windows search terminal and type "Anaconda Prompt" and inside it type following command, It will install the tensorflow inside the jupyter notebook.
conda install -c conda-forge tensorflow

the problem may when the Jupyter notebook may launching from the default but for able to import tensorflow and keras libraries so you have to install jupyter notebook like what you have installed the libraries
pip install jupyter

Run python -m ipykernel install --user --name <Environment_Name>. This should add your environment to the jupyter kernel list.
Change the kernel using Kernel->Change Kernel option or New-><Environment_Name>.
Note : Replace <Environment_Name> with the actual name of the environment.

run this command which will install tensorflow inside conda
conda install -c conda-forge tensorflow

This is what I did to fix this issue -
I installed tensorflow for windows by using below link -
https://www.tensorflow.org/install/install_windows
Once done - I activated tensorflow by using below command -
C:> activate tensorflow
(tensorflow)C:> # Your prompt should change
Once done I ran below command -
(tensorflow)C:> conda install notebook
Fetching package metadata ...........
Solving package specifications: .
Package plan for installation in environment
The following NEW packages will be INSTALLED:
bleach: 1.5.0-py35_0
colorama: 0.3.9-py35_0
decorator: 4.1.2-py35_0
entrypoints: 0.2.3-py35_0
html5lib: 0.9999999-py35_0
ipykernel: 4.6.1-py35_0
----
---
jupyter_client 100% |###############################| Time: 0:00:00 6.77 MB/s
nbformat-4.4.0 100% |###############################| Time: 0:00:00 8.10 MB/s
ipykernel-4.6. 100% |###############################| Time: 0:00:00 9.54 MB/s
nbconvert-5.2. 100% |###############################| Time: 0:00:00 9.59 MB/s
notebook-5.0.0 100% |###############################| Time: 0:00:00 8.24 MB/s
Once done I ran command
(tensorflow)C:>jupyter notebook
It opened new Juypter window and able to Run fine -
import tensorflow as tf

I was able to load tensorflow in Jupyter notebook on Windows by: first do conda create tensorflow install, then activate tensorflow at the command prompt , then execute "Jupyter notebook" from command line.
Tensorflow imports at the notebook with no error. However, I was unable to import "Pandas" &"Matplotlib, ....etc"

As suggested by #Jörg, if you have more than one kernel spec. You have to see the path it points to. In my case, it is actually the path that was to be corrected.
When I created TensorFlow virtual env, the spec had the entry for python which was pointing to base env. Thus by changing W:\\miniconda\\python.exe to W:\\miniconda\\envs\\tensorflow\\python.exe solved the problem.
So it is worth looking at your kernel spec. Delete that is not needed and keep those you want. Then look inside the JSON files where the path is given and change if needs be. I hope it helps.

There are two ways to fix this issue.
The foremost way is to create a new virtual environment and install all dependencies like jupyter notebook, tensorflow etc.
conda install jupyter notebook
conda install -c conda-forge tensorflow
The other way around is to install tensorflow in the current environment (base or any activated environment).
conda install -c conda-forge tensorflow
Note: It is advisable to create a new virtual environment for every new project. The details how to create and manage virtual environment using conda can be find here:
https://conda.io/docs/user-guide/tasks/manage-environments.html

Probably there is a problem with the TensorFlow in your environment.
In my case, After installing some libs, my TensorFlow stopped working.
So I installed TensorFlow again using pip. like so:
just run
pip install tensorflow
then I re-imported it into my jupyter notebook as :
import tensorflow as ft
In case you want to install jupyter and base libs try this:
pip install jupyter tensorflow keras numpy scipy ipython pandas matplotlib sympy nose

Other supported libraries are necessary to install with TensorFlow.Make sure if these libraries are installed:
numpy
scipy
jupyter
matplolib
pillow
scikit-learn
tensorflow-addons,
tensorflow.contrib
This worked for me. I followed this: https://www.pythonpool.com/no-module-named-tensorflow-error-solved/

TensorFlow package doesn't come by default with the root environment in Jupyter, to install it do the following :
Close Jupyter Notebook.
Open Anaconda Navigator (In windows : you can find it using the search bar)
On the sidebar, click on the Environments tab (by default you are using the root env).
You can see the installed packages, on the top switch to not-installed packages and search for tensorflow, if it doesn't show, click on Update index and it will be displayed.
The installation takes some time

If you have installed TensorFlow globally then this issue should not be occurring. As you are saying you have installed it, maybe you did it in a virtual environment.
Some background:
By default, Jupyter will open with a global python interpreter kernel.
Possible solutions:
Change your jupyter notebook kernel to your virtual environment kernel. Please check here to see how to create a kernel out of your virtual environment.
Troubleshooting:
If the above solution dint work lets do some troubleshooting. When you add your new kernel to jupyter you might have got output like below
Installed kernelspec thesis-venv in C:\Users\vishnunaik\AppData\Roaming\jupyter\kernels\venv
Check the file kernel.json in this path, which might look something like below
{
"argv": [
"C:\\Users\\vishnunaik\\Desktop\\Demo\\CodeBase\\venv\\Scripts\\python.exe",
"-m",
"ipykernel_launcher",
"-f",
"{connection_file}"
],
"display_name": "thesis-venv",
"language": "python",
"metadata": {
"debugger": true
}
}
Check the path to the python.exe is rightly pointing to your virtual environment python version or not. If not then update it accordingly.
Now you should be able to use a virtual environment in your jupyter notebook. If your kernel takes a lot of time to respond see jupyter notebook server logs, sometimes you might get output like this
[I 21:58:38.444 NotebookApp] Kernel started: adbd5551-cca3-4dad-a93f-974d7d25d53b, name: thesis-venv C:\\Users\\vishnunaik\\Desktop\\Demo\\CodeBase\\venv\\Scripts\\python.exe: No module named ipykernel_launcher
This means your virtual environment doesnot have ipykernel installed. So install it in your virtual environment using below command.
pip install ipykernel
Now you have done everything possible, so I hope this will solve your issue.

Related

setting up vitrual environment for deep learning on ubuntu

I am trying to setup virtual environment for every deep learning project that i do locally on my laptop so that every package like numpy, matplotlib, opencv-python etc. i installed will be installed in that environment only not on my laptop globally. Now, the problem is i activate the virtual environment using source ENV_NAME/bin/activate and when i import libraries like cv2 in my notebook after installing cv2 in that environment setup, it says "
ModuleNotFoundError: No module named 'cv2'" please see the screenshots below. but when i deactivate that environment and install cv2 on my laptop globally and then try to import cv2, it works fine. Could someone please tell my why?
You need to restart the kernel in Jupyter Notebook for the new modules to show up.
Not sure if you already found the solution, but I stumbled upon this blog and it answers most questions of the type: I installed package X and now I can't import it in the notebook. Help!
You can install the package directly in the jupyter notebook using:
# Install a pip package in the current Jupyter kernel
import sys
!{sys.executable} -m pip install opencv-python
if using pip.
OR
# Install a conda package in the current Jupyter kernel
import sys
!conda install --yes --prefix {sys.prefix} opencv-python
if using conda.
Head over to link for more details.

How to use lower version of keras and tensorflow

I'm running a code which requires keras version 1.2.0 and tensorflow version 1.1.0.
I'm using Jupyter notebook and I created an environment for all the dependencies.
However, I mistakenly installed both libraries again through pip command which installed the latest versions.
I closed the notebook, opened it again and created the environment once again so the older version of both libraries were installed again.
But when I run keras.__version___ command, it shows 2.4.3, which i do not want.
I also ran conda remove keras --force and pip uninstall keras, but it's still showing the latest version.
The code is only compatible with the older version. Please help.
It's probably because it is getting uninstalled on a different environment. Identify which python and pip executable you are using by running the following commands:
$ which pip
$ which python
These two commands will give out the path of the executable from which we can determine the environment. If it is different from what you were using you can try installing to the desired environment by running:
/path/to/desired/pip uninstall keras
/path/to/desired/pip install keras==1.2.0

Can't use Anaconda properly due to package installation error

I'm using Python 3.8 with pyCharm generally but for some Machine Learning tutorial I wanted to use Anaconda Spyder 4.1.2. So when I write in default windows cmd
C:\Users\Ege>python --version
Python 3.8.2
when I write in anaconda prompt:
(base) C:\Users\Ege>python --version
Python 3.7.7
This is the version of my base(root) environment. I searched and found Anaconda supports 3.8 for environments but not for base yet. Therefore I cannot upgrade it to 3.8. Now the problem comes:
When I try to use numpy in Spyder it says there is no module imported called numpy. I went and check my anaconda base environment and yes there is no numpy package. I opened the conda prompt and I wrote
pip3 install numpy
It says:
Requirement already satisfied: numpy in c:\python38\lib\site-packages (1.18.1)
But goddamit you don't allow me to use python38.
Some of you can say okay go make a new environment with python38 and use the packages. However I want to install the packages to base environment. Any ideas ?
Instead of usin pip one should use conda install. the names are not always equal to each other which means sometimes pip install thisPackage is not equal to conda install thisPackage therefore one should search before installing that package to their environment.

Matplotlib in Tensorflow Jupyter Notebook

In Anaconda Navigator, I switched to running applications on tensorflow and installed jupyter 5.6.0. I then open up a Python3 notebook. I then import tensorflow, keras and numpy without issue. Then when I try to import matplotlib, the notebook says ImportError: No module named 'matplotlib'.
I tried running the following command in my anaconda prompt in both base and after activating tensorflow: pip3 install matplotlib
And it says:
(tensorflow) C:\Users\danie>pip3 install matplotlib
Requirement already satisfied
for 7 different lines. What am I doing wrong?
Add import sys and sys.executable to the top of your notebook, then run it. This shows you the directory of the running kernel. With this information, from a new command-line (not a Python console) run C:\path\to\python.exe -m pip install matplotlib
! pip install matplotlib ,worked perfect for me in the tensorflow environment (Jupyter)

How do you install Tensorflow on Windows?

I am trying to install Tensorflow on Windows.
I have Anaconda 4.2.0. I tried running
conda create -n tensorflow python=3.5
in my command prompt. This seemed to do something, but I'm not sure what this accomplished. It created a folder within the Anaconda3 program in my username folder.
This folder is filled with the following content:
Over the summer, I used mainly Jupyter Notebooks to do my python coding. Within this environment, there is a tab marked Condas
So it looks like I should be able to switch to the Tensorflow environment. But this doesn't work when I try to switch, there is no option to change my kernel to a Tensorflow one.
I tried running
conda search tensorflow
But nothing appears.
I'm not sure what to do. I asked a few grad students in my economics research group, but they weren't sure what to do either.
My Question
How do I properly install Tensorflow on Windows?
The syntax of the command is conda create -n <name_of_new_env> <packages>. As a result, you created a clean environment named tensorflow with only Python 3.5 installed. Since conda search tensorflow returned nothing, you will have to use pip or some other method of installing the package. Since there is spotty official support for Windows, the conda-forge package (CPU only) at https://github.com/conda-forge/tensorflow-feedstock is probably the best way.
People have also reported success installing Tensorflow with docker, if you have docker set up already.
I was able to run it under the Windows 10 linux subsystem (https://msdn.microsoft.com/en-us/commandline/wsl/install_guide)
Which is basically a linux environment within windows.
The latest tensorflow version (0.12) added windows support
https://www.tensorflow.org/get_started/os_setup#pip_installation_on_windows
just run:
pip install --upgrade https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow-0.12.1-cp35-cp35m-win_amd64.whl
(the url is for the specific version - you will need to change it for future versions or other setups)
An Anaconda environment isolates itself completely with the outer world, so all the packages you installed outside the virtualenv is nothing in the virtualenv, if you want to use Tensorflow in the environment(seems like the only way with Anaconda), use activate tensorflow command and install the packages you want seperately.
pip provides an easy method to install tensorflow on windows machine.
use the following pip command
pip install --upgrade https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow-0.12.0rc0-cp35-cp35m-win_amd64.whl
Tensorflow only support python3.5 x64 bit on windows machines and it requires that you install Visual C++ 2015 redistributable (x64 version) to be able to import tensorflow

Categories

Resources