• home
  • about
  • 全ての投稿
  • ソフトウェア・ハードウェアの設定のまとめ
  • 分析関連のまとめ
  • ヘルスケア関連のまとめ
  • 生涯学習関連のまとめ

jupyter magic commands

date: 2023-12-16 excerpt: jupyter magic commandsの使い方

tag: jupyterkagglepython


jupyter magic commandsの使い方

概要

  • pythonのプログラムとは別にjupyter独自のコマンドがある
  • セルの先頭に%, %%をつけることで実行できる

代表的なマジックコマンドの使用例

%%time, %time

  • %time
    • 次の行の実行時間を測定する
  • %%time
    • セル全体の実行時間を測定する

%env

  • 環境変数を設定する
%env OPENAI_API_KEY=xxxxxxxxxxxxxxxxxxxx

%%writefile <output-name>

  • セルに記述するとセルの内容がファイルとして出力される
  • e.g.
    • %% writefile a.py
      • a.pyで出力する

%%bash

  • セルの最初に記述するとセルがshell scriptになる

%watermark

  • 実行しているos, マシンスペック, python, jupyterの環境を表示するmagic
%pip install watermark # 必要ならば
%load_ext watermark
%watermark
Python implementation: CPython
Python version       : 3.10.12
IPython version      : 7.34.0

Compiler    : GCC 11.4.0
OS          : Linux
Release     : 6.1.58+
Machine     : x86_64
Processor   : x86_64
CPU cores   : 2
Architecture: 64bit

%autoreloadの使い方

  • 自作したpythonモジュール等をimportたびに更新するmagic
%load_ext autoreload
%autoreload 2


jupyterkagglepython Share Tweet