<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>「magic commands」タグの記事一覧Python Tech</title>
	<atom:link href="https://tech.nkhn37.net/tag/magic-commands/feed/" rel="self" type="application/rss+xml" />
	<link>https://tech.nkhn37.net</link>
	<description>Python学習サイト</description>
	<lastBuildDate>Sun, 26 Oct 2025 07:08:25 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1.1</generator>

<image>
	<url>https://tech.nkhn37.net/wp-content/uploads/2021/01/cropped-lion-normal-clear-1-32x32.png</url>
	<title>「magic commands」タグの記事一覧Python Tech</title>
	<link>https://tech.nkhn37.net</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>【Python】IPython ~拡張された対話型インタプリタ~</title>
		<link>https://tech.nkhn37.net/python-ipython-basic/</link>
					<comments>https://tech.nkhn37.net/python-ipython-basic/#respond</comments>
		
		<dc:creator><![CDATA[naoki-hn]]></dc:creator>
		<pubDate>Sun, 14 Feb 2021 00:00:00 +0000</pubDate>
				<category><![CDATA[IPython]]></category>
		<category><![CDATA[%history]]></category>
		<category><![CDATA[%quickref]]></category>
		<category><![CDATA[magic commands]]></category>
		<guid isPermaLink="false">https://tech.nkhn37.net/?p=1035</guid>

					<description><![CDATA[Pythonの拡張された対話型インタプリタであるIPythonの使い方について紹介します。 IPython Pythonはインタプリタ型の言語で、他のC言語等のコンパイルが必要な言語と異なり、コンパイルを必要とせず順次プ [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">Pythonの拡張された対話型インタプリタである<span class="marker"><strong>IPython</strong></span>の使い方について紹介します。</p>



<h2 class="wp-block-heading jinr-heading d--bold" id="ipython">IPython</h2>



<p class="wp-block-paragraph">Pythonはインタプリタ型の言語で、他のC言語等のコンパイルが必要な言語と異なり、コンパイルを必要とせず順次プログラミングを実行することができます。</p>



<p class="wp-block-paragraph">IPythonは、<span class="marker"><strong>対話型インタプリタを強力に拡張したもの</strong></span>となっており、こちらの<a rel="noreferrer noopener" href="http://ipython.org/" target="_blank">IPython公式ページ</a>にも「IPython provides a rich architecture for interactive computing」と記載があります。</p>



<p class="wp-block-paragraph">様々な拡張機能があるので細かくは<a href="https://ipython.readthedocs.io/en/stable/" target="_blank" rel="noreferrer noopener">IPython公式ドキュメント</a>を参照してください。この記事ではIPythonの一部についてご紹介します。</p>



<section class="wp-block-jinr-blocks-iconbox b--jinr-block b--jinr-iconbox"><div class="d--simple-iconbox5 ">
			<i class="jif jin-ifont-v2speaker" aria-hidden="true"></i>
			<div class="a--jinr-iconbox">
<p class="wp-block-paragraph">Jupyter Notebook や Jupyter Lab における「Jupyter」は IPython の共通部分のコンポーネントのことで、グラフィカルに利用できるようにしたのが「Jupyter Notebook」です。Jupyter Lab は、Jupyter Notebook を、さらに拡張してデータ表示ツールなどを組み込んだものです。</p>
</div>
		</div></section>



<h3 class="wp-block-heading jinr-heading d--bold" id="ipythonのインストール">IPythonのインストール</h3>



<h4 class="wp-block-heading jinr-heading d--bold" id="anaconda">Anacondaの場合</h4>



<p class="wp-block-paragraph">Anacondaを開発環境として使用している場合は、IPythonはインストールされているので特にインストールをする必要はありません。</p>



<p class="wp-block-paragraph">Anacondaのインストールは「<a rel="noreferrer noopener" href="https://tech.nkhn37.net/python-anaconda-install/" target="_blank">Anacondaのインストール方法（Windows）</a>」や「<a rel="noreferrer noopener" href="https://tech.nkhn37.net/python-anaconda-install-linux/" target="_blank">Anacondaのインストール方法（Linux）</a>」を参考にしてください。</p>



<h4 class="wp-block-heading jinr-heading d--bold" id="pipによるインストール">pipによるインストール</h4>



<p class="wp-block-paragraph">Anaconda環境ではない場合には、個別にIPythonのインストールします。以下のpipコマンドによりIPythonをインストールしてください。</p>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">pip install ipython</pre>



<h3 class="wp-block-heading jinr-heading d--bold" id="ipythonの使用方法">IPythonの使用方法</h3>



<h4 class="wp-block-heading jinr-heading d--bold" id="コンソールからの起動">コンソールからの起動</h4>



<p class="wp-block-paragraph">コマンドプロンプト等のコンソールから起動する場合には「<span class="marker"><strong>ipython</strong></span>」とタイプして実行してもらえば以下のようにIPythonが起動します。</p>



<p class="wp-block-paragraph">以下はAnaconda Promptからipythonを実行した例です。</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img fetchpriority="high" decoding="async" width="772" height="239" src="https://tech.nkhn37.net/wp-content/uploads/2021/02/image-33.png" alt="IPython" class="wp-image-1044" srcset="https://tech.nkhn37.net/wp-content/uploads/2021/02/image-33.png 772w, https://tech.nkhn37.net/wp-content/uploads/2021/02/image-33-300x93.png 300w, https://tech.nkhn37.net/wp-content/uploads/2021/02/image-33-768x238.png 768w" sizes="(max-width: 772px) 100vw, 772px" /></figure>
</div>


<h4 class="wp-block-heading jinr-heading d--bold" id="pycharmでのipythonの利用">PyCharmでのIPythonの利用</h4>



<p class="wp-block-paragraph">私はPyCharmを開発環境として好んで使用していますが、同じようにPyCharmを利用している人であれば、画面左下に「<span class="marker"><strong>Python Console</strong></span>」と書かれたタブがあると思います。</p>



<p class="wp-block-paragraph">ここをクリックすると、IPythonが起動しますのでPyCharm上でIPythonを使用してプログラムを実行することが可能です。</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="1024" height="347" src="https://tech.nkhn37.net/wp-content/uploads/2021/02/image-29-1024x347.png" alt="iPython PyCharm" class="wp-image-1040" srcset="https://tech.nkhn37.net/wp-content/uploads/2021/02/image-29-1024x347.png 1024w, https://tech.nkhn37.net/wp-content/uploads/2021/02/image-29-300x102.png 300w, https://tech.nkhn37.net/wp-content/uploads/2021/02/image-29-768x260.png 768w, https://tech.nkhn37.net/wp-content/uploads/2021/02/image-29.png 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<h3 class="wp-block-heading jinr-heading d--bold" id="ipythonの特徴">IPythonの特徴</h3>



<p class="wp-block-paragraph">IPythonは基本的にpythonのインタプリタ機能を使う場合と同じように使えます。IPythonは様々な拡張機能があるので、特徴的な部分の一部だけを紹介しようと思います。細かな点については、<a rel="noreferrer noopener" href="https://ipython.readthedocs.io/en/stable/" target="_blank">IPython公式ドキュメント</a>を参照してください。</p>



<h4 class="wp-block-heading jinr-heading d--bold" id="補完機能">補完機能</h4>



<p class="wp-block-paragraph">IPythonを使用するとメソッドの補完機能を使用することができます。</p>



<p class="wp-block-paragraph">例えば、mathモジュールをインポートして使おうと思った時にどんな関数があったかを確認したくなった場合には、「math.」までタイプしてTabキーを押すと候補の一覧が表示されます。</p>



<p class="wp-block-paragraph">キーボードの上下で選択して反映することができるので便利です。</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="979" height="512" src="https://tech.nkhn37.net/wp-content/uploads/2021/02/image-34.png" alt="IPython" class="wp-image-1045" srcset="https://tech.nkhn37.net/wp-content/uploads/2021/02/image-34.png 979w, https://tech.nkhn37.net/wp-content/uploads/2021/02/image-34-300x157.png 300w, https://tech.nkhn37.net/wp-content/uploads/2021/02/image-34-768x402.png 768w" sizes="(max-width: 979px) 100vw, 979px" /></figure>
</div>


<h4 class="wp-block-heading jinr-heading d--bold" id="実行履歴参照と修正が容易">実行履歴参照と修正が容易</h4>



<p class="wp-block-paragraph">通常のPythonのインタプリタで過去の実行履歴を参照して実行する場合には、キーボードの「↑」キーを押すことで戻って実行をすることが可能です。</p>



<p class="wp-block-paragraph">しかし、この場合は1行ずつしか戻ることができないため、例えばfor文やif文のようなブロックを実行した時には、順番にまた上から実行する必要があります。</p>



<p class="wp-block-paragraph">IPythonでは「↑」キーを押すとfor等のブロックはまとめて履歴表示されて、かつ行の修正や追加が可能です。</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="1024" height="557" src="https://tech.nkhn37.net/wp-content/uploads/2021/02/image-35-1024x557.png" alt="IPython" class="wp-image-1046" srcset="https://tech.nkhn37.net/wp-content/uploads/2021/02/image-35-1024x557.png 1024w, https://tech.nkhn37.net/wp-content/uploads/2021/02/image-35-300x163.png 300w, https://tech.nkhn37.net/wp-content/uploads/2021/02/image-35-768x418.png 768w, https://tech.nkhn37.net/wp-content/uploads/2021/02/image-35.png 1239w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<h4 class="wp-block-heading jinr-heading d--bold" id="マジックコマンド-magic-commands">マジックコマンド（magic commands） %~</h4>



<p class="wp-block-paragraph">上の例では、実行履歴をキーボードの「↑」で戻って確認してきましたが、過去自分がどのようなプログラムをしてきたかを確認をしたくなった場合には、以下のように「<span class="marker"><strong>%history</strong></span>」と記載して実行することで履歴を確認することができます。</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="613" height="236" src="https://tech.nkhn37.net/wp-content/uploads/2021/02/image-36.png" alt="IPython" class="wp-image-1048" srcset="https://tech.nkhn37.net/wp-content/uploads/2021/02/image-36.png 613w, https://tech.nkhn37.net/wp-content/uploads/2021/02/image-36-300x115.png 300w" sizes="(max-width: 613px) 100vw, 613px" /></figure>
</div>


<p class="wp-block-paragraph">このように<span class="marker"><strong>%</strong></span>から始まるコマンドのことを<span class="marker"><strong>マジックコマンド（magic commands）</strong></span>と言います。</p>



<p class="wp-block-paragraph">マジックコマンドの詳細については、公式ドキュメントの<a rel="noreferrer noopener" href="https://ipython.readthedocs.io/en/stable/interactive/magics.html#built-in-magic-commands" target="_blank">Built-in magic commands</a>のページを参照してもらうのが詳しいですが、どのようなマジックコマンドがあるかを手軽に確認したい場合には<span class="marker"><strong>%quickref</strong></span>で一覧を参照することができます。</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="738" height="341" src="https://tech.nkhn37.net/wp-content/uploads/2021/02/image-37.png" alt="IPython" class="wp-image-1049" srcset="https://tech.nkhn37.net/wp-content/uploads/2021/02/image-37.png 738w, https://tech.nkhn37.net/wp-content/uploads/2021/02/image-37-300x139.png 300w" sizes="(max-width: 738px) 100vw, 738px" /></figure>
</div>


<p class="wp-block-paragraph">長いので途中を省略しますが、下へ進めてみていくと以下のように使用可能なコマンドの一覧を知ることができます。</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="738" height="453" src="https://tech.nkhn37.net/wp-content/uploads/2021/02/image-38.png" alt="IPython" class="wp-image-1051" srcset="https://tech.nkhn37.net/wp-content/uploads/2021/02/image-38.png 738w, https://tech.nkhn37.net/wp-content/uploads/2021/02/image-38-300x184.png 300w" sizes="(max-width: 738px) 100vw, 738px" /></figure>
</div>


<p class="wp-block-paragraph">まずは%quickrefでコマンドでどのようなものがあるかを調べてから具体的な使い方を<a rel="noreferrer noopener" href="https://ipython.readthedocs.io/en/stable/interactive/magics.html#built-in-magic-commands" target="_blank">Built-in magic commands</a>のページで確認するようにするとよいかなと思います。</p>



<p class="wp-block-paragraph">IPythonで試しているコードをファイルを保存する<span class="marker"><strong>%save</strong></span>や、Pythonファイルのコードを読み込む<span class="marker"><strong>%load</strong></span>等、色々と便利なコマンドがあるので必要に応じて調べてみるとよいと思います。</p>



<h4 class="wp-block-heading jinr-heading d--bold" id="メソッド情報を参照する">メソッド情報を参照する</h4>



<p class="wp-block-paragraph">メソッドの情報を参照したい場合には、以下のように「<span class="marker"><strong>?</strong></span>」をメソッドの末尾に加えて実行することでメソッド情報を表示することができます。</p>



<figure class="wp-block-image size-large"><img decoding="async" width="738" height="314" src="https://tech.nkhn37.net/wp-content/uploads/2021/02/image-44.png" alt="IPython" class="wp-image-1071" srcset="https://tech.nkhn37.net/wp-content/uploads/2021/02/image-44.png 738w, https://tech.nkhn37.net/wp-content/uploads/2021/02/image-44-300x128.png 300w" sizes="(max-width: 738px) 100vw, 738px" /></figure>



<p class="wp-block-paragraph">また、「<span class="marker"><strong>??</strong></span>」をメソッドの末尾に加えて実行すると、対象メソッドのソースを確認できます。</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="738" height="349" src="https://tech.nkhn37.net/wp-content/uploads/2021/02/image-40.png" alt="IPython" class="wp-image-1055" srcset="https://tech.nkhn37.net/wp-content/uploads/2021/02/image-40.png 738w, https://tech.nkhn37.net/wp-content/uploads/2021/02/image-40-300x142.png 300w" sizes="(max-width: 738px) 100vw, 738px" /></figure>
</div>


<p class="wp-block-paragraph">対話型インタプリタで各種メソッドを確認しながらコーディングを進めるときには便利な機能です。</p>



<h4 class="wp-block-heading jinr-heading d--bold" id="osのコマンドを実行する">OSのコマンドを実行する</h4>



<p class="wp-block-paragraph">通常のPythonのインタプリタを使用している場合、Windows等のOSのコマンドを使用しようとすると一度インタプリタを抜ける必要があります。</p>



<p class="wp-block-paragraph">しかし、IPythonを使用すると以下のように「<span class="marker"><strong>!</strong></span>」を付けることで、IPythonのインタプリタ上でOSのコマンドを実行することができます。</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="738" height="259" src="https://tech.nkhn37.net/wp-content/uploads/2021/02/image-42.png" alt="IPython" class="wp-image-1057" srcset="https://tech.nkhn37.net/wp-content/uploads/2021/02/image-42.png 738w, https://tech.nkhn37.net/wp-content/uploads/2021/02/image-42-300x105.png 300w" sizes="(max-width: 738px) 100vw, 738px" /></figure>
</div>


<h2 class="wp-block-heading jinr-heading d--bold">まとめ</h2>



<p class="wp-block-paragraph">Pythonの拡張された対話型インタプリタである<span class="marker"><strong>IPython</strong></span>の使い方について紹介しました。</p>



<p class="wp-block-paragraph">今回紹介した機能もIPythonの機能の一例で、他にも色々な機能があるので<a rel="noreferrer noopener" href="https://ipython.readthedocs.io/en/stable/" target="_blank">IPython公式ドキュメント</a>を参照してもらい色々と試してもらえるとよいかと思います。</p>


<section class="b--jinr-block b--jinr-blogcard d--blogcard-hover-up d--blogcard-style1 d--blogcard-mysite t--round "><div class="a--blogcard-label ef">あわせて読みたい</div><a class="o--blogcard-link t--round" href="https://tech.nkhn37.net/python-tech-summary-page/"><div class="c--blogcard-image"><img decoding="async" class="a--blogcard-img-src" width="128" height="72" src="https://tech.nkhn37.net/wp-content/uploads/2024/08/Python-Tech-Pythonプログラミングガイド_new1-640x360.jpg" alt="【Python Tech】プログラミングガイド" /></div><div class="a--blogcard-title d--bold">【Python Tech】プログラミングガイド</div></a></section>]]></content:encoded>
					
					<wfw:commentRss>https://tech.nkhn37.net/python-ipython-basic/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Disk: Enhanced  を使用したページ キャッシュ

Served from: tech.nkhn37.net @ 2026-09-18 23:34:51 by W3 Total Cache
-->