2

Here is the code:

import pytube as p
video_url = input("Enter the link: ")
youtube = p.YouTube(video_url)
filters = youtube.streams.filter(progressive=True, file_extension="mp4")
filters.get_highest_resolution().download("MyPath")

I tried to write a code to download a YouTube video. But it's throwing an error saying:

AttributeError: partially initialized module 'pytube' has no attribute 'YouTube' (most likely due to a circular import)`

full error message

I even copy-pasted codes from the internet, re-installed Python, and re-installed pytube but none worked. What's even more frustrating is, that it was working fine when I executed it a few months before.

error_09
  • 59
  • 7
  • 3
    Please do not post pictures, post code and error message! [mre] – Sören Apr 24 '22 at 08:26
  • change your file name. – nfn Apr 24 '22 at 08:30
  • 1
    @nfn should be right, if after changing the filename you are getting a ***different*** error you must edit the answer to make it possible to help you. – FLAK-ZOSO Apr 24 '22 at 08:33
  • ok I will not post pictures . This is my first time asking question here. And yes i did change the file name . The error didn't change – error_09 Apr 24 '22 at 08:39
  • my suggestion: search C-drive and delete all files "pytube.py". After that install my version: `!pip install git+https://github.com/JNYH/pytube`. More info: https://github.com/JNYH/pytube/blob/master/pytube_sample_code.ipynb – blackraven May 08 '22 at 14:15

1 Answers1

6

AttributeError: partially initialized module 'pytube' has no attribute 'YouTube' (most likely due to a circular import)

Did you notice how is your file named? pytube.py. This may have caused the circular import, since Python is trying to import the pytube.py file itself.


I would suggest you to read this and this, the first one is exactly your case.


So the short answer is:

Change your file name!

And when I say change I mean that you have to rename your file, and not to create a new one.

FLAK-ZOSO
  • 3,873
  • 4
  • 8
  • 28
  • 1
    Thanks for your response. But I did notice it and I changed the name but it didn't work :( – error_09 Apr 24 '22 at 08:31
  • You are getting the same error? This is very strange, could you edit your answer with what you've tried and what did you get? – FLAK-ZOSO Apr 24 '22 at 08:32
  • Yes I am getting the same error . I changed the file name to videodownload.py and the error didn't change – error_09 Apr 24 '22 at 08:36
  • Try to change it to myFileWithUnusualName.py, since there may be a module called videodownload imported by pytube with the scope to download videos. – FLAK-ZOSO Apr 24 '22 at 08:40
  • ok I will try it out and tell you – error_09 Apr 24 '22 at 08:42
  • Traceback (most recent call last): File "c:\Users\pavit\AppData\Local\Programs\Python\Python310\python programs\pavithra.py", line 1, in import pytube as p File "c:\Users\pavit\AppData\Local\Programs\Python\Python310\python programs\pytube.py", line 3, in youtube = p.YouTube(video_url) AttributeError: partially initialized module 'pytube' has no attribute 'YouTube' (most likely due to a circular import) – error_09 Apr 24 '22 at 08:43
  • it didn't work :( – error_09 Apr 24 '22 at 08:43
  • Ok, you didn't rename your file. You must still delete pytube.py – quamrana Apr 24 '22 at 08:56
  • @quamrana probably understood the problem, try deleting pytube.py, and in the future remember to avoid files with the same name of a module used in them. If it worked accept my answer, otherwise feel free to ask in the comments. – FLAK-ZOSO Apr 24 '22 at 09:02
  • ok I will try that out – error_09 Apr 24 '22 at 12:28
  • @PavithraE if it worked please accept my answer, otherwise feel free to ask in the comments. – FLAK-ZOSO Apr 28 '22 at 15:22
  • I'm sorry but it is still giving me that error, I thought it was resolved but I keep getting it no matter what I tried :( – error_09 Apr 30 '22 at 12:52
  • And sometimes the error wont show up but gets replaced by[ AttributeError: 'YouTube' object has no attribute 'filter'] – error_09 Apr 30 '22 at 12:57
  • The other AttributeError doesn't depend on it – FLAK-ZOSO Apr 30 '22 at 12:57