cweyy
cweyy

Reputation: 1

Why can't I import PyTube?

I would like to import PyTube but keep getting this error. How can I fix it?

Code:

import pytube
from pytube import YouTube

print("Give URL:")
url = input()

pytube.YouTube(url).streams.get_highest_resolution().download()

Error:

Traceback (most recent call last):
  File "pytube.py", line 1, in <module>
    import pytube
  File "/home/python/pytube.py", line 2, in <module>
    from pytube import YouTube
ImportError: cannot import name 'YouTube'

Upvotes: 0

Views: 1701

Answers (2)

أبو سيف
أبو سيف

Reputation: 1

import pytube

print ('Download video from web by python')
url = input ("Enter video url ")


pytube.YouTube(url).streams.get_highest_resolution().download("/storage/emulated/0/Download/")

Upvotes: 0

vvvvv
vvvvv

Reputation: 31760

As @Michael Szczesny stated, you named the file you executed with the same name as the library. Rename your script to something else and it should work.

Python checks the imports in a particular order that can be seen in sys.path:

import sys
print(sys.path)

In my case, it gives:

['', '/home/vinzee/.pyenv/versions/3.6.14/lib/python36.zip', '/home/vinzee/.pyenv/versions/3.6.14/lib/python3.6', '/home/vinzee/.pyenv/versions/3.6.14/lib/python3.6/lib-dynload', '/home/vinzee/.pyenv/versions/3.6.14/lib/python3.6/site-packages']

telling me it first searches in the current directory '', then, in '/home/vinzee/.pyenv/versions/3.6.14/lib/python36.zip' etc. and finally in '/home/vinzee/.pyenv/versions/3.6.14/lib/python3.6/site-packages' where the packages' files are stored.

In your case, since the script found a pytube in the first directory it searched (ie ''), it did not bother to check if there was a package with the same name in the last directory of the list. It just stopped there and tried importing your own pytube.py script.

Upvotes: 1

Related Questions