LoginSignup
28
19

More than 3 years have passed since last update.

Mac版Arduino IDE で M5Stackがビルドできない(import Serialエラー)

Last updated at Posted at 2020-12-08

まとめ

  • M5Paperをゲットし、MacでArduinoIDEのボードマネージャ(M5Stackライブラリ: v1.0.6)でM5Paperを選んでビルドしたところ、以下の問題に遭遇した。
    • Serial接続関連のパッケージがインストールされてないよ、と言う事らしい。
    • これはM5Stackライブラリに含まれるesptoolとMac環境の問題なので、このバージョンでM5Paper以外のボードを選択しても同じくビルドが通らなくなる。
  • Python3を何度かインストールし直したり、色々やった挙句、きれいに解決したのでここにメモしておく。
Traceback (most recent call last):
  File "/Users/nabeshin/Library/Arduino15/packages/m5stack/tools/esptool_py/3.0.0/esptool.py", line 39, in <module>
    import serial
ImportError: No module named serial
exit status 1
ボードM5Stack-Paperに対するコンパイル時にエラーが発生しました。

スクリーンショット 2020-12-06 23.07.50.png

実行環境

  • OS: MacOS Catalina
  • ArduinoIDE: v1.8.13
  • M5Stackライブラリ: v1.0.6
    • M5Paperが含まれているライブラリは以下のボードマネージャのURLから入手した。

スクリーンショット 2020-12-06 23.11.55.png

Python環境

nabeshin@iMacNabeshin ~ % python2

WARNING: Python 2.7 is not recommended. 
This version is included in macOS for compatibility with legacy software. 
Future versions of macOS will not include Python 2.7. 
Instead, it is recommended that you transition to using 'python3' from within Terminal.
  • MacOSでは、初めからPython2がインストールされているが、Python3への移行が推奨されているようなので別途python3をインストールしてある。
    • ~/.zprofile でpython3にパスを通し、デフォルトにしている。
nabeshin@iMacNabeshin ~ % env | grep PATH 
PATH=/Library/Frameworks/Python.framework/Versions/3.9/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
nabeshin@iMacNabeshin ~ % python --version
Python 3.9.0
nabeshin@iMacNabeshin ~ % 
  • python2で>>import serialを打って見たところ、ImportError: No module named serialとなる。
  • >pip install serialしてあるputhon3だとエラーにならない。

Arduino IDEは強制的にPython2を使ってしまう

  • シェルではバッチリpython3にパスが通っており、>>import serialも成功するが、Arduino IDEでビルドすると、どうしてもPython2が使われてしまう。

検証その1

  • 以下のようにpython3へのシンボリックリンクを張ってみたが、変わらず。。。
nabeshin@iMacNabeshin 3.0.0 % pwd
/Users/nabeshin/Library/Arduino15/packages/m5stack/tools/esptool_py/3.0.0
nabeshin@iMacNabeshin 3.0.0 % ls -la
total 7992
drwxr-xr-x  6 nabeshin  staff      192 12  7 21:37 .
drwxr-xr-x  4 nabeshin  staff      128 11 28 23:58 ..
-rw-r--r--@ 1 nabeshin  staff     6148 11 29 17:23 .DS_Store
-rwxr-xr-x  1 nabeshin  staff  3910331 11 28 21:47 esptool
-rwxr-xr-x@ 1 nabeshin  staff   169633 12  6 23:02 esptool.py
lrwxr-xr-x  1 nabeshin  staff       22 12  7 21:37 python -> /usr/local/bin/python3
nabeshin@iMacNabeshin 3.0.0 % 

検証その2

  • 以下のようにesptool.pyにpythonのバージョンをファイルに書き出すようにしてみた。

スクリーンショット 2020-12-06 23.07.02.png

  • 結果
    • だめだ、やはり2.7が強制的に使わされてしまう。。。
    • この呪縛から逃れる方法はないものか。。。
nabeshin@iMacNabeshin tmp % pwd
/Users/nabeshin/tmp
nabeshin@iMacNabeshin tmp % cat DEBUG_Version.txt
2.7.16 (default, Jun  5 2020, 22:59:21) 
[GCC 4.2.1 Compatible Apple LLVM 11.0.3 (clang-1103.0.29.20) (-macos10.15-objc-
nabeshin@iMacNabeshin tmp % 

と言うわけで、
なんとかしてpython2側にserialパッケージを追加しなければならない。と言うことがわかったが、
> pip2 install serial を素直にやりたくてもpip2がインストールできない。。。

解決方法

  • 全く同じ問題について、公式のM5Stack Communityで話されていた。
  • 解決した、とされている手順に沿って以下を実施。
  • これにより、Arduino IDEでもビルドできるようになった。
nabeshin@iMacNabeshin ~ % python2

WARNING: Python 2.7 is not recommended. 
This version is included in macOS for compatibility with legacy software. 
Future versions of macOS will not include Python 2.7. 
Instead, it is recommended that you transition to using 'python3' from within Terminal.

Python 2.7.16 (default, Jun  5 2020, 22:59:21) 
[GCC 4.2.1 Compatible Apple LLVM 11.0.3 (clang-1103.0.29.20) (-macos10.15-objc- on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print sys.path
['', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/Library/Python/2.7/site-packages', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC']
>>> ^D
nabeshin@iMacNabeshin ~ % sudo pip3 install --target /Library/Python/2.7/site-packages pyserial
Password:
WARNING: The directory '/Users/nabeshin/Library/Caches/pip' or its parent directory is not owned or is not writable by the current user. The cache has been disabled. Check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting pyserial
  Downloading pyserial-3.5-py2.py3-none-any.whl (90 kB)
     |████████████████████████████████| 90 kB 8.8 MB/s 
Installing collected packages: pyserial
Successfully installed pyserial-3.5
nabeshin@iMacNabeshin ~ % 






nabeshin@iMacNabeshin ~ % python2

WARNING: Python 2.7 is not recommended. 
This version is included in macOS for compatibility with legacy software. 
Future versions of macOS will not include Python 2.7. 
Instead, it is recommended that you transition to using 'python3' from within Terminal.

Python 2.7.16 (default, Jun  5 2020, 22:59:21) 
[GCC 4.2.1 Compatible Apple LLVM 11.0.3 (clang-1103.0.29.20) (-macos10.15-objc- on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import serial
>>> serial
<module 'serial' from '/Library/Python/2.7/site-packages/serial/__init__.py'>
>>> 

pip3からpython2側にパッケージを追加するコマンド

  • > sudo pip3 install --target /Library/Python/2.7/site-packages pyserial
    • なるほど、こうやって実行すればいいのか。
    • よく覚えておきます。
28
19
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
28
19