-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathappendix.html
More file actions
254 lines (233 loc) · 20.1 KB
/
Copy pathappendix.html
File metadata and controls
254 lines (233 loc) · 20.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="ja">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>16. 付録 — Python 3.6.5 ドキュメント</title>
<link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '../',
VERSION: '3.6.5',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true,
SOURCELINK_SUFFIX: '.txt'
};
</script>
<script type="text/javascript" src="../_static/jquery.js"></script>
<script type="text/javascript" src="../_static/underscore.js"></script>
<script type="text/javascript" src="../_static/doctools.js"></script>
<script type="text/javascript" src="../_static/translations.js"></script>
<script type="text/javascript" src="../_static/sidebar.js"></script>
<link rel="search" type="application/opensearchdescription+xml"
title="Python 3.6.5 ドキュメント 内を検索"
href="../_static/opensearch.xml"/>
<link rel="author" title="このドキュメントについて" href="../about.html" />
<link rel="index" title="索引" href="../genindex.html" />
<link rel="search" title="検索" href="../search.html" />
<link rel="copyright" title="Copyright" href="../copyright.html" />
<link rel="next" title="Python のセットアップと利用" href="../using/index.html" />
<link rel="prev" title="15. 浮動小数点演算、その問題と制限" href="floatingpoint.html" />
<link rel="shortcut icon" type="image/png" href="../_static/py.png" />
<link rel="canonical" href="https://docs.python.jp/3/tutorial/appendix.html" /><script type="text/javascript">
$(document).ready(function() {
var base = 'https://docs.python.org/3.6/tutorial/appendix.html';
$('a.headerlink').each(function() {
var html = '<a href="' + base + $(this).attr('href') +
'" title="原文へのリンク"><small>(原文)</small></a>';
$(this).after(html);
})
});
</script>
<script type="text/javascript" src="../_static/copybutton.js"></script>
<script type="text/javascript" src="../_static/version_switch.js"></script>
</head>
<body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>ナビゲーション</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="総索引"
accesskey="I">索引</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Pythonモジュール索引"
>モジュール</a> |</li>
<li class="right" >
<a href="../using/index.html" title="Python のセットアップと利用"
accesskey="N">次へ</a> |</li>
<li class="right" >
<a href="floatingpoint.html" title="15. 浮動小数点演算、その問題と制限"
accesskey="P">前へ</a> |</li>
<li><img src="../_static/py.png" alt=""
style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="https://www.python.org/">Python</a> »</li>
<li>
<span class="version_switcher_placeholder">3.6.5</span>
<a href="../index.html">ドキュメント</a> »
</li>
<li class="nav-item nav-item-1"><a href="index.html" accesskey="U">Python チュートリアル</a> »</li>
<li class="right">
<div class="inline-search" style="display: none" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="Quick search" type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
<script type="text/javascript">$('.inline-search').show(0);</script>
|
</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="appendix">
<span id="tut-appendix"></span><h1>16. 付録<a class="headerlink" href="#appendix" title="このヘッドラインへのパーマリンク">¶</a></h1>
<div class="section" id="interactive-mode">
<span id="tut-interac"></span><h2>16.1. 対話モード<a class="headerlink" href="#interactive-mode" title="このヘッドラインへのパーマリンク">¶</a></h2>
<div class="section" id="error-handling">
<span id="tut-error"></span><h3>16.1.1. エラー処理<a class="headerlink" href="#error-handling" title="このヘッドラインへのパーマリンク">¶</a></h3>
<p>エラーが発生すると、インタプリタはエラーメッセージとスタックトレース (stack trace) を出力します。対話モードにいるときは、インタプリタは一次プロンプトに戻ります; スクリプトをファイルから実行しているときは、インタプリタはスタックトレースを出力した後、非ゼロの終了ステータスで終了します。 (<a class="reference internal" href="../reference/compound_stmts.html#try"><code class="xref std std-keyword docutils literal"><span class="pre">try</span></code></a> 文の <a class="reference internal" href="../reference/compound_stmts.html#except"><code class="xref std std-keyword docutils literal"><span class="pre">except</span></code></a> 節で処理された例外は、ここでいうエラーにはあたりません。) いくつかのエラーは常に致命的であり、非ゼロの終了ステータスとなるプログラムの終了を引き起こします。例えばインタプリタ内部の矛盾やある種のメモリ枯渇が当てはまります。エラーメッセージは全て標準エラー出力に書き込まれます; これに対して、通常は実行した命令から出力される内容は標準出力に書き込まれます。</p>
<p>割り込み文字 (interrupt character、普通は <code class="kbd docutils literal"><span class="pre">Control-C</span></code> か <code class="kbd docutils literal"><span class="pre">Delete</span></code>) を一次または二次プロンプトに対してタイプすると、入力が取り消されて一次プロンプトに戻ります。 <a class="footnote-reference" href="#id2" id="id1">[1]</a> コマンドの実行中に割り込み文字をタイプすると <a class="reference internal" href="../library/exceptions.html#KeyboardInterrupt" title="KeyboardInterrupt"><code class="xref py py-exc docutils literal"><span class="pre">KeyboardInterrupt</span></code></a> 例外が送出されます。この例外は <a class="reference internal" href="../reference/compound_stmts.html#try"><code class="xref std std-keyword docutils literal"><span class="pre">try</span></code></a> 文で処理できます。</p>
</div>
<div class="section" id="executable-python-scripts">
<span id="tut-scripts"></span><h3>16.1.2. 実行可能な Python スクリプト<a class="headerlink" href="#executable-python-scripts" title="このヘッドラインへのパーマリンク">¶</a></h3>
<p>BSD 風の Unix システムでは、Python スクリプトはシェルスクリプトのように直接実行可能にできます。これを行うには、以下の行</p>
<div class="highlight-python3"><div class="highlight"><pre><span></span><span class="ch">#!/usr/bin/env python3.5</span>
</pre></div>
</div>
<p>(ここではインタプリタがユーザの <span class="target" id="index-4"></span><code class="xref std std-envvar docutils literal"><span class="pre">PATH</span></code> 上にあると仮定しています) をスクリプトの先頭に置き、スクリプトファイルに実行可能モードを設定します。 <code class="docutils literal"><span class="pre">#!</span></code> はファイルの最初の2文字でなければなりません。プラットフォームによっては、この最初の行を終端する改行文字が Windows 形式 (<code class="docutils literal"><span class="pre">'\r\n'</span></code>) ではなく、 Unix形式(<code class="docutils literal"><span class="pre">'\n'</span></code>)でなければならないことがあります。ハッシュまたはポンド文字、すなわち <code class="docutils literal"><span class="pre">'#'</span></code> は、Python ではコメントを書き始めるために使われていることに注意してください。</p>
<p><strong class="program">chmod</strong> コマンドを使えば、スクリプトに実行モードや実行権限を与えることができます。</p>
<div class="highlight-shell-session"><div class="highlight"><pre><span></span><span class="gp">$</span> chmod +x myscript.py
</pre></div>
</div>
<p>Windows では、"実行モード" のような概念はありません。Python のインストーラーは自動的に <code class="docutils literal"><span class="pre">.py</span></code> ファイルを <code class="docutils literal"><span class="pre">python.exe</span></code> に関連付けるので、Python ファイルをダブルクリックするとそれをスクリプトとして実行します。<code class="docutils literal"><span class="pre">.pyw</span></code> 拡張子も(訳注: <code class="docutils literal"><span class="pre">pythonw.exe</span></code> に)関連付けられ、通常コンソールウィンドウを抑制して実行します。</p>
</div>
<div class="section" id="the-interactive-startup-file">
<span id="tut-startup"></span><h3>16.1.3. 対話モード用の起動時実行ファイル<a class="headerlink" href="#the-interactive-startup-file" title="このヘッドラインへのパーマリンク">¶</a></h3>
<p>Python を対話的に使うときには、インタプリタが起動する度に実行される何らかの標準的なコマンドがあると便利なことがよくあります。これを行うには、 <span class="target" id="index-5"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONSTARTUP"><code class="xref std std-envvar docutils literal"><span class="pre">PYTHONSTARTUP</span></code></a> と呼ばれる環境変数を、インタプリタ起動時に実行されるコマンドが入ったファイル名に設定します。この機能は Unix シェルの <code class="file docutils literal"><span class="pre">.profile</span></code> に似ています。</p>
<p>このファイルは対話セッションのときだけ読み出されます。 Python がコマンドをスクリプトから読み出しているときや、 <code class="file docutils literal"><span class="pre">/dev/tty</span></code> がコマンドの入力元として明示的に指定されている(この場合対話的セッションのように動作します) <em>わけではない</em> 場合にはこのファイルは読み出されません。ファイル内のコマンドは、対話的コマンドが実行される名前空間と同じ名前空間内で実行されます。このため、ファイル内で定義されていたり import されたオブジェクトは、そのまま対話セッション内で使うことができます。また、このファイル内で <code class="docutils literal"><span class="pre">sys.ps1</span></code> や <code class="docutils literal"><span class="pre">sys.ps2</span></code> を変更して、プロンプトを変更することもできます。</p>
<p>もし現在のディレクトリから追加でスタートアップファイルを読み出したいのなら、グローバルのスタートアップファイルの中に <code class="docutils literal"><span class="pre">if</span> <span class="pre">os.path.isfile('.pythonrc.py'):</span> <span class="pre">exec(open('.pythonrc.py').read())</span></code> のようなプログラムを書くことができます。スクリプト中でスタートアップファイルを使いたいのなら、以下のようにしてスクリプト中で明示的に実行しなければなりません:</p>
<div class="highlight-python3"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">os</span>
<span class="n">filename</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'PYTHONSTARTUP'</span><span class="p">)</span>
<span class="k">if</span> <span class="n">filename</span> <span class="ow">and</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isfile</span><span class="p">(</span><span class="n">filename</span><span class="p">):</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span> <span class="k">as</span> <span class="n">fobj</span><span class="p">:</span>
<span class="n">startup_file</span> <span class="o">=</span> <span class="n">fobj</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
<span class="n">exec</span><span class="p">(</span><span class="n">startup_file</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="the-customization-modules">
<span id="tut-customize"></span><h3>16.1.4. カスタマイズ用モジュール<a class="headerlink" href="#the-customization-modules" title="このヘッドラインへのパーマリンク">¶</a></h3>
<p>Python はユーザーが Python をカスタマイズするための2つのフック、 <code class="xref py py-mod docutils literal"><span class="pre">sitecustomize</span></code> と <code class="xref py py-mod docutils literal"><span class="pre">usercustomize</span></code> を提供しています。これがどのように動作しているかを知るには、まずはユーザーの site-packages ディレクトリの場所を見つける必要があります。Python を起動して次のコードを実行してください:</p>
<div class="highlight-python3"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">site</span>
<span class="gp">>>> </span><span class="n">site</span><span class="o">.</span><span class="n">getusersitepackages</span><span class="p">()</span>
<span class="go">'/home/user/.local/lib/python3.5/site-packages'</span>
</pre></div>
</div>
<p><code class="file docutils literal"><span class="pre">usercustomize.py</span></code> をそのディレクトリに作成して、そこでやりたいことをすべて書くことができます。このファイルは自動インポートを無効にする <a class="reference internal" href="../using/cmdline.html#cmdoption-s"><code class="xref std std-option docutils literal"><span class="pre">-s</span></code></a> オプションを使わない限り、全ての Python の起動時に実行されます。</p>
<p><code class="xref py py-mod docutils literal"><span class="pre">sitecustomize</span></code> モジュールも同じように動作しますが、一般的にコンピューターの管理者によって、グローバルの site-packages ディレクトリに作成され、 <code class="xref py py-mod docutils literal"><span class="pre">usercustomize</span></code> より先にインポートされます。詳細は <a class="reference internal" href="../library/site.html#module-site" title="site: Module responsible for site-specific configuration."><code class="xref py py-mod docutils literal"><span class="pre">site</span></code></a> モジュールのドキュメントを参照してください。</p>
<p class="rubric">脚注</p>
<table class="docutils footnote" frame="void" id="id2" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id1">[1]</a></td><td>GNU Readline パッケージに関する問題のせいで妨げられることがあります。</td></tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h3><a href="../contents.html">目次</a></h3>
<ul>
<li><a class="reference internal" href="#">16. 付録</a><ul>
<li><a class="reference internal" href="#interactive-mode">16.1. 対話モード</a><ul>
<li><a class="reference internal" href="#error-handling">16.1.1. エラー処理</a></li>
<li><a class="reference internal" href="#executable-python-scripts">16.1.2. 実行可能な Python スクリプト</a></li>
<li><a class="reference internal" href="#the-interactive-startup-file">16.1.3. 対話モード用の起動時実行ファイル</a></li>
<li><a class="reference internal" href="#the-customization-modules">16.1.4. カスタマイズ用モジュール</a></li>
</ul>
</li>
</ul>
</li>
</ul>
<h4>前のトピックへ</h4>
<p class="topless"><a href="floatingpoint.html"
title="前の章へ">15. 浮動小数点演算、その問題と制限</a></p>
<h4>次のトピックへ</h4>
<p class="topless"><a href="../using/index.html"
title="次の章へ">Python のセットアップと利用</a></p>
<div role="note" aria-label="source link">
<h3>このページ</h3>
<ul class="this-page-menu">
<li><a href="../bugs.html">バグ報告</a></li>
<li>
<a href="https://github.com/python/cpython/blob/3.6/Doc/tutorial/appendix.rst"
rel="nofollow">ソースの表示
</a>
</li>
</ul>
</div>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>ナビゲーション</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="総索引"
>索引</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Pythonモジュール索引"
>モジュール</a> |</li>
<li class="right" >
<a href="../using/index.html" title="Python のセットアップと利用"
>次へ</a> |</li>
<li class="right" >
<a href="floatingpoint.html" title="15. 浮動小数点演算、その問題と制限"
>前へ</a> |</li>
<li><img src="../_static/py.png" alt=""
style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="https://www.python.org/">Python</a> »</li>
<li>
<span class="version_switcher_placeholder">3.6.5</span>
<a href="../index.html">ドキュメント</a> »
</li>
<li class="nav-item nav-item-1"><a href="index.html" >Python チュートリアル</a> »</li>
<li class="right">
<div class="inline-search" style="display: none" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="Quick search" type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
<script type="text/javascript">$('.inline-search').show(0);</script>
|
</li>
</ul>
</div>
<div class="footer">
© <a href="../copyright.html">Copyright</a> 2001-2018, Python Software Foundation.
<br />
Python Software Foundation は非営利団体です。
<a href="https://www.python.org/psf/donations/">寄付</a>
<br />
最終更新日時: 7月 29, 2018
<a href="../bugs.html">バグを見つけたら</a>?
<br />
このドキュメントは <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.6.5 を使って作成されました。
</div>
</body>
</html>