"Module Not Found Error" in Pycharm

"Module Not Found Error" in Pycharm

Introduction

There could be a few reasons why you might get a "module not found" error in PyCharm even though the module is installed, and there is no typo in the spelling of the name of the module which is being imported.

Why does "Module Not Found Error" error occurs in Pycharm

What Pycharm does is that while creating a new project, it by default creates a new virtual environment, without even inheriting the globally installed packages. Thus, we will be getting a "Module Not Found Error" even after the module is installed globally. There are various ways by which we can get rid of the above problem, which we will be seeing just now .

import my_module

# Some code here

Below is the error which we get upon running above code.

Traceback (most recent call last):
  File "/path/to/your/file.py", line 1, in <module>
    import my_module
ModuleNotFoundError: No module named 'my_module'

How to Resolve "Module Not Found Error" ?

Method 1 Creating a new project by clicking on File -> New Project-> Previously Configured Interpreter.This will create a new Python project with the same version and packages as the globally installed one, and thus, if a module is installed globally we will be able to use it with Pycharm . method-1-module-not-found

Method 2 Another way is to install the missing package for that virtual environment, and we can do that by simply going through File -> Settings -> Project -> Python Interpreter . After which we will get to see this kind of prompt. method-2-module-not-found-1

After which click on ‘+’, then search for the module which we desire in the available packages-> clicking on the install.This will install the required package and you will be able to use it with your code in Pycharm. If even after this it didn't resolve, you can try restarting your Pycharm, it will definitely work. method-2-module-not-found-2

Method 3 Another work around can be to change the Python Interpreter which we are using for our virtual environment to the python interpreter used inside the console. We can change the python version by typing the below command

python - -version

It will show the python version installed and we should change it to this python interpreter. We can do this by going through File -> Settings -> Project -> Python Interpreter -> Selecting a different interpreter from the drop down -> Clicking OK. method-3-module-not-found Our Project will start using that Python Interpreter and our error will be gone.

Method 4 There may be some other ways which may occur resulting in "ModuleNotFoundError" which are not related to Pycharm like, there can be a typo in the module name. Make sure that you've spelled the module name correctly.

Conclusion:-

Hope, this would help you in getting rid of your problem of "Module Not Found Error" in Pycharm without much of an effort.

"Module Not Found Error" in Pycharm