Skip to content

Instantly share code, notes, and snippets.

@mpilosov
Last active October 27, 2019 04:04
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mpilosov/233ccfae58d182d43f690be209b58ba5 to your computer and use it in GitHub Desktop.
Save mpilosov/233ccfae58d182d43f690be209b58ba5 to your computer and use it in GitHub Desktop.
Setting up Anaconda as a package manager, adding kernels to Jupyter.

How to Install Conda (a package manager)

Go download it from https://www.anaconda.com/download/ for your Linux/Windows/OSX

I like to use

wget http://repo.continuum.io/archive/Anaconda3-5.0.1-Linux-x86_64.sh

or if I want to keep things lightweight:

wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh

It will likely be saved in the Downloads folder, so you will install it with something like

bash ~/Downloads/Anaconda3-5.0.1-Linux-x86_64.sh

Use all the defaults, agree to everything, and at the end, hit ENTER to prepend it to your PATH

Now let's create an environment for python3:
conda create -n py3 python=3.6 where py3 is just my personal preferred shorthand for python 3. Feel free to use your own.

And one for python2:
conda create -n py2 python=2.7

To remove:
conda remove --name py2 --all

We can now switch between versions of python with commands like
source activate py3 and source activate py2 and then source deactivate when you're ready to be done.
In each of these environments, run
conda install -c conda-forge ipywidgets
which will install all sorts of dependencies, including Jupyter and notebooks. It supposedly enables the widgets too but you can run jupyter nbextension enable --py widgetsnbextension to make sure.

python -m ipykernel install --name py3 --user will then link the kernel to jupyter. Then --name py3 flag is optional and just assigns a name of your choosing to that kernel. The version of python used for the kernel is whatever is returned when you ask bash which python (so if you used source activate earlier, it will be the version associated with the kernel whose name is in parentheses in your Terminal session). The --user flag is necessary.

To see all the kernels jupyter is aware of: jupyter kernelspec list

To remove kernels, use jupyter kernelspec remove


JupyterHub

https://jupyterhub.readthedocs.io/en/0.7.2/getting-started.html

Running a notebook server:
https://jupyter-notebook.readthedocs.io/en/latest/public_server.html


Widgets

pip install ipywidgets
jupyter nbextension install --py --user widgetsnbextension
jupyter nbextension enable --py widgetsnbextension

More information about widgets can be found here


iPyParallel

https://github.com/ipython/ipyparallel
pip install ipyparallel to install.
ipcluster nbextension enable (add the --user flag as well if you encounter an error). This one also seems to be necessary to get the clusters to show up:
jupyter serverextension enable --sys-prefix --py ipyparallel

To install for all users (as root):

jupyter nbextension install --sys-prefix --py ipyparallel
jupyter nbextension enable --sys-prefix --py ipyparallel

JupyterLab

I love this environment but it is a little tricky to set up properly and get everything working as expected. Though once you do, I doubt you will want to use anything else.

Once Jupyter Lab is installed, go ahead and run it the same way you would a notebook:

jupyter lab (--no-browser)

By default (as of Dec 2018), it will be accessible through localhost:8888

Go ahead and launch an instance of Python and see if widgets work.

import ipywidgets as wd
wd.FloatSlider()

If the output of the following is text instead of a widget, try running the following in the command-line (shutdown the server with Ctrl-C twice, then

jupyter labextension install @jupyter-widgets/jupyterlab-manager

And relaunch JupyterLab (if you didn't close your browswer window, a simple refresh will work to re-establish the connection and pick up where you left off), and try the example above again.

Something that came up once

I saw my server output spitting out TypeError: __init__() got an unexpected keyword argument 'io_loop' over and over again when any kernel was started. Something was going on with the WebSocket I/O communication (whatever that means), but the following downgrade of the Python Web Framework worked to fix it:

pip install tornado==4.5.3


How to Export an Environment

conda env export > environment.yml

Which you then load up with conda env create -f environment.yml

Cloning

You can make an exact copy of an environment by creating a clone of it:
conda create --name myclone --clone myenv

To verify that the copy was made:
conda info --envs

@mpilosov
Copy link
Author

mpilosov commented Jan 11, 2018

For exporting cross-platforms, it is kind of tricky. Besides manually editing the yaml file, the other option available to you is to attempt to export without build dependencies by adding the --no-builds flag in the export command

@mathematicalmichael
Copy link

jupyter nbextension enable --sys-prefix --py widgetsnbextension is necessary for system-wide enabling (such as with docker container)

@mathematicalmichael
Copy link

mathematicalmichael commented Jul 6, 2018

jupyter labextension install @jupyter-widgets/jupyterlab-manager
for Lab

@mathematicalmichael
Copy link

may have to upgrade node

sudo npm cache clean -f
sudo npm install -g n
sudo n stable

@mathematicalmichael
Copy link

--sys-prefix is deprecated now.

also, for mac: https://repo.anaconda.com/archive/Anaconda3-2019.07-MacOSX-x86_64.sh

@mathematicalmichael
Copy link

this is getting out of hand... definitely should scrap altogether in favor of docker. =P

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment