[python]下載YouTube影片模組更新

參考網址:python黑洞網

執行以下程式時會出現錯誤
from pytube import YouTube

yt = YouTube('https://www.youtube.com/watch?v=27ob2G3GUCQ')
& C:/ProgramData/Anaconda3/python.exe d:/as/python/book/ch08/pytube1.py
Traceback (most recent call last):
  File "d:/as/python/book/ch08/pytube1.py", line 3, in <module>
    yt = YouTube('https://www.youtube.com/watch?v=27ob2G3GUCQ')
  File "C:\ProgramData\Anaconda3\lib\site-packages\pytube\__main__.py", line 102, in __init__
    self.prefetch()
  File "C:\ProgramData\Anaconda3\lib\site-packages\pytube\__main__.py", line 196, in prefetch
    self.vid_info_raw = request.get(self.vid_info_url)
  File "C:\ProgramData\Anaconda3\lib\site-packages\pytube\request.py", line 42, in get
    return _execute_request(url, headers=extra_headers).read().decode("utf-8")

 

原因是pytube的版本太老舊了

解決方法
 

進cmd安裝git
conda install git

pip uninstall pytube3
pip install git+https://github.com/nficano/pytube

 

安裝完新的pytube版本,打包的exe檔執行時會出現以下錯誤
Traceback (most recent call last):
  File "tkdownload.py", line 27, in <module>
ModuleNotFoundError: No module named 'pytube'
[1172] Failed to execute script tkdownload

google了一下在這篇文章中可以找到答案
https://yunchipang.github.io/create-exe-from-python-script-using-pyinstaller.html

文中提及
「因為第一次打包時error msg有提醒我我需要cmath這個module,所以在hiddenimports的部分有特別加上。」

將hiddenimports=[‘pytube’]加入tkdownload.spec
再做一次pyinstaller -F tkdownload.spec打包出來的exe就可以正常執行了

本篇發表於 程式設計。將永久鏈結加入書籤。