LoginSignup
8

More than 3 years have passed since last update.

Jupyter Notebookでwxpython [Mac]

Last updated at Posted at 2018-03-30

はじめに

wxpythonを用いたアプリケーション開発に携わることになり,Jupyter Notebookを用いて進めようと思ったが環境構築に少し手間取ったのでメモ.
Anacondaを入れ終わった直後の状態からJupyter NoteBook上でwxpythonを使えるようになるまでをまとめる.

実行環境

Mac OS High Sierra 10.13.3

Anacondaで仮想環境を構築する

まずはwxpythonを使用するための仮想環境を作る.

conda-create
$ conda create -n wxenv python=3.6 anaconda
$ conda info -e
wxenv                    /Users/username/anaconda3/envs/wxenv
root                  *  /Users/username/anaconda3

conda info -eコマンドで仮想環境が作られているか確認できる.

wxpythonをインストールする

構築した仮想環境に入る.

conda-activate
$ source activate wxenv

仮想環境から抜けるにはdeactivateコマンドを用いる.

conda-deactivate
(wxenv)$ source deactivate

仮想環境にwxpythonをインストールする.

install-wxpython
(wxenv)$ conda install wxpython

Jupyterのカーネルを作成する

この時点でJupyter Notebookにおいてwxpythonを用いたコードを実行すると以下のエラーが出る.

error-message
This program needs access to the screen. Please run with a Framework build of python, and only when you are logged in on the main display of your Mac.

この問題を解決するにはpythonwコマンドを用いればいいらしい.
Please run with 'pythonw', not 'python' — cannot make plots in python/ipython from cmd line OSX

pythonwを用いるカーネルを作成して実行することとする.

カーネルの作成

kernel作成については複数の手段が紹介されている.
以下のサイトを基に作成した.
Jupyter上でkernel切り替えをしてもうまく機能しない場合の解決方法

jupyter kernelspec listコマンドで使用できるカーネルを確認できる.

kernel-list
(wxenv)$ jupyter kernelspec list
Available kernels:
  python3     /Users/username/anaconda3/envs/wxenv/share/jupyter/kernels/python3

カーネルを追加する.

create-kernel
(wxenv)$ conda install notebook ipykernel
(wxenv)$ ipython kernel install --user --name wxpython --display-name wxpython
Installed kernelspec wxpython in /Users/username/Library/Jupyter/kernels/wxpython

--name NAMEで名前を指定しないと既存のPython3のkernelが上書きされるので注意.
--display-name DISPLAY-NAMEでJupyter Notebook上での表記を変更できる.

カーネルが追加されたか確認.

kernel-list2
(wxenv)$ jupyter kernelspec list
Available kernels:
  python3     /Users/username/anaconda3/envs/wxenv/share/jupyter/kernels/python3
  wxpython    /Users/username/Library/Jupyter/kernels/wxpython

既存のカーネルの保存場所と異なる場所にwxpythonが保存されている.
仮想環境内でしか使わないので既存のカーネルの場所と同じ場所に移す.

kernel-list2
(wxenv)$ mv /Users/username/Library/Jupyter/kernels/wxpython /Users/username/anaconda3/envs/wxenv/share/jupyter/kernels/wxpython

(wxenv)$ jupyter kernelspec list
Available kernels:
  python3     /Users/username/anaconda3/envs/wxenv/share/jupyter/kernels/python3
  wxpython    /Users/username/anaconda3/envs/wxenv/share/jupyter/kernels/wxpython

既存のカーネルはそのフォルダを消去すれば消える.

remove-kernel
(wxenv)$ jupyter kernelspec list
Available kernels:
  python3     /Users/username/anaconda3/envs/wxenv/share/jupyter/kernels/python3
  wxpython    /Users/username/anaconda3/envs/wxenv/share/jupyter/kernels/wxpython

(wxenv)$ rm -r /Users/username/anaconda3/envs/wxenv/share/jupyter/kernels/wxpython
(wxenv)$ jupyter kernelspec list
Available kernels:
  python3     /Users/username/anaconda3/envs/wxenv/share/jupyter/kernels/python3

コマンドの変更(python→pythonw)

カーネルで用いるコマンドはカーネルが保存されているフォルダ内のkernel.jsonに書かれている.

kernel.json
{
 "argv": [
  "/Users/username/anaconda3/envs/wxenv/bin/python",
  "-m",
  "ipykernel_launcher",
  "-f",
  "{connection_file}"
 ],
 "display_name": "wxpython",
 "language": "python"
}

ファイル内の/Users/username/anaconda3/envs/wxenv/bin/python/Users/username/anaconda3/envs/wxenv/bin/pythonwに書き換えて保存.
これでwxpythonを使用できるようになった.

テスト

Jupyter Notebookを開き,kernelを変更.

スクリーンショット 2018-03-30 15.20.38.png

以下のサンプルコードを実行

sample-code
import wx

application = wx.App()
frame = wx.Frame(None, wx.ID_ANY, 'testframe')
frame.Show()

application.MainLoop()

testframeという名前のウィンドウが出てきたらOK.
スクリーンショット 2018-03-30 15.22.36.png

さいごに

一応,Jupyter Notebookでwxpythonを用いることができるようになったので作業が捗るはず.
(もっといい方法があったり,この方法に問題があるかもしれない)

参照

Please run with 'pythonw', not 'python' — cannot make plots in python/ipython from cmd line OSX
Jupyter上でkernel切り替えをしてもうまく機能しない場合の解決方法
Jupyter Notebookをインストール・設定して勉強ノート作成環境をつくる [Mac]
How to use wxpython in an ipython notebook or console

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
8