Coexistence of Anaconda 2 and Anaconda 3 in Jupyter + Bonus (Julia)

(PS2016/1/13) After reading Jupyter is coming so fast, prepare for you (IPython Notebook + R), I'm addicted to Jupyter.

I'm using pyenv to make Anaconda 2 and Anaconda 3 coexist, but when switching between Anaconda versions with Jupyter, after quitting Jupyter, switch with pyenv and start again! It became troublesome to do. I wish I could easily switch between Python 2 and Python 3 from the New menu! I thought, so I tried to set that area. (Because I'm new to Jupyter, please let me know in the comments if there is an easier way ...)

environment

The following Python is installed in pyenv.

Junbi

Whether you use pyenv or not, Jupyter reflects the Python environment applied by pyenv.

However, it gets in the way when using Jupyter. I want to be able to easily switch between Anaconda 2 and Anaconda 3. Therefore, I will put a symbolic link as follows so that each Anaconda can be started directly.

cd /usr/local/bin/ #Where the PATH is
ln -s ~/.pyenv/versions/anaconda-2.4.0/bin/python ./jupyter-python2
ln -s ~/.pyenv/versions/anaconda3-2.4.0/bin/python ./jupyter-python3

I'm doing this for a file called kernel.json that I'm about to write, is there a better way ...?

Now let's make Python 2 and Python 3 appear together in the New menu.

Add Python kernel to Jupyter

First, hit the following command.

mkdir -p ~/.ipython/kernels/python3
mkdir ~/.ipython/kernels/python2

touch ~/.ipython/kernels/python3/kernel.json
touch ~/.ipython/kernels/python2/kernel.json

In this way, you can add kernels by creating a .ipython / kernels directory in your home directory and a directory with a kernel.json file under it.

Now, edit ~ / .ipython / kernels / python2 / kernel.json with your favorite editor as follows.

python2/kernel.json


{
    "display_name": "Python 2"
    "language": "python"
    "argv": [
        "jupyter-python2",
        "-m", "ipykernel",
        "-f", "{connection_file}"
    ]
}

In this way, you can specify the command and its arguments in the " argv " field. So, if you specify jupyter-python2 earlier, Jupyter will start Anaconda 2 by selecting" Python 2 "no matter which Python is applied in pyenv.

Here, I got into the "ipykernel" part. Actually, when I look at various sites, this part is described as "IPython.kernel". When I did the same in my environment, I got the following Warning, which didn't seem to be good.

ShimWarning: The `IPython.kernel` package has been deprecated.

From the Warning statement that was output at that time, I felt the atmosphere of "No, import the ipykernel", so when I specified ʻipykernel` as it is now, Warning disappeared , It has settled down to its current shape.

So, based on that, I also write python3 as follows.

python3/kernel.json


{
    "display_name": "Python 3"
    "language": "python"
    "argv": [
        "jupyter-python3",
        "-m", "ipykernel",
        "-f", "{connection_file}"
    ]
}

This should probably allow you to switch within your notebook without having to go back to pyenv.

(Addition) conda + jupyter kernelspec

y__sama gave me some information in the comments, so I don't have to do the above crazy things, so I'll add it here.

By the way, there is a command called conda in the Anaconda area. I knew it existed, but I didn't know what it was. (Eh.) Simply put, this also seems to create a virtual environment.

For example, by using conda of anaconda3, I could create a virtual environment jupyter-py2 like ~ / .pyenv / versions / anaconda3-2.4.0 / envs / jupyter-py2 /.

With the Python virtual environment active, you can type the following command to automatically add the kernel to ~ / Library / Jupyter / kernels /.

jupyter kernelspec install-self --user

(Addition) Oh, that means

Looking at my usage and the contents of the generated kernel.json, I felt that there was no need to create a virtual environment with conda. So now (for now) it looks like this:

--Prepare anaconda2 and anaconda3 in pyenv --Activate anaconda2, jupyter kernelspec install-self --user --Activate anaconda3, jupyter kernelspec install-self --user

Anyway, all you need to know is jupyter kernelspec install-self --user. Thank you for studying.

I also want to add Julia!

At this point, you'll want to add the Julia kernel as well. I think Jupyter is also useful for studying Julia, which is a hot topic these days, so I will add it at this opportunity.

Julia installation

I dropped the dmg file from Julialang.org/downloads and installed it. It seems that you can do it with Homebrew, but I have the impression that the build failed or it was troublesome, so this is recommended (laugh)

After that, put the alias julia in /Applications/Julia-0.4.2.app/Contents/Resources/julia/bin/julia.

Install IJulia

Then install ʻIJulia`. Start Julia and type:

Pkg.add("IJulia")

Then, in my case, the following error was output near the end.

===============================[ ERROR: IJulia ]================================

LoadError: __precompile__(true) but require failed to create a precompiled cache file
while loading /Users/"username"/.julia/v0.4/IJulia/deps/build.jl, in expression starting on line 2

================================================================================

=====================================[ BUILD ERRORS ]=====================================

WARNING: IJulia had build errors.

 - packages with build errors remain installed in /Users/"username"/.julia/v0.4
 - build the package(s) and all dependencies with `Pkg.build("IJulia")`
 - build a single package by running its `deps/build.jl` script

==========================================================================================

I thought this was ... but it worked if I did the following. why? Lol

Pkg.rm("IJulia")
Pkg.add("IJulia")

Launch Jupyter notebook from Julia

At this point, start Julia and do the following to start notebook as usual.

using IJulia
notebook()

It was added to the New menu like this.

ijulia.png

After that, even if I opened Jupyter notebook in Python, there was an item of Julia in the New menu, so it seems that there is no need to write kernel.json for julia as before. (If necessary, add a style at that time lol)

So it's sunny and I can write notebooks in Julia! Thank you for your hard work.

(Addition) By the way

Julia kernels added this way are saved in ~ / Library / Jupyter /.

People like me who are annoyed by the details such as wanting to change "Julia 0.4.2" to "Julia" in the NEW menu of the notebook should take a look at kernel.json here.

References

These are the articles and sites that were taken care of this time. I hope this is also helpful!

-Jupyter environment setting supplement #pythontokai

Recommended Posts

Coexistence of Anaconda 2 and Anaconda 3 in Jupyter + Bonus (Julia)
About import error of numpy and scipy in anaconda
Coexistence of pyenv and autojump
Coexistence of Python2 and 3 with CircleCI (1.0)
Coexistence of Flask logging and other module logging
Screenshots of Megalodon in selenium and Chrome.
Separation of design and data in matplotlib
Summary of modules and classes in Python-TensorFlow2-
Project Euler # 1 "Multiples of 3 and 5" in Python
Summary of OSS tools and libraries created in 2016
Coexistence of Fcitx and Zoom ~ With Japanese localization ~
Browser specification of Jupyter Notebook in Windows environment
Header shifts in read_csv () and read_table () of Pandas
Error running Jupyter Notebook in Anaconda virtual environment
Explanation of edit distance and implementation in Python
Installation of Anaconda3
Python 3.3 in Anaconda
Bash in Jupyter
"Linear regression" and "Probabilistic version of linear regression" in Python "Bayesian linear regression"
Full-width and half-width processing of CSV data in Python
Specify past version of chromedriver in Anaconda update / install
Calculation of standard deviation and correlation coefficient in Python
Difference between Ruby and Python in terms of variables
[python] Calculation of months and years of difference in datetime
Automatically save .py and .html files in Jupyter notebook.
Settings for easy selection of multiple kernels in Jupyter
Overview of generalized linear models and implementation in Python
Personal notes about the integration of vscode and anaconda
Sample of getting module name and class name in Python
Summary of date processing in Python (datetime and dateutil)
How to build Anaconda virtual environment used in Azure Machine Learning and link with Jupyter