The following code does not render in Jupyter lab:
%matplotlib widget
import plotly.express as px
import numpy as np
import pandas as pd
df = pd.DataFrame(np.random.randint(0,100,size=(5, 4)), columns=list('ABCD'))
px.bar(df, x='A', y='B')
I have tried to install all the dependencies and extensions mentioned in here
https://plot.ly/python/getting-started/#jupyterlab-support-python-35
but also the steps in here
https://github.com/matplotlib/jupyter-matplotlib
Nothing worked
Here is my set up:
jupyter lab --version
1.0.2
python --version
Python 3.6.1 :: Continuum Analytics, Inc.
conda list jupyterlab
# packages in environment at C:\Users\***\Anaconda3:
#
# Name Version Build Channel
jupyterlab 1.0.2 py36hf63ae98_0
jupyterlab_launcher 0.13.1 py36_0
jupyterlab_server 1.0.0 py_0
conda list nodejs
# packages in environment at C:\Users\***\Anaconda3:
#
# Name Version Build Channel
nodejs 0.1.1 pypi_0 pypi
conda list plotly
# packages in environment at C:\Users\***\Anaconda3:
#
# Name Version Build Channel
plotly 4.1.0 pypi_0 pypi
plotly-express 0.4.1 pypi_0 pypi
EDIT:
jupyter-labextension list
JupyterLab v1.0.2
Known labextensions:
app dir: C:\Users\***\Anaconda3\share\jupyter\lab
#jupyter-widgets/jupyterlab-manager v1.0.2 enabled ok
#jupyterlab/git v0.8.0 enabled ok
#jupyterlab/plotly-extension v1.0.0 enabled ok
jupyter-matplotlib v0.4.2 enabled ok
jupyterlab-chart-editor v1.2.0 enabled ok
jupyterlab-plotly v1.1.0 enabled ok
plotlywidget v1.1.0 enabled ok
EDIT: these instructions and more are now in our official Troubleshooting Guide!
It could be that remnants of previous installations or attempts at installation are causing issues. I recommend either starting with a clean install or uninstalling all Plotly modules (from both pip and conda!) and plotly-related jlab extensions, and then following the instructions here: https://plot.ly/python/getting-started/
Uninstalling the module is a matter of
conda uninstall plotly
pip uninstall plotly
And then reinstalling with one or the other but not both, according to the instructions linked above.
Uninstalling JupyterLab extensions is performed with
jupyter labextension uninstall #jupyterlab/plotly-extension
jupyter labextension uninstall jupyterlab-plotly
jupyter labextension uninstall plotlywidget
Following the official plotly.py repo https://github.com/plotly/plotly.py, for correct rendering of plotly in JupyterLab there's a need to installing the special extension by command
jupyter labextension install jupyterlab-plotly#4.14.3
I ran into the same problem but with a different cause and requiring a different solution. Just thought I'd share it for anyone encountering the same issue.
I'm running jupyterlab in a Docker container which did not yet have nodejs or npm installed.
I was unable to install the required extension via:
jupyter labextension install jupyterlab-plotly
Because it gave me this error:
ValueError: Please install nodejs and npm before continuing installation. nodejs may be installed using conda or directly from the nodejs website.
Conda was not available on the container and when installing node and npm via the jupyterlab terminal (through pip or apt-get) I got the same error, or a version mismatch (when using apt-get the nodejs version I got was too old).
The following steps helped me solve the problem.
Install nvm in the docker container when building the container, thus in the Dockerfile:
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
Mind the version number, you might want to change that to whatever is the latest stable version
Make the nvm command available by loading some included init scripts:
SHELL ["bash", "-lc"] <-- Only necessary if your container is not using bash as shell already
RUN export NVM_DIR="$HOME/.nvm"
RUN [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
RUN [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
Install a specific nodejs version via nvm:
RUN nvm install 14.17.0
Mind the version number again, change to whatever version you need.
Install the jupyter extension:
RUN jupyter labextension install jupyterlab-plotly
Restart the kernel and happy plotting ;)
You might also consider installing conda and then nodejs via conda if that makes sense for your use case. I have not tested if that works though.
Try installing jupyterlab dash, it worked for me!
you can do it through the jupyterlab menu or by following these instructions.
https://github.com/plotly/jupyter-dash
It looks like you should upgrade your plotly as well, because plotly express is now part of plotly i.e.
import plotly.express as px
For anyone still struggling to make this work – these steps worked for me:
If you have jupyter lab running – shut down your session.
Uninstall plotly and all other related packages like #nicolaskruchten recommends here.
Make a fresh install of plotly and jupyter-dash with EITHER conda or pip, but do not mix between both package managers.
Execute jupyter lab from terminal again. Now you should see a window Build recommended – jupyterlab-dash needs to be included in build. Confirm this.
After the successful build close the jupyter-lab session and initiate again.
Related
I am trying to install the jupyterlab plotly extension with this command (according to https://plotly.com/python/getting-started/):
jupyter labextension install jupyterlab-plotly#4.14.3
I get this error:
An error occured.
ValueError: Please install Node.js and npm before continuing installation. You may be able to install Node.js from your package manager, from conda, or directly from the Node.js website (https://nodejs.org).
See the log file for details: /tmp/jupyterlab-debug-epx8b4n6.log
I didn't install Node.js on system level, but in a virtual environment using pip. Pip list shows both nodejs 0.1.1 and npm 0.1.1 . I am also using ipywidgets in jupyterlab, which requires nodejs and it is working fine.
So I have two questions:
How to use plotlywidgets with pip nodejs in a virtual environment?
What's the difference between pip nodejs and system level Node.js
No, you have not installed node.js. You installed some kind of Python bindings for node (python-nodejs, with its repository archived by the author) which itself require an actual nodejs. It is dangerous to install stuff from PyPI without checking what you are installing. It could have been a malicious code - you shouldn't just type a name after pip install and hope that it installs what you think. It's the same for your npm installation (package comes from the same author); both were not updated in the last 6 years and may contain some vulnerabilities so I would uninstall those quickly ;)
It can be seen immediately from the version number that something is wrong because the current nodejs versions are generally >10, (with exact version depending on your JupyterLab version, i.e. either 10 or 12; 14 might work too).
First install nodejs latest version
conda install nodejs -c conda-forge --repodata-fn=repodata.json
Then install jupyterlab extension:
jupyter labextension install jupyterlab-plotly#4.14.3
Then RESTART JUPYTER LAB
(Using latest miniconda + VS Code on 64-bit Windows10:) After clean reinstalling VS Code, I can no longer launch jupyter notebook from within it. When I tried to create a new jupyter file for the first time, the Python extension installed ipykernel in my virtual environment "da38" (my main working environment). Then it stays on Connecting to IPython kernel: Connecting to kernel for an unusually long time, and stops with the error message Unable to start session for kernel Python 3.8.5 64-bit ('da38':conda) (images pasted below). I also removed and re-created da38 environment just in case. Used jupyter many times before with no issues until this new VS Code install today, and an identical setup is working on my other computer. Any help is appreciated. Thanks!
I had the latest version and below command worked for me
python -m pip install 'traitlets==4.3.3' --force-reinstall
This issue also occurs on my computer. The solution is to restore the version number of a dependency package "traitlets" of ipython kernel to 4.3.3.
You could try to use "conda list" to view the version of the module traitlets in the current conda environment. If it shows version 5.0, it is recommended that you use version 4.3.3.
Reference: Unable to start session for kernel Python.
I had this same problem. What worked for me is updating ipykernel.
$ pip install ipykernel --upgrade
I also did a few other upgrades just in case.
$ pip install traitlets --upgrade
$ pip install notebook --upgrade
I did not role back to an older version like 'traitlets==4.3.3'.
If anyone facing same issues now, Can check below steps which helped me to solve this
Update and Restart VS code
Install latest Jupyter Extension from Extensions.
We need to update "ipykernel" and restart VSC:
pip install ipykernel --upgrade
I've installed Jupyter extension in the latest Visual Studio:
Visual Studio 1.3.01 64
Jupyter 1.1.4
As I am using tensorflow I need Python 3 64bit.
When I try to run simple code I get:
Jupyter kernel cannot be started from 'Python 3.6.8 64-bit ('tensorflow64': virtualenv)'. Using closest match Python 3.7.0 32-bit instead.
Code:
#%%
import tensorflow as tf
session = tf.Session()
hello = tf.constant("Hello from Milan.")
print(session.run(hello))
a = tf.constant(20)
b = tf.constant(22)
print('a + b = {0}'.format(session.run(a + b)))
All works fine if I Run code not using Jupyter from VS Code.
Message from Jupyter is not the best description of the issue, missing ipykernel package.
Fix was to install additional python package 'ipykernel' into virtual environment with Python 3.64 bit.
pip install ipykernel
Additional info:
https://github.com/Microsoft/vscode-python/issues/3579
conda create --name test_env
conda activate test_env
conda install -c anaconda ipykernel
python -m ipykernel install --user --name=test_env
Or this You can run this it might work
ipython kernel install --user --name=ENVNAME
Simple installation is the best. While learning Python et-all I had installed many things in many different ways. It was a mess. Removed everything and did a fresh install only I used pyenv and pipenv. However pipenv does not work with the latest version which you can install using brew. Solution revert back to version 2018.10.13 using pip install. Then I discovered that pipenv was no longer recommended so, I removed it. Built a test project and could not get jupyter to run. Spent all day trying everything.
Solution was, remove the old version of pipenv and install the latest using brew. All is well.
However, I do like the features of pyenv like setting global etc.
You have to select and save the .ipynb file with VSCode indicating a kernel that is reachable from your Jupyter installation:
In my case installing anaconda and call following comments solves it:
conda create -n tf tensorflow
conda activate tf
In Jupyter Notebook, ipywidgets work fine, however they seem to not work in Jupyter Lab (which is supposedly better than Notebook).
I followed these directions.
Step 1: Installed Node.js (https://nodejs.org/en/)
Step 2: Installed requirements on Python 3 with conda:
conda install -c conda-forge ipywidgets
jupyter labextension install #jupyter-widgets/jupyterlab-manager
Unfortunately, basic widgets do not work inside the jupyter lab notebook:
JupyterLab now prefers a model where arbitrary javascript is no longer allowed to be embedded in a cell's output, which is how many interactive Jupyter Notebook modules used to work. They now ask that modules with interactivity create a JupyterLab extension.
ipywidgets provides #jupyter-widgets/jupyterlab-manager extension which satisfies this requirement.
When using ipywidgets 7.6 or newer in JupyterLab 3.0 or newer you do not need to do anything: it is installed by default. You can check if this is installed by running:
jupyter labextension list
which should include a line like:
#jupyter-widgets/jupyterlab-manager v3.0.0 enabled OK (python, jupyterlab_widgets)
If you are using JupyterLab 1 or 2 (or old version of ipywidgets) you need to install this extension manually by running this on your command line (which assumes you already have NodeJS installed):
jupyter labextension install #jupyter-widgets/jupyterlab-manager
I had the same pbm, and tried this solution (hope it can help others):
The jupyter labextension install #jupyter-widgets/jupyterlab-manager gave this kind of error in my case:
> /Users/user/.nvm/versions/node/v8.7.0/bin/npm pack #jupyter-widgets/jupyterlab-manager
jupyter-widgets-jupyterlab-manager-0.35.0.tgz
Errored, use --debug for full output:
ValueError:
"#jupyter-widgets/jupyterlab-manager#0.35.0" is not compatible with the current JupyterLab
Conflicting Dependencies:
JupyterLab Extension Package
>=0.15.4-0 <0.16.0-0 >=0.16.0-0 <0.17.0-0 #jupyterlab/application
>=1.1.4-0 <2.0.0-0 >=2.0.0-0 <3.0.0-0 #jupyterlab/services
>=0.15.4-0 <0.16.0-0 >=0.16.0-0 <0.17.0-0 #jupyterlab/rendermime
>=0.15.4-0 <0.16.0-0 >=0.16.0-0 <0.17.0-0 #jupyterlab/notebook
Then, what I did is to use a previous version 0.34 instead of 0.35:
jupyter labextension install #jupyter-widgets/jupyterlab-manager#0.34
In fact, according to this, sometime teams get time to consider the last version.
UP (according to comments):
You can check jupyter lab --version and find match on its version compatibility.
And it works now !
Had the same issue, and what worked for me today was running the 'clean' command, as mentioned here: https://ipywidgets.readthedocs.io/en/latest/user_install.html#installing-the-jupyterlab-extension
So:
jupyter lab clean
jupyter labextension install #jupyter-widgets/jupyterlab-manager
And that got it working right for me just now.
Note: most of the above answers are outdated (as of july 19 2021). It should be a lot more seamless with the latest versions of these packages. However, in 2021 I was having an issue rendering panel widgets in jupyterlab hosted on jupyterhub and came across this post. I tried a few of these answers at first but none of them worked. After digging into some of the libraries, I found the following:
jupyter labextension install #jupyter-widgets/jupyterlab-manager is no longer required as of ipywidgets==7.6 and jupyterlab>=3.0. source. If you're on the latest version of jupyterlab, you should just need to install ipywidgets and the extension will be enabled automatically as long as widget extension authors also follow some steps.
The main change in this release is that installing ipywidgets 7.6.0 will now automatically enable ipywidgets support in JupyterLab 3.0—a user has no extra JupyterLab installation step and no rebuild of JupyterLab, nor do they need Node.js installed. Simply install the python ipywidgets package with pip (pip install ipywidgets==7.6.0) or conda/mamba (conda install -c conda-forge ipywidgets=7.6.0) and ipywidgets will automatically work in classic Jupyter Notebook and in JupyterLab 3.0.
panel has also followed suit and bundled the necessary extensions in the pyviz_comms package, so theoretically everything should have been included for my case. source
In the classic Jupyter notebook environment and JupyterLab, first make sure to load the pn.extension(). Panel objects will then render themselves if they are the last item in a notebook cell. For versions of jupyterlab>=3.0 the necessary extension is automatically bundled in the pyviz_comms package, which must be >=2.0.
The answer to my problem turned out to be that I was installing panel after building the jupyterlab server through jupyterhub. (e.g. for a specific notebook, a user was running !pip install panel). This is where I get a little fuzzy since I'm not sure why this doesn't work, given that ipywidgets is already installed and panel install includes the pre-built jupyterlab extension. However, I was able to fix my issue by instead using an a jupyterlab image to spawn from jupyterhub which had ipywidgets>=7.6 and panel>=0.11.3 (and therefore pyviz_comms>=2.0) pre-installed. After this, panel widgets in jupyterlab on jupyterhub now work. Hope this helps anybody having a similar issue.
I have the same issue as #jtlz2 that none of the above suggestion works for me except #hainm's very specific version combination for jupyter-lab, ipywidgests, and jupyter-widgets/jupyterlab-manager.
Following the initial version numbers listed in this post (https://github.com/jupyter-widgets/ipywidgets/issues/2488#issuecomment-509719214), I tried to find the most updated version combination that works. I list them below, such that one can try it if there is really nothing else working for him/her.
pythonversion=3.8.0
labversion=2.1.5
labmanagerversion=2.0
ipywidgetsversion=7.5.1
nodejsversion=10.13.0
conda create -n lab python=$pythonversion -y
source activate lab
conda install nodejs=$nodejsversion -c conda-forge -y
conda install ipywidgets=$ipywidgetsversion -c conda-forge -y
conda install jupyterlab=$labversion -y -c conda-forge
jupyter-labextension install #jupyter-widgets/jupyterlab-manager#$labmanagerversion
It seems that the version of nodejs plays a key role. Holing everything else equal, if I update nodejsversion to 12.x+ or the latest 14.x, this combination as well as #hainm's combination both fail to make ipywidgets behave normally in Jupyterlab.
Other than the one I listed in the above code cell, below 6 combinations also work for me.
(pythonversion ,labversion ,labmanagerversion ,ipywidgets ,nodejsversion)
(3.7 , 0.34 , 0.37 , 7.4.2 , 10.13)
(3.7 , 1.0 , 1.0 , 7.4.2 , 10.13)
(3.7 , 2.0 , 2.0 , 7.4.2 , 10.13)
(3.8 , 2.0 , 2.0 , 7.4.2 , 10.13)
(3.8 , 2.0 , 2.0 , 7.5.1 , 10.13)
(3.8 , 2.1.5 , 2.0 , 7.5.1 , 10.13)
I was getting a Permission Denied error, so adding sudo to the accepted command helped: sudo jupyter labextension install #jupyter-widgets/jupyterlab-manager.
According to ipywidgets.readthedocs.io documentation (Installing the JupyterLab Extension), for "JupyterLab" do the following steps in "Anaconda Prompt".
Step 1
conda install -c conda-forge nodejs
Step 2
jupyter labextension install #jupyter-widgets/jupyterlab-manager
None of the other answers worked to me. It all seems to be down to version compatibility. Finally got it working - see below.
All credit to #hainm on github (original link: https://github.com/jupyter-widgets/ipywidgets/issues/2488#issuecomment-509719214)
My SO answer on this: https://stackoverflow.com/a/60059786/1021819
Here is a straight copy of that answer:
Leveraging https://github.com/jupyter-widgets/ipywidgets/issues/2488#issuecomment-509719214, in a jupyterlab terminal - running on jupyterhub - execute:
pythonversion=3.7
labversion=0.34.12
labmanagerversion=0.37.4
ipywidgetsversion=7.4.2
conda install ipywidgets=$ipywidgetsversion -c conda-forge -y --override-channels -c main
conda install jupyterlab=$labversion -y -c conda-forge --override-channels -c main
jupyter-labextension install #jupyter-widgets/jupyterlab-manager#$labmanagerversion
At this point a jupyter lab clean; jupyter lab build might be of interest.
Then in a .ipynb notebook running in the same jupyterlab window, hit the restart kernel button.
IMPORTANT: Don't forget to also REFRESH the browser page - or all efforts will have been in vain . :\
Then execute the example:
from ipywidgets import interact
#interact(x=(0, 100, 10))
def p(x=50):
pass
I never thought I would live to see the day but - hey presto - the widget finally appears!
The sad things are that the setup is extremely sensitive to the installation of other extensions and the combination of compatible versions is very specific.
Besides everything else, make sure that your ipywidgets are installed in the same conda environment as jupyterlab.
Here is my story (ipywidgets 7.7.0, jupyterlab 3.2.5 installed via conda inside docker):
Running dockerized jupyterlab in base conda environment
Created new conda environment, install ipykernel, make it visible to jupyterlab, install ipywidgets
Switch to new kernel, now jupyterlab recognized ipywidgets as installed, but widgets are not working (only displays text widget description), which is confusing
Solution:
Install ipywidgets in the same conda environment as jupyterlab
Disclaimer: My answer may not be applicable to the original question, since the author did not use separate conda environment. But it can be useful for others facing similar problem.
If you're on linux and you'd rather avoid conda entirely, and use virtual envs (venvs) to keep python happy, AND you happen to be using an 'older'/LTS Debian based OS, which may not have upto date nodejs: Ie, Ubuntu 16.04 LTS, which doesn't have a node but rather nodejs (node belongs to another package, and the 'legacy nodejs' version is too old), then read on.
This is a little more complicated to setup, but much easier to maintain long-term than conda is. (you can always just mk a new venv for a new project, without breaking your old projects).
Main points are:
use PPA's to get fresh versions of the things you need
Use virtualenvwrapper so you can:
use up-to-date python3
avoid messing up your 'pip install''s
avoid getting the system package manager confused
also easily work with people with different versions of python
follow python Best Practise
easily have different venvs which might have old or incompatible python and pip packages.
Use Nodejs binary distributions
need node.js version "10.x" for jupyterlab widgets
allows using the system's package manager to keep nodejs fresh
will be maintained and available as long as LTS's are
So, all actual steps (these were tested to work on Linux Mint 18.3 Sylvia, which is basically compatible with ubuntu xenial aka Ubuntu 16.04 LTS. Differences will arise mostly in nodejs, read the readme in the github link above to solve for other OS):
Get an admin to do (or do yourself if you can sudo):
sudo apt update
sudo apt install software-properties-common
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
sudo apt install -y python3.8 python3.8-dev python3.8-distutils python3-pip python3-venv
curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
sudo apt-get install -y nodejs
Then, as your own user, you can complete the rest of the steps:
pip3 install --user virtualenv virtualenvwrapper
mkdir ~/.envs
You'll then want to add the following to the end of your .bashrc :
export PATH=~/.local/bin:$PATH
export WORKON_HOME=~/.envs
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
source ~/.local/bin/virtualenvwrapper.sh
At this point, make a new shell, and you will be able to run the rest of the setup, actually installing jupyterlab:
mkvirtualenv -p python3.8 jupenv
pip install jupyter matplotlib pandas ipympl tqdm
jupyter labextension install #jupyter-widgets/jupyterlab-manager
Now you're done.
To open/use jupyter, you want (because of the venv I've called jupenv above, you can name it as you like in that mkvirtualenv line):
workon jupenv
jupyter lab
Otherwise, I had no end of hell trying to get nodejs to work with outdated ubuntu packages. Sometimes it would work, for a few restarts, and then fail. Other times it would just keep giving me the same missing widgets, or sometimes little lines of junk js code.
Virtualenvs are well worth using, especially when you start using python seriously, and working with others who may use different versions / different sets of pip packages. VirtualEnvWrapper makes this pretty painless.
The basic point is that everything you 'pip install', even jupyter, ends up being kept cleanly separate (and separate from the system packages), which keeps everything working very nicely.
There are some basic DO's and DON'T's:
DON'T run pip install ... lines without being in a venv
DON'T use pip3 in place of pip within a venv.
DO just use python and not python3 to run within.
DON'T use conda...!
DO know that you can have all your virtualenvs updated at once with:
allvirtualenv pip install -U pip
As for the nodejs binary distribution packages: These are highly recommended where they support your particular OS. They'll be very up-to-date and should present the minimum of trouble.
Please check steps to make it work in Jupyter 3.0 docs
Please check steps to make it work in Jupyter 1 0r 2 docs
After this restart or build the lab to see the changes
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