-1

having trouble with this. fairly new to programming so would appreciate assistance. it works fine when running .py file, but when i try and make it into a .exe and run it (using pyinstaller) it doesnt work (using command pyinstaller --onefile converter.py).

i tried reinstalling all modules used but didnt make any difference. wondering if it is an issue with pyinstaller not copying the files, but not sure how to fix... python 3.11 if it is relevant code and error message below:

`import tkinter as tk
from pytube import YouTube, Playlist
import os

HEIGHT = 500
WIDTH = 600

def downloadSingle(videoURL):
    yt = YouTube(videoURL, use_oauth=True)

    video = yt.streams.filter(only_audio=True).first()

    destination = "C:\\Users\\myname\\Downloads\\music"
    
    outFile = video.download(output_path=destination)
    
    base, ext = os.path.splitext(outFile)
    newFile = base + '.mp3'
    
    os.rename(outFile, newFile)

    print(yt.title + " has been successfully downloaded.")

def downloadPlaylist():
    playlist = Playlist(input("URL of playlist: "))
    for singleURL in playlist.video_urls:
        downloadSingle(singleURL)

def singleButtonClicked(entry):
    downloadSingle(entry)

def playlistButtonClicked(entry1):
    downloadPlaylist(entry1)

root = tk.Tk()

canvas = tk.Canvas(root, height=HEIGHT, width=WIDTH)
canvas.pack()

titleCanvas = tk.Canvas(root, bg="#80c1ff", bd=5)
titleCanvas.create_text(130, 10, text="YTMP3 Converter", font=("Arial", 20), anchor="n")
titleCanvas.place(relx=0.5, rely=0.1, relwidth=0.75, relheight=0.1, anchor="n")

frame = tk.Frame(root, bg="#80c1ff", bd=5)
frame.place(relx=0.5, rely=0.3, relwidth=0.75, relheight=0.1, anchor="n")

entry = tk.Entry(frame, font=("Arial", 12))
entry.insert(0, "Enter Video URL")
entry.bind("<FocusIn>", lambda args: entry.delete("0", "end"))
entry.place(relwidth=0.65, relheight=1)

button = tk.Button(frame, text="Download Single", font=("Arial", 12), command=lambda: singleButtonClicked(entry.get()))
button.place(relx=0.7, relwidth=0.3, relheight=1)

frame1 = tk.Frame(root, bg="#80c1ff", bd=5)
frame1.place(relx=0.5, rely=0.5, relwidth=0.75, relheight=0.1, anchor="n")

entry1 = tk.Entry(frame1, font=("Arial", 12))
entry1.insert(0, "Enter Playlist URL")
entry1.bind("<FocusIn>", lambda args: entry1.delete("0", "end"))
entry1.place(relwidth=0.65, relheight=1)

button1 = tk.Button(frame1, text="Download Playlist", font=("Arial", 12), command=lambda: playlistButtonClicked(entry1.get()))
button1.place(relx=0.7, relwidth=0.3, relheight=1)

root.mainloop()`

error message:

`Exception in Tkinter callback
Traceback (most recent call last):
  File "tkinter\__init__.py", line 1948, in __call__
  File "YTMP3_Converter.py", line 52, in <lambda>
  File "YTMP3_Converter.py", line 30, in singleButtonClicked
  File "YTMP3_Converter.py", line 11, in downloadSingle
  File "pytube\__main__.py", line 296, in streams
  File "pytube\__main__.py", line 176, in fmt_streams
  File "pytube\__main__.py", line 157, in streaming_data
  File "pytube\__main__.py", line 246, in vid_info
  File "pytube\innertube.py", line 300, in player
  File "pytube\innertube.py", line 239, in _call_api
  File "pytube\innertube.py", line 200, in fetch_bearer_token
  File "pytube\innertube.py", line 121, in cache_tokens
FileNotFoundError: [WinError 3] The system cannot find the path specified: 'C:\\Users\\myname\\AppData\\Local\\Temp\\_MEI224762\\pytube\\__cache__'`
thing
  • 1
  • 1
    If the problem is that the `.exe` raises that error (a `pytube` file/folder can't be found), it means that the problem is in how `pyinstaller` creates the `exe`. If so, please remove the `tkinter` tag from the question. – TheLizzard Jan 04 '23 at 14:36

1 Answers1

0

The root cause of this issue is because the packaged pytube is trying to access cache folders and files that have been deleted by the system. We can fix this by explicitly specifying the cache folder.

from pytube import innertube
innertube._cache_dir = os.path.join(os.getcwd(), "cache")
innertube._token_file = os.path.join(innertube._cache_dir, 'tokens.json')

This solution is mentioned in the following webpage. https://github.com/pytube/pytube/issues/1322

Khaos
  • 1
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 08 '23 at 06:18