tensorflow is not working in jupyter notebook - python

I am a beginner in Anaconda. I am using jupyter notebook. Tensorflow is working on anaconda prompt but not opening in jupyter notebook. how to resolve this problem. Please help me out. I am a beginner.
enter image description here

You created an environment with tensorflow but you didn't install jupyter-notebook in it. You need to install the notebook in the same environment where you installed tensorflow:
On Windows from the anaconda command line you need to activate the environment where you installed tensoflow (in your case from the picture you posted it seems to be called tensorflow). Then install it there. Execute the following commands in the anaconda command line to achieve this and then start the notebook in tensorflow's environment
(base) C:\Users\usr>activate tensorflow
(tensorflow) C:\Users\usr>conda install jupyter
(tensorflow) C:\Users\usr>jupyter-notebook
This should solve your problem.

The python kernel in Jupyter notebook points to the root kernel. If you need a specific environment to be displayed in your Jupyter notebook, do the following
# Creating a custom environment in anaconda prompt with python 3.6
(base)C:\>conda create -n MyEnvironment python=3.6
(base)C:\>activate MyEnvironment
# Install your custom pacakges like tensorflow etc
(MyEnvironment)C:\>pip install tensorflow
(MyEnvironment)C:\>python -m ipykernel install --name MyEnvironment
(MyEnvironment)C:\>jupyter notebook
Now you should be able to see both root kernal and MyEnvironment kernel version in Jupyter notebook

Related

Problems in installing jupyter notebook

I have installed jupyter notebook with pip through python and anaconda multiple times but I cannot find it in my start menu or any other location in my computer.
I have also tried using pip download and then pip install. It is successfull i cmd but I still cannot find it in my gui.
I think the best way to go about using Jupyter notebooks is to first create a conda environment that has jupyter installed. You can do this with the following line of code,
conda create -n my_env python jupyter
Optionally, you can even specify a python version as follows,
conda create -n my_env python=3.7 jupyter
Now, activate your environment,
conda activate my_env
Finally, launch Jupyter notebooks,
jupyter notebook
If you already have an environment created, just activate it, install jupyter and launch Jupyter notebooks,
conda activate my_env
pip install jupyter
jupyter notebook

Can't install arcpy in Jupyter Notebook

I have ArcGIS Pro installed, which includes the installed Python packages. I am trying to learn Python and would like to use the ArcPy functions. I have never used Jupyter Notebook before, but to get started, I created a new Python 3 file. I entered the code to install arcpy but received an error.
pip install arcpy
ERROR: Could not find a version that satisfies the requirement arcpy (from versions: none)
ERROR: No matching distribution found for arcpy
import arcpy
ModuleNotFoundError: No module named 'arcpy'
I know I am missing a link somewhere, and I am guessing it has to do with environments. How can I reference the arcpy packages that are installed in my ArcGIS Pro program files?
Using "Non-ArcGIS Pro" Conda Environment
I assume that you use an Anaconda or Miniconda installation not provided by ArcGIS Pro, otherwise arcpy should already be available and there is no need to install it.
Note: If you want to use ArcGIS Pro's conda environment, scroll down to Using ArcGIS Pro's Conda Enviroment or Using Jupyter Notebook from within ArcGIS Pro.
To install arcpy, you need to use conda instead of pip. As far as I know arcpy is not available in the Python Package Index (pip's repository).
Initial Setup
Open Anaconda Prompt (or any command prompt if conda can be found through the PATH variable).
First you need a conda environment. Use the following command to create an environment called esri-notebook (can be any name):
conda create -n esri-notebook
Then activate the environment using:
conda activate esri-notebook
Install Jupyter Lab (or Notebook) with:
conda install jupyterlab
conda update --all
Install arcpy with:
conda install arcpy -c esri
Run Jupyter Lab with:
jupyter lab
Or, run Jupyter Notebook with:
jupyter notebook
Your browser will open with Jupyter Lab or Notebook.
Using Jupyter Lab/Notebook
Open Anaconda Prompt (or any command prompt if conda can be found through the PATH variable). Activate the environment:
conda activate esri-notebook
Run Jupyter Lab with:
jupyter lab
Or, run Jupyter Notebook with:
jupyter notebook
Your browser will open with Jupyter Lab or Notebook.
Using ArcGIS Pro's Conda Enviroment
Open Python Command Prompt. arcpy is available (no setup needed).
Then start Jupyter Notebook using:
jupyter notebook
Using Jupyter Notebook from within ArcGIS Pro
You can also create a notebook from within ArcGIS Pro and arcpy should be available to use it without any further setup. Find the Python button within the Analysis ribbon:

WSL2 jupyter notebook cannot use kernel install in conda environment

I have anaconda with jupyter notebook installed in my WSL2
however, when I'm start the jupyter notebook, It not use my conda environment but using from window
In the "Change kernel" menu only Python3 is available
(env) user#DESKTOP:/mnt/c/Users/user$ ipython kernelspec list
Available kernels:
python3 /home/user/.local/share/jupyter/kernels/python3
pytorch /home/user/.local/share/jupyter/kernels/pytorch
rlml /home/user/.local/share/jupyter/kernels/rlml
I already tried
python -m ipykernel install --user --name env --display-name "name"
but still doesn't work.
Is there anyway to force jupyter notebook to use kernel in WSL ?
I also tried to use from VS code itself but I face another problem.
When try to change jupyter kernel to my conda environment it prompt dialog
but after click install, it can't find
-bash: C:/Users/user/anaconda3/envs/env/python.exe: No such file or directory
I have checked the directory and it have python.exe in there.

Load Keras library in Jupiter notebook using virtual environment

I installed TensorFlow and Keras using virtual environment as explained here and here.
Then inside the virtual environment I installed Jupyter notebook and executed this command to create a new kernel:
ipython kernel install --name "jupyter3_Python_3" --user
When I run jupyter-notebook from the console and change the kernel to jupyter3_Python_3 in Jupyter notebook, I get the following error:
It looks like keras in not installed.
However, when I run python from the virtual environment and do import keras, then everything works.
I assume that I incorrectly launch Jupyter notebook, therefore it does not get access to the virtual environment. But I followed all instructions. Any idea?
Try to first enter your virtual environment from the console:
source /var/venv/virtual_environment/bin/activate # or whatever your path is
and then run jupyter notebook

Trouble with TensorFlow in Jupyter Notebook

I installed Jupyter notebooks in Ubuntu 14.04 via Anaconda earlier, and just now I installed TensorFlow. I would like TensorFlow to work regardless of whether I am working in a notebook or simply scripting. In my attempt to achieve this, I ended up installing TensorFlow twice, once using Anaconda, and once using pip. The Anaconda install works, but I need to preface any call to python with "source activate tensorflow". And the pip install works nicely, if start python the standard way (in the terminal) then tensorflow loads just fine.
My question is: how can I also have it work in the Jupyter notebooks?
This leads me to a more general question: it seems that my python kernel in Jupyter/Anaconda is separate from the python kernel (or environment? not sure about the terminology here) used system wide. It would be nice if these coincided, so that if I install a new python library, it becomes accessible to all the varied ways I have of running python.
Update
TensorFlow website supports five installations.
To my understanding, using Pip installation directly would be fine to import TensorFlow in Jupyter Notebook (as long as Jupyter Notebook was installed and there were no other issues) b/z it didn't create any virtual environments.
Using virtualenv install and conda install would need to install jupyter into the newly created TensorFlow environment to allow TensorFlow to work in Jupyter Notebook (see the following original post section for more details).
I believe docker install may require some port setup in the VirtualBox to make TensorFlow work in Jupyter Notebook (see this post).
For installing from sources, it also depends on which environment the source code is built and installed into. If it's installed into a freshly created virtual environment or an virtual environment which didn't have Jupyter Notebook installed, it would also need to install Jupyter Notebook into the virtual environment to use Tensorflow in Jupyter Notebook.
Original Post
To use tensorflow in Ipython and/or Jupyter(Ipython) Notebook, you'll need to install Ipython and Jupyter (after installing tensorflow) under the tensorflow activated environment.
Before install Ipython and Jupyter under tensorflow environment, if you do the following commands in terminal:
username$ source activate tensorflow
(tensorflow)username$ which ipython
(tensorflow)username$ /Users/username/anaconda/bin/ipython
(tensorflow)username$ which jupyter
(tensorflow)username$ /Users/username/anaconda/bin/jupyter
(tensorflow)username$ which python
(tensorflow)username$ /User/username//anaconda/envs/tensorflow/bin/python
This is telling you that when you open python from terminal, it is using the one installed in the "environments" where tensorflow is installed. Therefore you can actually import tensorflow successfully. However, if you are trying to run ipython and/or jupyter notebook, these are not installed under the "environments" equipped with tensorflow, hence it has to go back to use the regular environment which has no tensorflow module, hence you get an import error.
You can verify this by listing out the items under envs/tensorflow/bin directory:
(tensorflow) username$ ls /User/username/anaconda/envs/tensorflow/bin/
You will see that there are no "ipython" and/or "jupyer" listing out.
To use tensorflow with Ipython and/or Jupyter notebook, simply install them into the tensorflow environment:
(tensorflow) username$ conda install ipython
(tensorflow) username$ pip install jupyter #(use pip3 for python3)
After installing them, there should be a "jupyer" and a "ipython" show up in the envs/tensorflow/bin/ directory.
Notes:
Before trying to import tensorflow module in jupyter notebook, try close the notebook. And "source deactivate tensorflow" first, and then reactivate it ("source activate tensorflow") to make sure things are "on the same page". Then reopen the notebook and try import tensorflow. It should be import successfully (worked on mine at least).
i used these following which in virtualenv.
pip3 install --ignore-installed ipython
pip3 install --ignore-installed jupyter
This re-installs both ipython and jupyter notebook in my tensorflow virtual environment. You can verify it after installation by which ipython and which jupyter. The bin will be under the virtual env.
NOTE I am using python 3.*
I have another solution that you don't need to source activate tensorflow before using jupyter notebook every time.
Partion 1
Firstly, you should ensure you have installed jupyter in your virtualenv. If you have installed, you can skip this section (Use which jupyter to check). If you not, you could run source activate tensorflow, and then install jupyter in your virtualenv by conda install jupyter. (You can use pip too.)
Partion 2
1.From within your virtualenv, run
username$ source activate tensorflow
(tensorflow)username$ ipython kernelspec install-self --user
This will create a kernelspec for your virtualenv and tell you where it is:
(tensorflow)username$ [InstallNativeKernelSpec] Installed kernelspec pythonX in /home/username/.local/share/jupyter/kernels/pythonX
Where pythonX will match the version of Python in your virtualenv.
2.Copy the new kernelspec somewhere useful. Choose a kernel_name for your new kernel that is not python2 or python3 or one you've used before and then:
(tensorflow)username$ mkdir -p ~/.ipython/kernels
(tensorflow)username$ mv ~/.local/share/jupyter/kernels/pythonX ~/.ipython/kernels/<kernel_name>
3.If you want to change the name of the kernel that IPython shows you, you need to edit ~/.ipython/kernels/<kernel_name>/kernel.json and change the JSON key called display_name to be a name that you like.
4.You should now be able to see your kernel in the IPython notebook menu: Kernel -> Change kernel and be able so switch to it (you may need to refresh the page before it appears in the list). IPython will remember which kernel to use for that notebook from then on.
Reference.
Here is what I did to enable tensorflow in Anaconda -> Jupyter.
Install Tensorflow using the instructions provided at
Go to /Users/username/anaconda/env and ensure Tensorflow is installed
Open the Anaconda navigator and go to "Environments" (located in the left navigation)
Select "All" in teh first drop down and search for Tensorflow
If its not enabled, enabled it in the checkbox and confirm the process that follows.
Now open a new Jupyter notebook and tensorflow should work
Your Anaconda install probably went to different directory than your Python install
For instance on my machine I can find location here
yaroslavvb-macbookpro:~ yaroslavvb$ which ipython
/Users/yaroslavvb/anaconda/bin/ipython
When you type python, it tries to find it in PATH going in left-to-right order. So you may have another version of python in a folder before Anaconda folder, and it'll use that. To fix, you can do export PATH=.... to change the path, and put Anaconda directory in front, so that it takes python from there instead of the default, ie
export PATH=/Users/yaroslavvb/anaconda/bin:$PATH
I installed PIP with Conda conda install pip instead of apt-get install python-pip python-dev.
Then installed tensorflow
use Pip Installation:
# Ubuntu/Linux 64-bit, CPU only, Python 2.7
$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.9.0-cp27-none-linux_x86_64.whl
# Ubuntu/Linux 64-bit, GPU enabled, Python 2.7
# Requires CUDA toolkit 7.5 and CuDNN v4. For other versions, see "Install from sources" below.
$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.9.0-cp27-none-linux_x86_64.whl
...
pip install --upgrade $TF_BINARY_URL
Then it will work in jupyter notebook.
The accepted answer (by Zhongyu Kuang) has just helped me out. Here I've create an environment.yml file that enables me to make this conda / tensorflow installation process repeatable.
Step 1 - create a Conda environment.yml File
environment.yml looks like this:
name: hello-tensorflow
dependencies:
- python=3.6
- jupyter
- ipython
- pip:
- https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-1.1.0-cp36-cp36m-linux_x86_64.whl
Note:
Simply replace the name to whatever you want. (mine is hello-tensorflow)
Simply replace the python version to whatever you want. (mine is 3.6)
Simply replace the tensorflow pip install URL to whatever you want (mine is the Tensorflow URL where Python 3.6 with GPU support)
Step 2 - create the Conda environment
With the environment.yml being in the current path you are on, this command creates the environment hello-tensorflow (or whatever you have renamed it to):
conda env create -f environment.yml
Step 3: source activate
Activate the newly created environment:
source activate hello-tensorflow
Step 4 - which python / jupyter / ipython
which python...
(hello-tensorflow) $ which python
/home/johnny/anaconda3/envs/hello-tensorflow/bin/python
which jupyter...
(hello-tensorflow) $ which jupyter
/home/johnny/anaconda3/envs/hello-tensorflow/bin/jupyter
which ipython...
(hello-tensorflow) $ which ipython
/home/johnny/anaconda3/envs/hello-tensorflow/bin/ipython
Step 5
You should now be able to import tensorflow from python, jupyter (console / qtconsole / notebook, etc.) and ipython.
I think your question is very similar with the question post here. Windows 7 jupyter notebook executing tensorflow. As Yaroslav mentioned, you can try
conda install -c http://conda.anaconda.org/jjhelmus tensorflow .
I had a similar issue when using a custom Ubuntu 16 image. The problem was related to an existing version of numpy that was already installed on my system.
I initially tried
sudo pip3 install tensorflow
This resulted in the following exception:
Exception:
Traceback (most recent call last):
File "/anaconda/envs/py35/lib/python3.5/shutil.py", line 538, in move
os.rename(src, real_dst)
PermissionError: [Errno 13] Permission denied: '/anaconda/envs/py35/lib/python3.5/site-packages/numpy' -> '/tmp/pip-co73r3hm-uninstall/anaconda/envs/py35/lib/python3.5/site-packages/numpy'
The docs advise that if you encounter any issues with this command to try the following:
sudo pip3 install --upgrade \
https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.0.1-cp35-cp35m-linux_x86_64.whl
However, my system was unable to locate pip3
sudo: pip3 command not found
The ulitmate solution was to create a symlink for pip3
sudo ln -s /anaconda/envs/py35/bin/pip3.5 /usr/local/bin/pip3
Finally, the following command worked without trouble
sudo /usr/local/bin/pip3 install --upgrade \
https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.0.1-cp35-cp35m-linux_x86_64.whl
I verified the installation in the terminal and also verified a successful import in my Jupyter Notebook
import tensorflow as tf
I wonder if it is not enough to simply launch ipython from tensorflow environnement. That is
1) first activate tensorflow virtualenv with:
source ~/tensorflow/bin/activate
2) launch ipython under tensorflow environnement
(tensorflow)$ ipython notebook --ip=xxx.xxx.xxx.xxx
I found the solution from someone else's post. It is simple and works well!
http://help.pythonanywhere.com/pages/IPythonNotebookVirtualenvs
Just install the following in the Command Prompt and change kernel to Python 3 in Jupyter Notebook. It will import tensorflow successfully.
pip install tornado==4.5.3
pip install ipykernel==4.8.2
(Orginial post: https://github.com/tensorflow/tensorflow/issues/11851)
Jupyter Lab: ModuleNotFound tensorflow
For a future version of me or a colleague that runs into this issue:
conda install -c conda-forge jupyter jupyterlab keras tensorflow
Turns out jupyterlab is a plugin for jupyter.
So even if you are in an environment that has jupyter but not jupyterlab as well, if you try to run:
jupyter lab
then jupyter will look in the (base) environment for the jupyterlab plugin.
Then your imports in jupyter lab will be relative to that plugin and not your conda environment.
pip install tensorflow
This worked for me in my conda virtual environment.
I was trying to use conda install tensorflow in a conda virtual environment where jupyter notebooks was already installed, resulting in many conflicts and failure. But pip install worked fine.
conda info --envs
conda create --name py3-TF2.0 python=3
Proceed ([y]/n)? y
conda activate py3-TF2.0
conda install tensorflow
pip install --upgrade tensorflow
conda install -c anaconda ipykernel
python -m ipykernel install --user --name=py3-TF2.0
it is the best way but if it is required you should upgrade numpy with scipy as well
You can try "conda install tensorflow". This will install TensorFlow in your Anaconda directory.
Your local pip directory might not be shared with the Anaconda directory.
Thanks!
Open an Anaconda Prompt screen: (base) C:\Users\YOU>conda create -n tf tensorflow
After the environment is created type: conda activate tf
Prompt moves to (tf) environment, that is: (tf) C:\Users\YOU>
then install Jupyter Notebook in this (tf) environment:
conda install -c conda-forge jupyterlab - jupyter notebook
Still in (tf) environment, that is type
(tf) C:\Users\YOU>jupyter notebook
The notebook screen starts!!
A New notebook then can import tensorflow
FROM THEN ON
To open a session
click Anaconda prompt,
type conda activate tf
the prompt moves to tf environment
(tf) C:\Users\YOU>
then type (tf) C:\Users\YOU>jupyter notebook

Categories