'NameError in pytube

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


Solution 1:[1]

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).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 PuffinDev