2017年4月24日 星期一

在 Windows 上離線安裝 PyAutoGUI 的方法

我上週在明儀買了這本書 "Python 自動化的樂趣 (碁峰, H&C 譯)" :
在第 18 章看到如何利用 PyAutoGUI 函式庫控制視窗介面以達成自動化, 剛好同事工作上遇到需在指定時間去按某個應用程式上的一個按鈕並進行存檔動作, 詢問有無可能將此工作自動化, 雖然我可以用 AutoIt 來做, 但我想嘗試看看是否 Python 也能解決此問題, 所以就先在自己電腦上安裝 PyAutoGUI 來玩玩看.

其實在 Windows 環境只要下 pip3 install pyautogui 指令即可下載安裝此函式庫, 但公司有些電腦因為資安考量無法連外, 可能需要離線安裝. 我先在網路連線的電腦下載 PyAutoGUI 的 gz 壓縮檔, 先試試看離線安裝行不行, 可以的話再把檔案複製到無網路連線的工作電腦安裝 :

#  https://pypi.python.org/pypi/PyAutoGUI

我把下載的檔案 PyAutoGUI-0.9.36.tar.gz 放在 D:\Python 下, 然後用 pip3 指令安裝, 結果竟然失敗, 雖然這台電腦可連 Internet, 但是看起來似乎 PyAutoGUI 有相依套件, 在下載這些套件時出問題了 :

D:\>pip3 install d:\Python\PyAutoGUI-0.9.36.tar.gz
Processing d:\python\pyautogui-0.9.36.tar.gz
Collecting pymsgbox (from PyAutoGUI==0.9.36)
  Retrying (Retry(total=4, connect=None, read=None, redirect=None)) after connec
tion broken by 'ConnectTimeoutError(<pip._vendor.requests.packages.urllib3.conne
ction.VerifiedHTTPSConnection object at 0x00000000050561D0>, 'Connection to pypi
.python.org timed out. (connect timeout=15)')': /simple/pymsgbox/
  Retrying (Retry(total=3, connect=None, read=None, redirect=None)) after connec
tion broken by 'ConnectTimeoutError(<pip._vendor.requests.packages.urllib3.conne
ction.VerifiedHTTPSConnection object at 0x00000000050567B8>, 'Connection to pypi
.python.org timed out. (connect timeout=15)')': /simple/pymsgbox/
  Retrying (Retry(total=2, connect=None, read=None, redirect=None)) after connec
tion broken by 'ConnectTimeoutError(<pip._vendor.requests.packages.urllib3.conne
ction.VerifiedHTTPSConnection object at 0x00000000050565C0>, 'Connection to pypi
.python.org timed out. (connect timeout=15)')': /simple/pymsgbox/
  Retrying (Retry(total=1, connect=None, read=None, redirect=None)) after connec
tion broken by 'ConnectTimeoutError(<pip._vendor.requests.packages.urllib3.conne
ction.VerifiedHTTPSConnection object at 0x0000000005056A20>, 'Connection to pypi
.python.org timed out. (connect timeout=15)')': /simple/pymsgbox/
  Retrying (Retry(total=0, connect=None, read=None, redirect=None)) after connec
tion broken by 'ConnectTimeoutError(<pip._vendor.requests.packages.urllib3.conne
ction.VerifiedHTTPSConnection object at 0x0000000005056D68>, 'Connection to pypi
.python.org timed out. (connect timeout=15)')': /simple/pymsgbox/
  Could not find a version that satisfies the requirement pymsgbox (from PyAutoG
UI==0.9.36) (from versions: )
No matching distribution found for pymsgbox (from PyAutoGUI==0.9.36)

我猜可能是被公司防火牆擋住了 (但我安裝 Python 其他套件都沒問題啊, 奇怪), 參考 :

# Cannot install PyAutoGUI - receiving error

好吧, 那我就直接安裝看看, 但下面兩個指令結果都跟上面一樣 :

D:\Python\>pip install pyautogui
D:\Python\>pip3 install pyautogui

但改用我自己的筆電用手機上網, 以離線方式安裝並不會出現上面 "Retrying" 問題, 可以順利安裝, 所以確實是被防火牆擋住了沒錯. 當然若能在線安裝是最好不過了, 下面是我在自己的筆電直接安裝 :

E:\>pip3 install pyautogui
Collecting pyautogui
  Downloading PyAutoGUI-0.9.36.tar.gz (46kB)Collecting pymsgbox (from pyautogui)
  Downloading PyMsgBox-1.0.6.zip
Collecting PyTweening>=1.0.1 (from pyautogui)
  Downloading PyTweening-1.0.3.zip
Collecting Pillow (from pyautogui)
  Downloading Pillow-4.1.0-cp36-cp36m-win_amd64.whl (1.5MB)
Collecting pyscreeze (from pyautogui)
  Downloading PyScreeze-0.1.9.tar.gz
Collecting olefile (from Pillow->pyautogui)
  Downloading olefile-0.44.zip (74kB)Installing collected packages: pymsgbox, PyTweening, olefile, Pillow, pyscreeze, pyautogui
  Running setup.py install for pymsgbox ... done
  Running setup.py install for PyTweening ... done
  Running setup.py install for olefile ... done
  Running setup.py install for pyscreeze ... done
  Running setup.py install for pyautogui ... done
Successfully installed Pillow-4.1.0 PyTweening-1.0.3 olefile-0.44 pyautogui-0.9.36 pymsgbox-1.0.6 pyscreeze-0.1.9

觀察上面安裝程序, 可見在安裝 PyAutoGUI 之前其實會先下載安裝另外五個相依套件 : pymsgbox, PyTweening, olefile, Pillow, pyscreeze, 然後才安裝 pyautogui, 用 pip3 list 指令可以看到這六個套件均已安裝完成 :

E:\>pip3 list
DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.
beautifulsoup4 (4.5.3)
cycler (0.10.0)
matplotlib (2.0.0)
numpy (1.12.1+mkl)
olefile (0.44)
pandas (0.19.2)
Pillow (4.1.0)
pip (9.0.1)
PyAutoGUI (0.9.36)
PyMsgBox (1.0.6)
pyparsing (2.2.0)
PyScreeze (0.1.9)
python-dateutil (2.6.0)
PyTweening (1.0.3)
pytz (2017.2)
requests (2.13.0)
scikit-learn (0.18.1)
scipy (0.19.0)
setuptools (28.8.0)
six (1.10.0)
virtualenv (15.1.0)

因此若要離線安裝 PyAutoGUI 必須先下載安裝這五個相依的套件, 下載點如下 :

https://pypi.python.org/pypi/PyMsgBox
https://pypi.python.org/pypi/PyTweening
# https://pypi.python.org/pypi/olefile
https://pypi.python.org/pypi/Pillow/
https://pypi.python.org/pypi/PyScreeze

下載後逐一安裝這五個套件 :

D:\Python>pip install d:\python\pyautogui\PyMsgBox-1.0.6.zip
Processing d:\python\pyautogui\pymsgbox-1.0.6.zip
Installing collected packages: PyMsgBox
  Running setup.py install for PyMsgBox ... done
Successfully installed PyMsgBox-1.0.6

哇, 下錯指令了, 應該用 pip3 才對 :

D:\Python>pip3 install d:\python\pyautogui\PyMsgBox-1.0.6.zip
Processing d:\python\pyautogui\pymsgbox-1.0.6.zip
  Requirement already satisfied (use --upgrade to upgrade): PyMsgBox==1.0.6 from
 file:///D:/python/pyautogui/PyMsgBox-1.0.6.zip in c:\python36\lib\site-packages

因為已經安裝 pip2 了, 所以應該用 --upgrade 選項 :

D:\Python>pip3 install d:\python\pyautogui\PyMsgBox-1.0.6.zip --upgrade
Processing d:\python\pyautogui\pymsgbox-1.0.6.zip
Installing collected packages: PyMsgBox
  Found existing installation: PyMsgBox 1.0.6
    Uninstalling PyMsgBox-1.0.6:
      Successfully uninstalled PyMsgBox-1.0.6
  Running setup.py install for PyMsgBox ... done
Successfully installed PyMsgBox-1.0.6

其次安裝 PyTweening :

D:\Python>pip3 install d:\python\pyautogui\PyTweening-1.0.3.zip
Processing d:\python\pyautogui\pytweening-1.0.3.zip
Installing collected packages: PyTweening
  Running setup.py install for PyTweening ... done
Successfully installed PyTweening-1.0.3

接著安裝 olefile :

D:\Python>pip3 install d:\python\pyautogui\olefile-0.44.zip
Processing d:\python\pyautogui\olefile-0.44.zip
Installing collected packages: olefile
  Running setup.py install for olefile ... done
Successfully installed olefile-0.44

然後安裝 PyScreeze, 結果又出現 "Retrying" 了, 原來是應該先安裝 Pillow 才對 :

D:\Python>pip3 install d:\python\pyautogui\PyScreeze-0.1.9.tar.gz
Processing d:\python\pyautogui\pyscreeze-0.1.9.tar.gz
Collecting Pillow (from PyScreeze==0.1.9)
  Retrying (Retry(total=4, connect=None, read=None, redirect=None)) after connec
tion broken by 'ConnectTimeoutError(<pip._vendor.requests.packages.urllib3.conne
ction.VerifiedHTTPSConnection object at 0x00000000047DB5F8>, 'Connection to pypi
.python.org timed out. (connect timeout=15)')': /simple/pillow/
  Retrying (Retry(total=3, connect=None, read=None, redirect=None)) after connec
tion broken by 'ConnectTimeoutError(<pip._vendor.requests.packages.urllib3.conne
ction.VerifiedHTTPSConnection object at 0x00000000047DBCC0>, 'Connection to pypi
.python.org timed out. (connect timeout=15)')': /simple/pillow/
  Retrying (Retry(total=2, connect=None, read=None, redirect=None)) after connec
tion broken by 'ConnectTimeoutError(<pip._vendor.requests.packages.urllib3.conne
ction.VerifiedHTTPSConnection object at 0x00000000047DBA20>, 'Connection to pypi
.python.org timed out. (connect timeout=15)')': /simple/pillow/
  Retrying (Retry(total=1, connect=None, read=None, redirect=None)) after connec
tion broken by 'ConnectTimeoutError(<pip._vendor.requests.packages.urllib3.conne
ction.VerifiedHTTPSConnection object at 0x00000000047DBC50>, 'Connection to pypi
.python.org timed out. (connect timeout=15)')': /simple/pillow/
  Retrying (Retry(total=0, connect=None, read=None, redirect=None)) after connec
tion broken by 'ConnectTimeoutError(<pip._vendor.requests.packages.urllib3.conne
ction.VerifiedHTTPSConnection object at 0x0000000005050F98>, 'Connection to pypi
.python.org timed out. (connect timeout=15)')': /simple/pillow/
  Could not find a version that satisfies the requirement Pillow (from PyScreeze
==0.1.9) (from versions: )
No matching distribution found for Pillow (from PyScreeze==0.1.9)

先安裝 Pillow :

D:\Python>pip3 install d:\python\pyautogui\Pillow-4.1.0-cp36-cp36m-win_amd64.whl

Processing d:\python\pyautogui\pillow-4.1.0-cp36-cp36m-win_amd64.whl
Requirement already satisfied: olefile in c:\python36\lib\site-packages (from Pi
llow==4.1.0)
Installing collected packages: Pillow
Successfully installed Pillow-4.1.0

這樣就可以順利安裝 PyScreeze 了 :

D:\Python>pip3 install d:\python\pyautogui\PyScreeze-0.1.9.tar.gz
Processing d:\python\pyautogui\pyscreeze-0.1.9.tar.gz
Requirement already satisfied: Pillow in c:\python36\lib\site-packages (from PyS
creeze==0.1.9)
Requirement already satisfied: olefile in c:\python36\lib\site-packages (from Pi
llow->PyScreeze==0.1.9)
Installing collected packages: PyScreeze
  Running setup.py install for PyScreeze ... done
Successfully installed PyScreeze-0.1.9

OK, 萬事俱備只欠東風, 相依套件都有了之後就可以安裝 PyAutoGUI 了 :

D:\Python>pip3 install d:\python\pyautogui\PyAutoGUI-0.9.36.tar.gz
Processing d:\python\pyautogui\pyautogui-0.9.36.tar.gz
Requirement already satisfied: pymsgbox in c:\python36\lib\site-packages (from P
yAutoGUI==0.9.36)
Requirement already satisfied: PyTweening>=1.0.1 in c:\python36\lib\site-package
s (from PyAutoGUI==0.9.36)
Requirement already satisfied: Pillow in c:\python36\lib\site-packages (from PyA
utoGUI==0.9.36)
Requirement already satisfied: pyscreeze in c:\python36\lib\site-packages (from
PyAutoGUI==0.9.36)
Requirement already satisfied: olefile in c:\python36\lib\site-packages (from Pi
llow->PyAutoGUI==0.9.36)
Installing collected packages: PyAutoGUI
  Running setup.py install for PyAutoGUI ... done
Successfully installed PyAutoGUI-0.9.36

最後用 pip3 list 查詢已安裝套件清單 :

D:\Python>pip3 list
DEPRECATION: The default format will switch to columns in the future. You can us
e --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.con
f under the [list] section) to disable this warning.
beautifulsoup4 (4.5.3)
cycler (0.10.0)
Django (1.8.18)
matplotlib (2.0.0)
numpy (1.12.1+mkl)
olefile (0.44)
pandas (0.19.2)
Pillow (4.1.0)
pip (9.0.1)
py2exe (0.9.2.0)
PyAutoGUI (0.9.36)
PyMsgBox (1.0.6)
pyparsing (2.2.0)
PyScreeze (0.1.9)
python-dateutil (2.6.0)
PyTweening (1.0.3)
pytz (2017.2)
requests (2.13.0)
scikit-learn (0.18.1)
scipy (0.19.0)
setuptools (28.8.0)
six (1.10.0)
virtualenv (15.1.0)

然後到 IDLE 介面匯入 pyautogui 函式庫, 果然可順利匯入了 : 

Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import pyautogui
Traceback (most recent call last):
  File "
    import pyautogui
ModuleNotFoundError: No module named 'pyautogui'  (尚未安裝 PyAutoGUI)

>>> import pyautogui
>>>   (已安裝 PyAutoGUI)

以上便是今日安裝 PyAutoGUI 的全紀錄, 供日後參考.


4 則留言 :

Sony 提到...

你好,請問可否傳送 PyAutoGUI-0.9.36.tar.gz 的檔案給我的因為新版的PyAutoGUI 需要額外的套件 (PyGetWindow),而新需要的套件安裝上出現錯誤不能使用,麻煩你,謝謝。

小狐狸事務所 提到...

Dear Sony,

找到了, 我放在 Dropbox :

https://www.dropbox.com/s/1rvxlwzyrmxjyav/PyAudio-0.2.11-cp36-cp36m-win_amd64.whl?dl=0

小狐狸事務所 提到...

Sorry, 我弄錯了, 正確連結如下 :

https://www.dropbox.com/s/ff4n6am2pt3q6wr/PyAutoGUI-0.9.36.tar.gz?dl=0

Sony 提到...

你好Tony,

已成功安裝你提供的套件,謝謝。