2012年5月14日月曜日

開発環境

『初めてのPython 第3版』(Mark Lutz 著、夏目 大 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-393-7) のI部(Pythonの基礎知識)のまとめ演習3(モジュールのインポート)を解いてみる。

3

モジュールファイルを別のディレクトリに移動して、同じように再度インポートしてみた場合、対話型セッションを終了せずにインポートする場合と対話型セッションを終了してから同じように再度インポートする場合は挙動は異なる。

コード(TextWrangler)

#!/usr/bin/env python
#encoding: utf-8

print("Hello module world!")

セッションを終了しなかった場合。

2度目のインポートではメッセージが表示されないみたい。

入出力結果(Terminal)

$ python
Python 3.2.3 (default, Apr 18 2012, 20:17:30) 
[GCC 4.2.1 Compatible Apple Clang 3.0 (tags/Apple/clang-211.12)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sample
Hello module world!
>>> import sample
>>> quit()
$

折衝を終了した場合。

2度目のインポートはエラーになる。

入出力結果(Terminal)

$ ls sample*
sample.py sample.pyc sample.txt
$ python
Python 3.2.3 (default, Apr 18 2012, 20:17:30) 
[GCC 4.2.1 Compatible Apple Clang 3.0 (tags/Apple/clang-211.12)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sample
Hello module world!
>>> quit()
$ mkdir sample_folder
$ mv sample.py sample_folder
$ ls sample*
sample.pyc sample.txt

sample_folder:
sample.py
$ python
Python 3.2.3 (default, Apr 18 2012, 20:17:30) 
[GCC 4.2.1 Compatible Apple Clang 3.0 (tags/Apple/clang-211.12)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sample
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: Bad magic number in sample.pyc
>>> quit()
$

も読み始めたから、Terminal、bashシェルの使い方も少しずつ覚えてきた。

0 コメント:

コメントを投稿