開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Python (プログラミング言語)
Learning Python (Mark Lutz (著)、Oreilly & Associates Inc)のPart Ⅰ.(Getting Started)、Test Your Knowledge: Part I Exercises、3. (Modules)を解いてみる。
その他参考書籍
3. (Modules)
コード(BBEdit)
module1.py
print('Hello module world!')
入出力結果(Terminal)
$ python3 Python 3.3.3 (default, Dec 2 2013, 01:40:21) [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import module1 Hello module world! >>> [1]+ Stopped python3.3 $ ls __pycache__ blog.html module1.py tmp $ ls -F __pycache__/ blog.html module1.py tmp/ $ mv module1.py tmp $ ls __pycache__ blog.html tmp $ fg python3.3 import imp import imp >>> imp.reload(module1) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/opt/local/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/imp.py", line 276, in reload module.__loader__.load_module(name) File "<frozen importlib._bootstrap>", line 584, in _check_name_wrapper File "<frozen importlib._bootstrap>", line 1022, in load_module File "<frozen importlib._bootstrap>", line 1003, in load_module File "<frozen importlib._bootstrap>", line 560, in module_for_loader_wrapper File "<frozen importlib._bootstrap>", line 853, in _load_module File "<frozen importlib._bootstrap>", line 948, in get_code File "<frozen importlib._bootstrap>", line 1041, in path_stats FileNotFoundError: [Errno 2] No such file or directory: './module1.py' >>> quit() $ ls __pycache__ blog.html tmp $ rm -rf __pycache__/ $ mv tmp/module1.py . $ ls blog.html module1.py tmp $ python Python 2.7.6 (default, Nov 18 2013, 15:12:51) [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import module1 Hello module world! >>> [1]+ Stopped python $ ls blog.html module1.py module1.pyc tmp $ mv module1.py tmp $ fg python reload(module1) reload(module1) Hello module world! <module 'module1' from 'module1.pyc'> >>> quit() $ ls blog.html module1.pyc tmp $ python3 Python 3.3.3 (default, Dec 2 2013, 01:40:21) [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import module1 Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: bad magic number in 'module1': b'\x03\xf3\r\n' >>> quit() $
python2.xとpython3.xでモジュールのリロードについて、方法(impモジュール、reload関数等)や挙動(作成するファイル__pycache__/(ディレクトリ)、module1.pyc)が違うことに注意。
0 コメント:
コメントを投稿