Math Adventures with Python
An Illustrated Guide to Exploring Math with Code
楽天ブックス(Kobo)
紀伊国屋書店(Kinoppy)
開発環境
- macOS Mojave - Apple (OS)
- Emacs (Text Editor)
- Windows 10 Pro (OS)
- Visual Studio Code (Text Editor)
- Python 3.7 (プログラミング言語)
Math Adventures with Python: An Illustrated Guide to Exploring Math with Code (Peter Farrell(著)、No Starch Press)のPART Ⅰ(HITCHIN' UP YOUR PYTHON WAGON)、1(DRAWING POLYGONS WITH THE TURTLE MODULE)、EXERCISE1-1(SQUARE DANCE)の解答を求めてみる。
turtleモジュールの使用にちょっとつまずく。(環境はmacOS、python3のインストール方法はMacPorts)
入出力結果(cmd(コマンドプロンプト)、Terminal、Jupyter(IPython))
$ ipython
Python 3.7.2 (default, Dec 30 2018, 08:55:50)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.2.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: import turtle
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-ab23d5c8f022> in <module>
----> 1 import turtle
/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/turtle.py in <module>
105 # print(_ver)
106
--> 107 import tkinter as TK
108 import types
109 import math
/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/__init__.py in <module>
34 import sys
35
---> 36 import _tkinter # If this fails your Python may not be configured for Tk
37 TclError = _tkinter.TclError
38 from tkinter.constants import *
ModuleNotFoundError: No module named '_tkinter'
ということで、tkinterをmacportsで探してインストール。
入出力結果(cmd(コマンドプロンプト)、Terminal、Jupyter(IPython))
$ port search tkinter
py-tkinter @2.4.6_2 (python, graphics)
Python bindings to the Tk widget set
py26-tkinter @2.6.9 (python, graphics)
Python bindings to the Tk widget set
py27-tkinter @2.7.15 (python, graphics)
Python bindings to the Tk widget set
py31-tkinter @2.5.6_1 (python)
Obsolete port, replaced by py34-tkinter
py32-tkinter @3.2.6_2 (python, graphics)
Python bindings to the Tk widget set
py33-tkinter @3.3.7_2 (python, graphics)
Python bindings to the Tk widget set
py34-tkinter @3.4.9 (python, graphics)
Python bindings to the Tk widget set
py35-tkinter @3.5.6 (python, graphics)
Python bindings to the Tk widget set
py36-tkinter @3.6.8 (python, graphics)
Python bindings to the Tk widget set
py37-tkinter @3.7.2 (python, graphics)
Python bindings to the Tk widget set
pypy-tkinter @6.0.0 (python, graphics)
PyPy bindings to the Tk widget set
pypy3-tkinter @6.0.0 (python, graphics)
PyPy bindings to the Tk widget set
Found 12 ports.
$ sudo port install py37-tkinter
...
$
python3.7を使用してるからpy37-tkinterをインストール。インストール時にtkも必要だった。これで大丈夫かと思ったら再びエラー。
入出力結果(cmd(コマンドプロンプト)、Terminal、Jupyter(IPython))
TclError: couldn't connect to display "[ここはコンピューター名].local"
py37-tkinterインストール時に依存関係でインストールしたtkのvariantsを調べる。
$ port variants tk
tk has the variants:
quartz: Enable native macOS graphics support
* conflicts with x11
universal: Build for multiple architectures
[+]x11: Enable X11 support
* conflicts with quartz
$
tkinterインストール時に、macOSの標準のGUIの描画コアエンジンquartzではなくx11(X Windows System)がデフォルトでインストールされるみたい。x11(X Window System)をインストールした記憶がなかったし(もしかしたら他のソフトの依存関係でインストールしたかも)、quartzに変更すればいいのかもと思い、quartzの方をインストール。x11はquartzと衝突するみたいだし、macOSだからせっかくならquartzを使いたいから削除。
$ sudo port uninstall tk ... $ sudo port install tk +quartz ... $
これで動いて問題解決した。ということで本題に。
コード
Python 3
#!/usr/bin/env python3
import time
import turtle
# おまけ
turtle.shape('turtle')
turtle.forward(200)
turtle.right(90)
turtle.forward(200)
turtle.right(90)
turtle.forward(200)
turtle.right(90)
turtle.forward(200)
# すぐにWindowが閉じないように
time.sleep(10)
入出力結果(cmd(コマンドプロンプト)、Terminal、Jupyter(IPython))
$ ipython
Python 3.7.2 (default, Dec 30 2018, 08:55:50)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.2.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: import turtle
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-ab23d5c8f022> in <module>
----> 1 import turtle
/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/turtle.py in <module>
105 # print(_ver)
106
--> 107 import tkinter as TK
108 import types
109 import math
/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/__init__.py in <module>
34 import sys
35
---> 36 import _tkinter # If this fails your Python may not be configured for Tk
37 TclError = _tkinter.TclError
38 from tkinter.constants import *
ModuleNotFoundError: No module named '_tkinter'

0 コメント:
コメントを投稿