LoginSignup
5
9

More than 3 years have passed since last update.

複数のpythonのバージョンを1つの jupyter で扱う

Posted at

Overview

  • python のバージョンを複数使いたい(venv含む)
  • けど、それぞれのバージョンに jupyter 入れるのは効率が悪い
  • ので、どれか1つのバージョンに jupyter を入れて kernel だけ選べるようにしたい

ときってあるじゃないですか。

具体的に

こういう状況で、

bash
$ pyenv versions
  system
  3.6.12
* 3.8.6 (set by /Users/kuryu/.pyenv/version)

3.8.6 で jupyter を起動して、 3.6.12 の kernel を動かすのを目指す。

まず jupyter を入れる

bash
$ python -V
Python 3.8.6

$ pip install jupyter

python のバージョンを変更する

bash
$ pyenv global 3.6.12

$ python -V
Python 3.6.12

必要なら venv を作成

pyenv 環境にそのまま構築することも可能だけど、今回は venv 作ります。

bash
$ python -m venv .venv

$ . .venv/bin/activate

(.venv) $ python -V
Python 3.6.12

(.venv) $ pip list
Package    Version
---------- -------
pip        18.1
setuptools 40.6.2
You are using pip version 18.1, however version 20.2.4 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

ipykernel を入れる

bash
(.venv) $ pip install ipykernel

(.venv) $ ipython kernel install --user --name=hoge

仮想環境を出て

bash
(.venv) $ deactivate

python のバージョンも戻す

bash
$ pyenv global 3.8.6

$ python -V
Python 3.8.6

おもむろに jupyter を起動

bash
$ jupyter notebook

kernel が追加されているので

スクリーンショット 2020-10-22 19.06.29.png

実行してバージョンを確認

3.8.6 で起動した jupyter の中で 3.6.12 の kernel を起動できました。

ちなみに、スクショ撮り忘れちゃったんですけど、

jupyter
sys.executable

とかやると、ちゃんと venv 環境の python のパスが表示されます。

スクリーンショット 2020-10-22 19.01.03.png

kernel が不要になったら

bash
$ jupyter kernelspec uninstall hoge

っていう話なんですけど、まぁまぁややこしいので混乱しないように注意。

cf.

5
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
9