[Python]PyAutoGUIのインストールで躓いた話

スポンサーリンク

Pythonでスクリーンショットを保存したく、PyAutoGUIをインストールしようとしたところエラーが発生し、解決に至るまでやったことをメモします。

論理的なことや正確さは考慮していないため、 必要ない操作がある可能性はあります。

※ただ、Selenium動作中にスクリーンショットを取得したかったので、別の方法でスクリーンショットを保存するようにしました。

参考

背景

画層の生成を行いたく、以下のコードを実行しようとしました。

例として、「01t.py」というファイルに以下のコードを実装。

import pyautogui
s = pyautogui.screenshot()
s.save('screenshot1.png')

実行したときのエラーと対応

$ python 01t.py
Traceback (most recent call last):
  File 01t.py, line 1, in <module>
    import pyautogui
ImportError: No module named pyautogui

→pyautoguiがない、といわれる。
 なので、インストールする

pyautoguiのインストール

$ pip install pyautogui
Collecting pyautogui
  Downloading PyAutoGUI-0.9.33.zip (55kB)
    100% |████████████████████████████████| 61kB 2.9MB/s
Collecting pymsgbox (from pyautogui)
  Downloading PyMsgBox-1.0.3.zip
Collecting PyTweening>=1.0.1 (from pyautogui)
  Downloading PyTweening-1.0.3.zip
Collecting Pillow (from pyautogui)
  Downloading Pillow-3.3.1-cp27-cp27m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (3.1MB)
    100% |████████████████████████████████| 3.1MB 399kB/s
Collecting pyscreeze (from pyautogui)
  Downloading PyScreeze-0.1.8.zip
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
      File <string>, line 1, in <module>
      File /private/var/folders/2_/9vv1s2tn69l24fg7jh1zlvfr0000gn/T/pip-build-QmlBhA/pyscreeze/setup.py, line 6, in <module>
        version=__import__('pyscreeze').__version__,
      File pyscreeze/__init__.py, line 21, in <module>
        from PIL import Image
    ImportError: No module named PIL
    ----------------------------------------
Command python setup.py egg_info failed with error code 1 in /private/var/folders/2_/9vv1s2tn69l24fg7jh1zlvfr0000gn/T/pip-build-QmlBhA/pyscreeze/
</module></module></module></string>

→PILがない、といわれる。
 まあ、言われた通りインストールする

PILのインストール

$ sudo pip install PIL --allow-external PIL --allow-unverified PIL
Password:
DEPRECATION: --allow-external has been deprecated and will be removed in the future. Due to changes in the repository protocol, it no longer has any effect.
DEPRECATION: --allow-unverified has been deprecated and will be removed in the future. Due to changes in the repository protocol, it no longer has any effect.
The directory '/Users/User/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/User/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting PIL
  Could not find a version that satisfies the requirement PIL (from versions: )
No matching distribution found for PIL

→みつかんない???
 この状態でpyautoguiを使おうとしても状況は変わらなかった。
グーグルでいろいろ調べて試してみる。。。

pipのアップデート

$ pip install --upgrade pip
Requirement already up-to-date: pip in /Library/Python/2.7/site-packages/pip-8.1.2-py2.7.egg

→しかし、結果は最新だった

Pillow(Python Imaging Library)のインストール

$ pip install -t lib pillow
Collecting pillow
  Using cached Pillow-3.3.1-cp27-cp27m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl
Installing collected packages: pillow
Successfully installed pillow

Imageのインストール(これ必要??)

$ sudo pip install Image
Password:
The directory '/Users/User/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/User/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting Image
  Downloading image-1.5.4.tar.gz
Collecting pillow (from Image)
  Downloading Pillow-3.3.1-cp27-cp27m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (3.1MB)
    100% |████████████████████████████████| 3.1MB 390kB/s
Collecting django (from Image)
  Downloading Django-1.10.1-py2.py3-none-any.whl (6.8MB)
    100% |████████████████████████████████| 6.8MB 200kB/s
Installing collected packages: pillow, django, Image
  Running setup.py install for Image ... done
Successfully installed Image-1.5.4 django-1.10.1 pillow-3.3.1

再度、pyautoguiのインストール(これも必要??)

$ sudo pip install pyautogui
The directory '/Users/User/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/User/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting pyautogui
  Downloading PyAutoGUI-0.9.33.zip (55kB)
    100% |████████████████████████████████| 61kB 3.0MB/s
Collecting pymsgbox (from pyautogui)
  Downloading PyMsgBox-1.0.3.zip
Collecting PyTweening>=1.0.1 (from pyautogui)
  Downloading PyTweening-1.0.3.zip
Requirement already satisfied (use --upgrade to upgrade): Pillow in /Library/Python/2.7/site-packages (from pyautogui)
Collecting pyscreeze (from pyautogui)
  Downloading PyScreeze-0.1.8.zip
Installing collected packages: pymsgbox, PyTweening, pyscreeze, pyautogui
  Running setup.py install for pymsgbox ... done
  Running setup.py install for PyTweening ... done
  Running setup.py install for pyscreeze ... done
  Running setup.py install for pyautogui ... done
Successfully installed PyTweening-1.0.3 pyautogui-0.9.33 pymsgbox-1.0.3 pyscreeze-0.1.8

再度、実行

$ python 01t.py
$

→なんかうまくいった!??
 無事、画像が生成された。
 ※ログ出力とかはしていないです。

コメント

タイトルとURLをコピーしました