My .py file works perfectly, but when I turn it to .exe it fails

  Kiến thức lập trình

I’m trying to code an app that lets you download many videos (or audios) from Youtube at the same time using Python, specifically the libraries tkinter and pytube. When I run it as a .py file, everything goes perfect. However, when I turn it into a .exe (using auto-py-to-exe), there’s a problem, although everything else works, the “Download” Button does absolutely nothing.

I want to clarify, when I press the “Downlaod” button no error pops up, it just doesn’t work.

Here’s the whole code.

# importing packages 
from pytube import YouTube 
import tkinter as tk
from tkinter import filedialog, messagebox

WIDTH = "500"
HEIGHT = "300"

dir = ""
url_list = []

root = tk.Tk()
root.geometry(WIDTH + "x" + HEIGHT)
root.resizable(False, True)
root.title("Youtube Downloader")

audio = tk.BooleanVar()
single_song = tk.BooleanVar()

def download(url, dir, audio):
    yt = YouTube(url=url)
    if audio.get(): 
        video = yt.streams.filter(only_audio=True).first()
    else:
        video = yt.streams.get_highest_resolution()
    video.download(output_path=dir) 

    if single_song.get():
        messagebox.showinfo("Done", yt.title + " has been successfully downloaded.")

def change_value(var):
    if var:
        var = False
    else:
        var = True

def get_dir():
    global dir  
    dir = filedialog.askdirectory()

def add(url):
    global url_list
    url_list.append(url)

def display():    
    global url_list
    for child in frm_disp.winfo_children():
        child.destroy()
    for url in url_list:
        label = tk.Label(frm_disp, text=url)
        label.pack()

def clear():
    global url_list
    url_list = []
    for child in frm_disp.winfo_children():
        child.destroy()

def download_all(dir, audio):
    global url_list 
    if len(url_list) == 0:
        messagebox.showwarning("URL","Please write a URL")
        return
    if dir == "":
        messagebox.showwarning("Directory", "Please select a directory")
        return
    
    for url in url_list:
        download(url, dir, audio)

    messagebox.showinfo("Done", "All songs downloaded correctly")

frm = tk.Frame(root, width=480, height=480)
frm.pack()

lbl = tk.Label(root, text="List of songs to be donwloaded (Press "Display" to refresh):", pady=10)
lbl.pack()

frm_disp = tk.Frame(root, width=480, height=100, highlightthickness=2,highlightbackground="black")
frm_disp.pack()

btsingle_song = tk.Checkbutton(root, variable=single_song, text="Warn when each song has been downloaded")
btsingle_song.pack()

guide = tk.Label(frm, text="Write the URL of the Youtube video:")
guide.grid(row=0, column=0, columnspan=3,pady=10)

bturl = tk.Entry(frm, width=75)
bturl.grid(row=1, column=0, columnspan=3)

dwn = tk.Button(frm, command= lambda: download_all(dir, audio), width=15, text="Download")
dwn.grid(row=2, column=1)

btdir = tk.Button(frm, command=get_dir, width=15, text="Directory")
btdir.grid(row=2, column=0)

btadd = tk.Button(frm, command= lambda: add(bturl.get()), width=15, text="Add")
btadd.grid(row=3, column=0)

disp = tk.Button(frm, command=display, width=15, text="Display")
disp.grid(row=3, column=1)

clr = tk.Button(frm, command=clear, width=15, text="Clear")
clr.grid(row=3, column=2)

only_audio = tk.Checkbutton(frm, text="Only audio (as mp3)", variable=audio)
only_audio.grid(row=2, column=2)

root.update()
root.mainloop()


I have tried to attach the folders in auto-py-to-exe to see if the modules wwere the problem, but It didn’t work. I’ve also tried running it in a virtual environment that has pytube installed (as it is not installed on my machine, it is installed on an env), but it didn’t work either.

As the only problem is the “Download” button I’ve thought that maybe pytube is the problem, but I’ve got no idea.

LEAVE A COMMENT