📘hkob-astro-notion-blog

これまではてなブログにて情報発信をしていましたが、令和5年3月22日より、こちらでの情報発信を始めました。2019年以前の古い記事は過去の Middleman 時代のものなので、情報が古いです。記録のためだけに残しています。

Python 環境の作成 - Rubyist の Python 学習記録(3)

Python のインストール

macOS Mojave にデフォルトで入っていた Python は「Python 2.7.10」であった. これから学習を始めるのであれば,わざわざ古い Python を使う必要もないので,Python 3 をインストールする. 以下手順を示す.すでにインストール済みだったので,一度全部アンインストールしてから再実行してみた.

  • rbenv のように pyenv で複数の Python をインストールすることも可能だが,Ruby on Rails のように古い環境を残す必要がないので,今回は homebrew で python をインストールする.
    $ brew install python
    ==> Downloading https://homebrew.bintray.com/bottles/python-3.7.2.mojave.bottle.tar.gz
    Already downloaded: /Users/hkob/Library/Caches/Homebrew/downloads/4815dde6eb3f3f13f2f9ad517589a3527953075de73c6c16dfdbc6067cd9d392--python-3.7.2.mojave.bottle.tar.gz
    ==> Pouring python-3.7.2.mojave.bottle.tar.gz
    ==> /usr/local/Cellar/python/3.7.2/bin/python3 -s setup.py --no-user-cfg install --force --verbose --install-scripts=/usr/local/Cellar/python/3.7.2/bin --install-lib=/usr/local/l
    ==> /usr/local/Cellar/python/3.7.2/bin/python3 -s setup.py --no-user-cfg install --force --verbose --install-scripts=/usr/local/Cellar/python/3.7.2/bin --install-lib=/usr/local/l
    ==> /usr/local/Cellar/python/3.7.2/bin/python3 -s setup.py --no-user-cfg install --force --verbose --install-scripts=/usr/local/Cellar/python/3.7.2/bin --install-lib=/usr/local/l
    ==> Caveats
    Python has been installed as
      /usr/local/bin/python3
    
    Unversioned symlinks `python`, `python-config`, `pip` etc. pointing to
    `python3`, `python3-config`, `pip3` etc., respectively, have been installed into
      /usr/local/opt/python/libexec/bin
    
    If you need Homebrew's Python 2.7 run
      brew install python@2
    
    You can install Python packages with
      pip3 install <package>
    They will install into the site-package directory
      /usr/local/lib/python3.7/site-packages
    
    See: https://docs.brew.sh/Homebrew-and-Python
    ==> Summary
    🍺  /usr/local/Cellar/python/3.7.2: 3,833 files, 59.3MB
  • NumPy も homebrew からインストールすることは可能だが,二重のパッケージ管理に煩わされたくないので,pip3 で直接インストールすることにする.
    $ pip3 install numpy
    Collecting numpy
      Downloading https://files.pythonhosted.org/packages/3d/c3/a69406093c9a780a74964f41cd56b06c0346d686a9b3f392d123a663f5e0/numpy-1.15.4-cp37-cp37m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (24.5MB)
        100% |████████████████████████████████| 24.5MB 672kB/s
    Installing collected packages: numpy
    Successfully installed numpy-1.15.4
Jupyter Lab のインストール

研究レポート記録のために,Jupyter Lab をインストールする.

  • Jupyter Lab をインストールする.
    $ pip3 install jupyterlab
    Collecting jupyterlab
      Downloading https://files.pythonhosted.org/packages/45/77/40f2a382508d78a3f9fc59b98d67edd5b2999f8441d3945bfd37d2351a4d/jupyterlab-0.35.4-py3-none-any.whl (14.7MB)
        100% |████████████████████████████████| 14.7MB 1.3MB/s
    Collecting notebook>=4.3.1 (from jupyterlab)
      Downloading https://files.pythonhosted.org/packages/f8/a6/dbdf0954d073ab2cf5421cad20334d1e4c780da37205c04522c045298f67/notebook-5.7.4-py2.py3-none-any.whl (9.0MB)
        100% |████████████████████████████████| 9.0MB 345kB/s
    Collecting jupyterlab-server<0.3.0,>=0.2.0 (from jupyterlab)
      Downloading https://files.pythonhosted.org/packages/78/77/e8a9c300afbe24aa46abaf1091d9e7b82328559e99cf2d601e858bcb3e1a/jupyterlab_server-0.2.0-py3-none-any.whl
    Collecting prometheus-client (from notebook>=4.3.1->jupyterlab)
      Downloading https://files.pythonhosted.org/packages/bc/e1/3cddac03c8992815519c5f50493097f6508fa153d067b494db8ab5e9c4ce/prometheus_client-0.5.0.tar.gz
    Collecting terminado>=0.8.1 (from notebook>=4.3.1->jupyterlab)
      Downloading https://files.pythonhosted.org/packages/2e/20/a26211a24425923d46e1213b376a6ee60dc30bcdf1b0c345e2c3769deb1c/terminado-0.8.1-py2.py3-none-any.whl
    Collecting ipython-genutils (from notebook>=4.3.1->jupyterlab)
      Downloading https://files.pythonhosted.org/packages/fa/bc/9bd3b5c2b4774d5f33b2d544f1460be9df7df2fe42f352135381c347c69a/ipython_genutils-0.2.0-py2.py3-none-any.whl
    Collecting jupyter-client>=5.2.0 (from notebook>=4.3.1->jupyterlab)
      Downloading https://files.pythonhosted.org/packages/3b/c3/3043fe9ffd140d03c9d091a056794ccdc427c56ec19b8eea74f9ea0a498f/jupyter_client-5.2.4-py2.py3-none-any.whl (89kB)
        100% |████████████████████████████████| 92kB 1.5MB/s
    Collecting nbformat (from notebook>=4.3.1->jupyterlab)
      Downloading https://files.pythonhosted.org/packages/da/27/9a654d2b6cc1eaa517d1c5a4405166c7f6d72f04f6e7eea41855fe808a46/nbformat-4.4.0-py2.py3-none-any.whl (155kB)
        100% |████████████████████████████████| 163kB 807kB/s
    Collecting Send2Trash (from notebook>=4.3.1->jupyterlab)
      Downloading https://files.pythonhosted.org/packages/49/46/c3dc27481d1cc57b9385aff41c474ceb7714f7935b1247194adae45db714/Send2Trash-1.5.0-py3-none-any.whl
    Collecting tornado>=4 (from notebook>=4.3.1->jupyterlab)
      Downloading https://files.pythonhosted.org/packages/e6/78/6e7b5af12c12bdf38ca9bfe863fcaf53dc10430a312d0324e76c1e5ca426/tornado-5.1.1.tar.gz (516kB)
        100% |████████████████████████████████| 522kB 2.3MB/s
    Collecting traitlets>=4.2.1 (from notebook>=4.3.1->jupyterlab)
      Downloading https://files.pythonhosted.org/packages/93/d6/abcb22de61d78e2fc3959c964628a5771e47e7cc60d53e9342e21ed6cc9a/traitlets-4.3.2-py2.py3-none-any.whl (74kB)
        100% |████████████████████████████████| 81kB 344kB/s
    Collecting ipykernel (from notebook>=4.3.1->jupyterlab)
      Downloading https://files.pythonhosted.org/packages/d8/b0/f0be5c5ab335196f5cce96e5b889a4fcf5bfe462eb0acc05cd7e2caf65eb/ipykernel-5.1.0-py3-none-any.whl (113kB)
        100% |████████████████████████████████| 122kB 2.9MB/s
    Collecting nbconvert (from notebook>=4.3.1->jupyterlab)
      Downloading https://files.pythonhosted.org/packages/b5/bb/94c493051d60e5b9c0f7f9a368b324201818c1b1c4cae85d1e49a41846c7/nbconvert-5.4.0-py2.py3-none-any.whl (405kB)
        100% |████████████████████████████████| 409kB 2.4MB/s
    Collecting jupyter-core>=4.4.0 (from notebook>=4.3.1->jupyterlab)
      Downloading https://files.pythonhosted.org/packages/1d/44/065d2d7bae7bebc06f1dd70d23c36da8c50c0f08b4236716743d706762a8/jupyter_core-4.4.0-py2.py3-none-any.whl (126kB)
        100% |████████████████████████████████| 133kB 2.8MB/s
    Collecting jinja2 (from notebook>=4.3.1->jupyterlab)
      Downloading https://files.pythonhosted.org/packages/7f/ff/ae64bacdfc95f27a016a7bed8e8686763ba4d277a78ca76f32659220a731/Jinja2-2.10-py2.py3-none-any.whl (126kB)
        100% |████████████████████████████████| 133kB 541kB/s
    Collecting pyzmq>=17 (from notebook>=4.3.1->jupyterlab)
      Downloading https://files.pythonhosted.org/packages/bd/ce/d068e7494c29ddf0c6850186fc4a81ec180b4612a30ba187ceb3fd266179/pyzmq-17.1.2-cp37-cp37m-macosx_10_9_x86_64.whl (741kB)
        100% |████████████████████████████████| 747kB 3.2MB/s
    Collecting jsonschema>=2.6.0 (from jupyterlab-server<0.3.0,>=0.2.0->jupyterlab)
      Downloading https://files.pythonhosted.org/packages/77/de/47e35a97b2b05c2fadbec67d44cfcdcd09b8086951b331d82de90d2912da/jsonschema-2.6.0-py2.py3-none-any.whl
    Collecting ptyprocess; os_name != "nt" (from terminado>=0.8.1->notebook>=4.3.1->jupyterlab)
      Downloading https://files.pythonhosted.org/packages/d1/29/605c2cc68a9992d18dada28206eeada56ea4bd07a239669da41674648b6f/ptyprocess-0.6.0-py2.py3-none-any.whl
    Collecting python-dateutil>=2.1 (from jupyter-client>=5.2.0->notebook>=4.3.1->jupyterlab)
      Downloading https://files.pythonhosted.org/packages/74/68/d87d9b36af36f44254a8d512cbfc48369103a3b9e474be9bdfe536abfc45/python_dateutil-2.7.5-py2.py3-none-any.whl (225kB)
        100% |████████████████████████████████| 235kB 296kB/s
    Collecting six (from traitlets>=4.2.1->notebook>=4.3.1->jupyterlab)
      Downloading https://files.pythonhosted.org/packages/73/fb/00a976f728d0d1fecfe898238ce23f502a721c0ac0ecfedb80e0d88c64e9/six-1.12.0-py2.py3-none-any.whl
    Collecting decorator (from traitlets>=4.2.1->notebook>=4.3.1->jupyterlab)
      Downloading https://files.pythonhosted.org/packages/bc/bb/a24838832ba35baf52f32ab1a49b906b5f82fb7c76b2f6a7e35e140bac30/decorator-4.3.0-py2.py3-none-any.whl
    Collecting ipython>=5.0.0 (from ipykernel->notebook>=4.3.1->jupyterlab)
      Downloading https://files.pythonhosted.org/packages/f0/b4/a9ea018c73a84ee6280b2e94a1a6af8d63e45903eac2da0640fa63bca4db/ipython-7.2.0-py3-none-any.whl (765kB)
        100% |████████████████████████████████| 768kB 2.2MB/s
    Collecting pandocfilters>=1.4.1 (from nbconvert->notebook>=4.3.1->jupyterlab)
      Downloading https://files.pythonhosted.org/packages/4c/ea/236e2584af67bb6df960832731a6e5325fd4441de001767da328c33368ce/pandocfilters-1.4.2.tar.gz
    Collecting defusedxml (from nbconvert->notebook>=4.3.1->jupyterlab)
      Downloading https://files.pythonhosted.org/packages/87/1c/17f3e3935a913dfe2a5ca85fa5ccbef366bfd82eb318b1f75dadbf0affca/defusedxml-0.5.0-py2.py3-none-any.whl
    Collecting testpath (from nbconvert->notebook>=4.3.1->jupyterlab)
      Downloading https://files.pythonhosted.org/packages/be/a4/162f9ebb6489421fe46dcca2ae420369edfee4b563c668d93cb4605d12ba/testpath-0.4.2-py2.py3-none-any.whl (163kB)
        100% |████████████████████████████████| 163kB 1.9MB/s
    Collecting pygments (from nbconvert->notebook>=4.3.1->jupyterlab)
      Downloading https://files.pythonhosted.org/packages/13/e5/6d710c9cf96c31ac82657bcfb441df328b22df8564d58d0c4cd62612674c/Pygments-2.3.1-py2.py3-none-any.whl (849kB)
        100% |████████████████████████████████| 849kB 3.7MB/s
    Collecting entrypoints>=0.2.2 (from nbconvert->notebook>=4.3.1->jupyterlab)
      Downloading https://files.pythonhosted.org/packages/cc/8b/4eefa9b47f1910b3d2081da67726b066e379b04ca897acfe9f92bac56147/entrypoints-0.2.3-py2.py3-none-any.whl
    Collecting bleach (from nbconvert->notebook>=4.3.1->jupyterlab)
      Downloading https://files.pythonhosted.org/packages/d4/0d/4696373c3b714f6022d668fbab619690a42050dbeacede6d10ed97fbd3e2/bleach-3.0.2-py2.py3-none-any.whl (148kB)
        100% |████████████████████████████████| 153kB 5.0MB/s
    Collecting mistune>=0.8.1 (from nbconvert->notebook>=4.3.1->jupyterlab)
      Downloading https://files.pythonhosted.org/packages/09/ec/4b43dae793655b7d8a25f76119624350b4d65eb663459eb9603d7f1f0345/mistune-0.8.4-py2.py3-none-any.whl
    Collecting MarkupSafe>=0.23 (from jinja2->notebook>=4.3.1->jupyterlab)
      Downloading https://files.pythonhosted.org/packages/96/52/eef455862764cb6d6c136aa52c7f9fc4e7c1c644790a7107b1244a2b8a53/MarkupSafe-1.1.0-cp37-cp37m-macosx_10_6_intel.whl
    Collecting prompt-toolkit<2.1.0,>=2.0.0 (from ipython>=5.0.0->ipykernel->notebook>=4.3.1->jupyterlab)
      Downloading https://files.pythonhosted.org/packages/d1/e6/adb3be5576f5d27c6faa33f1e9fea8fe5dbd9351db12148de948507e352c/prompt_toolkit-2.0.7-py3-none-any.whl (338kB)
        100% |████████████████████████████████| 348kB 8.3MB/s
    Collecting pexpect; sys_platform != "win32" (from ipython>=5.0.0->ipykernel->notebook>=4.3.1->jupyterlab)
      Downloading https://files.pythonhosted.org/packages/89/e6/b5a1de8b0cc4e07ca1b305a4fcc3f9806025c1b651ea302646341222f88b/pexpect-4.6.0-py2.py3-none-any.whl (57kB)
        100% |████████████████████████████████| 61kB 8.6MB/s
    Collecting backcall (from ipython>=5.0.0->ipykernel->notebook>=4.3.1->jupyterlab)
      Downloading https://files.pythonhosted.org/packages/84/71/c8ca4f5bb1e08401b916c68003acf0a0655df935d74d93bf3f3364b310e0/backcall-0.1.0.tar.gz
    Collecting jedi>=0.10 (from ipython>=5.0.0->ipykernel->notebook>=4.3.1->jupyterlab)
      Downloading https://files.pythonhosted.org/packages/c2/bc/54d53f5bc4658380d0eca9055d72be4df45e5bfd91a4bac97da224a92553/jedi-0.13.2-py2.py3-none-any.whl (177kB)
        100% |████████████████████████████████| 184kB 7.4MB/s
    Requirement already satisfied: setuptools>=18.5 in /usr/local/lib/python3.7/site-packages (from ipython>=5.0.0->ipykernel->notebook>=4.3.1->jupyterlab) (40.6.3)
    Collecting appnope; sys_platform == "darwin" (from ipython>=5.0.0->ipykernel->notebook>=4.3.1->jupyterlab)
      Downloading https://files.pythonhosted.org/packages/87/a9/7985e6a53402f294c8f0e8eff3151a83f1fb901fa92909bb3ff29b4d22af/appnope-0.1.0-py2.py3-none-any.whl
    Collecting pickleshare (from ipython>=5.0.0->ipykernel->notebook>=4.3.1->jupyterlab)
      Downloading https://files.pythonhosted.org/packages/9a/41/220f49aaea88bc6fa6cba8d05ecf24676326156c23b991e80b3f2fc24c77/pickleshare-0.7.5-py2.py3-none-any.whl
    Collecting webencodings (from bleach->nbconvert->notebook>=4.3.1->jupyterlab)
      Downloading https://files.pythonhosted.org/packages/f4/24/2a3e3df732393fed8b3ebf2ec078f05546de641fe1b667ee316ec1dcf3b7/webencodings-0.5.1-py2.py3-none-any.whl
    Collecting wcwidth (from prompt-toolkit<2.1.0,>=2.0.0->ipython>=5.0.0->ipykernel->notebook>=4.3.1->jupyterlab)
      Downloading https://files.pythonhosted.org/packages/7e/9f/526a6947247599b084ee5232e4f9190a38f398d7300d866af3ab571a5bfe/wcwidth-0.1.7-py2.py3-none-any.whl
    Collecting parso>=0.3.0 (from jedi>=0.10->ipython>=5.0.0->ipykernel->notebook>=4.3.1->jupyterlab)
      Downloading https://files.pythonhosted.org/packages/09/51/9c48a46334be50c13d25a3afe55fa05c445699304c5ad32619de953a2305/parso-0.3.1-py2.py3-none-any.whl (88kB)
        100% |████████████████████████████████| 92kB 4.0MB/s
    Building wheels for collected packages: prometheus-client, tornado, pandocfilters, backcall
      Running setup.py bdist_wheel for prometheus-client ... done
      Stored in directory: /Users/hkob/Library/Caches/pip/wheels/1a/74/d7/dc59e0bf44fdfd6395c0076129453abf563e4aeca5d72c8574
      Running setup.py bdist_wheel for tornado ... done
      Stored in directory: /Users/hkob/Library/Caches/pip/wheels/6d/e1/ce/f4ee2fa420cc6b940123c64992b81047816d0a9fad6b879325
      Running setup.py bdist_wheel for pandocfilters ... done
      Stored in directory: /Users/hkob/Library/Caches/pip/wheels/39/01/56/f1b08a6275acc59e846fa4c1e1b65dbc1919f20157d9e66c20
      Running setup.py bdist_wheel for backcall ... done
      Stored in directory: /Users/hkob/Library/Caches/pip/wheels/98/b0/dd/29e28ff615af3dda4c67cab719dd51357597eabff926976b45
    Successfully built prometheus-client tornado pandocfilters backcall
    Installing collected packages: prometheus-client, tornado, ptyprocess, terminado, ipython-genutils, six, decorator, traitlets, jupyter-core, pyzmq, python-dateutil, jupyter-client, jsonschema, nbformat, Send2Trash, wcwidth, prompt-toolkit, pexpect, backcall, pygments, parso, jedi, appnope, pickleshare, ipython, ipykernel, pandocfilters, defusedxml, testpath, entrypoints, webencodings, bleach, MarkupSafe, jinja2, mistune, nbconvert, notebook, jupyterlab-server, jupyterlab
    Successfully installed MarkupSafe-1.1.0 Send2Trash-1.5.0 appnope-0.1.0 backcall-0.1.0 bleach-3.0.2 decorator-4.3.0 defusedxml-0.5.0 entrypoints-0.2.3 ipykernel-5.1.0 ipython-7.2.0 ipython-genutils-0.2.0 jedi-0.13.2 jinja2-2.10 jsonschema-2.6.0 jupyter-client-5.2.4 jupyter-core-4.4.0 jupyterlab-0.35.4 jupyterlab-server-0.2.0 mistune-0.8.4 nbconvert-5.4.0 nbformat-4.4.0 notebook-5.7.4 pandocfilters-1.4.2 parso-0.3.1 pexpect-4.6.0 pickleshare-0.7.5 prometheus-client-0.5.0 prompt-toolkit-2.0.7 ptyprocess-0.6.0 pygments-2.3.1 python-dateutil-2.7.5 pyzmq-17.1.2 six-1.12.0 terminado-0.8.1 testpath-0.4.2 tornado-5.1.1 traitlets-4.3.2 wcwidth-0.1.7 webencodings-0.5.1
  • jupyter-lab の config を作成する.
    $ jupyter-lab --generate-config
    Writing default config to: /Users/hkob/.jupyter/jupyter_notebook_config.py
  • password の文字列を作成する.この文字列を記録しておく.
    python3 -c 'from notebook.auth import passwd;print(passwd())'
  • $HOME/.jupyter/jupyter_notebook_config.py の設定を変更する.パスワードの部分には先ほど記録したパスワードを設定しておく.
    #  Local IP addresses (such as 127.0.0.1 and ::1) are allowed as local, along
    #  with hostnames configured in local_hostnames.
    c.NotebookApp.allow_remote_access = True
    
    ## The IP address the notebook server will listen on.
    c.NotebookApp.ip = '*'
    
    ## Whether to open in a browser after starting. The specific browser used is
    #  platform dependent and determined by the python standard library `webbrowser`
    #  module, unless it is overridden using the --browser (NotebookApp.browser)
    #  configuration option.
    c.NotebookApp.open_browser = False
    
    ## Hashed password to use for web authentication.
    #
    #  To generate, type in a python/IPython shell:
    #
    #    from notebook.auth import passwd; passwd()
    #
    #  The string should be of the form type:salt:hashed-password.
    c.NotebookApp.password = 'sha1:作成したパスワード'
Jupyter Lab の起動
  • 別の端末を開き,jupyter-lab を起動する.
    $ jupyter-lab
    [W 14:03:08.142 LabApp] WARNING: The notebook server is listening on all IP addresses and not using encryption. This is not recommended.
    [I 14:03:08.175 LabApp] JupyterLab extension loaded from /usr/local/lib/python3.7/site-packages/jupyterlab
    [I 14:03:08.175 LabApp] JupyterLab application directory is /usr/local/share/jupyter/lab
    [W 14:03:08.178 LabApp] JupyterLab server extension not enabled, manually loading...
    [I 14:03:08.185 LabApp] JupyterLab extension loaded from /usr/local/lib/python3.7/site-packages/jupyterlab
    [I 14:03:08.185 LabApp] JupyterLab application directory is /usr/local/share/jupyter/lab
    [I 14:03:08.186 LabApp] Serving notebooks from local directory: /Users/hkob/python3
    [I 14:03:08.186 LabApp] The Jupyter Notebook is running at:
    [I 14:03:08.187 LabApp] http://(rMBP.local or 127.0.0.1):8888/
    [I 14:03:08.187 LabApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
  • Safari 以外のブラウザで http://localhost:8888/ にアクセスする.もしポート番号を変えたければ,上記の jupyter_notebook_config.py に設定する.Notebook を開くと以下のような画面になる.IPython の実行結果がセルとして残されていく.
  • テキストファイルの編集も可能である.またその際に vim キーバインドを用いることが可能である.
  • ブラウザなので,iPad のブラウザからでもこの環境にアクセスすることが可能である.このため,高速な据え置きのサーバ側で Jupyter Lab を起動し,軽いクライアントでアクセスするという開発手法を取ることもできる.

長くなったので今日はここまで.