2012年7月29日日曜日

開発環境

『初めてのPython 第3版』(Mark Lutz 著、夏目 大 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-393-7) のV部(パッケージインポート)の20章(パッケージインポートとインポート)練習問題3を解いてみる。

3.

パッケージインポートするディレクトリパス中の各ディレクトリに__init__.pyが必要。

コード(TextWrangler)

sample.py

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

a = 10
b = "Python"
def f():
 print("Hello, Python!")

入出力結果(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.
>>> from sample_folder.sample_folder.sample import *
>>> a
10
>>> b
'Python'
>>> f()
Hello, Python!
>>> quit()
$ rm sample_folder/__init__.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.
>>> from sample_folder.sample_folder.sample import *
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named sample_folder.sample_folder.sample
>>> quit()
$

0 コメント:

コメントを投稿