LoginSignup
33
8

More than 3 years have passed since last update.

JupyterやColabのマジックコマンドに対してセル内で定義した変数を引数として与える

Posted at

すごーーーーーーく下らないけど忘れそうだからQiitaにメモしておく

どういうシチュエーションのことを言っているのか?

JupyterやColaboratoryをいじっているとたまに%%!でマジックコマンドを実行したくなる時がある

しかもJupyter内で定義した変数を引数として渡したい時がある

今まではあまりなにも考えずに文字列をそのまま打ち込んでいたけど、shellのお作法にしたがって$をつければセルで定義した変数名の中に入っているオブジェクトをそのまま認識してくれることを今更知った

今更知って結構恥ずかしい

アンチパターン

チンパンジー的対処方法
# 無理やりファイル名を直で文字列として渡す!!
!python main.py --input_dir="/pathx/file.txt"
# 別パターン
%%gcs list --objects "gs://mypath"

良さげパターン

ホモ・サピエンス的対処方法
inputpath = "/pathx/file.txt"

!python main.py --input_dir=$inputpath
# 別パターン
gs_path = "gs://mypath"
%%gcs list --objects $gs_path

画面イメージも一応記載

スクリーンショット 2019-05-04 11.40.49.png

やっぱり、しょうもないな。。。この記事

おしまい

33
8
1

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
33
8