0

I was trying to make a YouTube mp3-mp4 downloader to practice python. But I'm getting this error always. I tried something but nothing was helpful. I tried using "global YouTube" but I don't know what is the true usage. I downloaded pytube before downloading pytubex I wonder if it affects the code. I checked other peoples codes and I couldn't see the difference.

from pytube import *
from distutils import command, extension
from tkinter import *
from tkinter import filedialog
app = Tk()
app.title("Youtube MP3-MP4 Downloader")
app.geometry("350x450")

def mp4Button():
    directory1= filedialog.askdirectory()
    url= urlEntry.get()
    yt= YouTube(url)
    video = yt.streams.first()
    video.download(directory1)  

def youtube1():
    button.destroy()
    urlEntry.get()
    url = urlEntry.get()
    mp4= Button(app,text="mp4",command=mp4Button)
    mp4.pack()
    mp4.place(x=100,y=75)
    mp3= Button(app,text="mp3")
    mp3.pack()
    mp3.place(x=100,y=100)
    

button = Button(app,text="Search",justify="center",command=youtube1)
button.pack()
button.place(x=100,y=75)

urlEntry= Entry(app,text="Enter video url",justify="center")
urlEntry.pack(fill=BOTH,ipady=10,padx=18,pady=5)
urlEntry.focus()

pathlabel = Label(app)
pathlabel.pack()

app.mainloop()

Output:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1884, in __call__
    return self.func(*args)
  File "c:\pythonP\youtube mp4 mp3\pytube.py", line 12, in mp4Button
    yt= YouTube(url)
NameError: name 'YouTube' is not defined

How can I fix that I can't understand what is wrong

Thanks in advance

new error

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1884, in __call__
    return self.func(*args)
  File "c:\pythonP\youtube mp4 mp3\ytdownloader1.py", line 12, in mp4Button
    yt= YouTube(url)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\pytube\__main__.py", line 91, in __init__        
    self.prefetch()
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\pytube\__main__.py", line 181, in prefetch       
    self.vid_info_raw = request.get(self.vid_info_url)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\pytube\request.py", line 36, in get
    return _execute_request(url).read().decode("utf-8")
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\pytube\request.py", line 24, in _execute_request 
    return urlopen(request)  # nosec
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 214, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 523, in open
    response = meth(req, response)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 632, in http_response
    response = self.parent.error(
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 555, in error
    result = self._call_chain(*args)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 494, in _call_chain
    result = func(*args)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 747, in http_error_302
    return self.parent.open(new, timeout=req.timeout)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 523, in open
    response = meth(req, response)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 632, in http_response
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 561, in error
    return self._call_chain(*args)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 494, in _call_chain
    result = func(*args)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 641, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 410: Gone
  • 1
    It's a bad idea to call your own script `pytube.py` when you're trying to import the library `pytube`. I suggest calling your script something different to avoid the pitfalls associated with that. I'm not 100% sure if that's the source of your issue though but definitely try that to see if that fixes it. – Random Davis Feb 03 '22 at 17:45
  • I tried it and now I'm getting another error. – orewadeniz Feb 03 '22 at 18:00
  • Did you forget to post the error you're getting now? How can we help you if you don't say what it is? Just paste the error into your question, you're free to edit it. – Random Davis Feb 03 '22 at 18:13
  • I tried to paste it but it has more characters than limit. Oh okay got it now – orewadeniz Feb 03 '22 at 18:20
  • You tried to paste it into your question (not as a comment) and it was over the character limit? So you're saying the error is pages and pages long? Can you just show the last few relevant lines of it then, maybe? It seems unlikely that the error would be so huge. – Random Davis Feb 03 '22 at 18:21
  • At first i pasted it as a comment but now i edited my question – orewadeniz Feb 03 '22 at 18:22
  • When I googled that error, I found [this question](https://stackoverflow.com/questions/68680322/pytube-urllib-error-httperror-http-error-410-gone), where the answers say to update `pytube`, and explain how to do that. There's also a recent [bug report](https://github.com/pytube/pytube/issues/1191) about that exact issue on the project's GitHub page, which is unresolved, meaning you might have to wait for that bug to be fixed in order to be able to download that video. – Random Davis Feb 03 '22 at 18:29
  • Alright thank you very much – orewadeniz Feb 03 '22 at 18:33

1 Answers1

0

I don't see any issues with the code, and it works for me. Either you have an old version of pytube installed, or you named the file pytube.py (which is problematic because it imports itself instead of importing pytube).

PuffinDev
  • 1
  • 1
  • Now it occurs a different error but i cannot paste it here. It's about request.py __main__.py __init__.py. Should I uninstall python and install it again (I didnt mean to write main and init like that) – orewadeniz Feb 03 '22 at 18:09