LoginSignup
9
5

More than 3 years have passed since last update.

IPython における「%run hoge.py」と「!python hoge.py」の違い

Last updated at Posted at 2019-05-07

TL;DR:

%run:
  .pyの中身をセルに展開して実行したみたいな挙動
  その後のセルの実行に影響する
!python:
  サブシェルでpython hoge.pyを実行
  その後のセルの実行には影響しない

経緯

Google Colaboratoryで.pyファイルの実行方法に

%run hoge.py
!python hoge.py

の2種類があり、どちらでも実行できる。
両者に違いがあるのかないのかハッキリさせたい。

検証

hoge.py
hoge = 256
piyo.py
piyo = 512
print(hoge)  # -> NameError
%run hoge.py
print(hoge)  # -> 256

print(piyo)  # -> NameError
!python piyo.py
print(piyo)  # -> NameError

結果:%runでは名前空間が取り込まれたが、!pythonはそうならない。

Google Colaboratoryで実験する
→ runPyFile.ipynb - Colaboratory

なぜ?

IPython公式ドキュメントの IPython as a system shell の節には

Since each command passed by IPython to the underlying system is executed in a subshell which exits immediately, you can NOT use !cd to navigate the filesystem.

と書かれている。

!で始める)シェルコマンドはサブシェルで実行されてすぐに破棄される、よって!cdではカレントディレクトリは変更されない。(意訳)

つまり、!pythonではpythonがサブシェルで実行されるため名前空間が共有されることはない。

対して、Built-in magic commands の節には%runについて

This is similar to running at a system prompt python file args, but with the advantage of giving you IPython’s tracebacks, and of loading all variables into your interactive namespace for further use (unless -p is used, see below).

と書かれている。

シェルでpython [.pyファイル] -引数...とやるのに似てるけど、トレースバックと名前空間のロードが付いてきてお得。(意訳)

つまり、%runでは.pyファイルで用意した変数が次のセル以降でも維持される。
逆に%run実行以前に定義していた変数は%runの実行で上書きされる。
-pオプションの挙動と変数の上書きは runPyFile.ipynb - Colaboratory でも実験できる)

9
5
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
9
5