Search code examples
pythonpycharmpytube

"Module not found error" when trying to run pytube on PyCharm


I am trying to use pytube to download videos but it isn't working. I don't know much about python (or any other programming language) so it is likely that my problem is easy to solve. I have tried reading multiple websites and watching several YouTube videos, but I haven't been able to make it work yet.

  • I use a MacOS High Sierra 10.13.6;
  • I have both Python 2.7.16 and Python 3.8.3 versions installed;
  • I used the terminal to install pytube using "pip install pytube3";
  • When I try to install again using the same command as above, I think that it is correctly installed, because the terminal shows the following messages:
Requirement already satisfied: pytube3 in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (9.6.4)
Requirement already satisfied: typing-extensions in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (from pytube3) (3.7.4.3)
  • I am trying to use PyCharm 2020.2.1 (Community Edition)
  • I think that pytube is also OK in PyCharm, because it appears in "External libraries" under "site-packages"

My problem: When I open PyCharm, it opens on "main.py". There, I try to write:

import pytube.

But I get the following message:

/Users/applemacosx/PycharmProjects/pythonProject5/venv/bin/python /Users/applemacosx/PycharmProjects/pythonProject5/main.py
Traceback (most recent call last):
  File "/Users/applemacosx/PycharmProjects/pythonProject5/main.py", line 1, in <module>
    import pytube
ModuleNotFoundError: No module named 'pytube'

Process finished with exit code 1

I have tried searching about this error, but I wasn't able to find anything that I could make work. I would be very grateful if someone could help me understand what I am doing wrong.


Solution

  • What's happening is that you've indeed installed pytube on the python installation on your machine, but Pycharm creates a virtual environment for each new project you create.

    So while pytube is installed in the python environment on your machine, it's not installed in this newly created python environment. The solution is to install pytube in the project's virtual environment. There a couple of ways to do that. Probably the easiest would be to go to the terminal window in Pycharm, and execute the pip install command.