In this article, we’ll discuss the error
can't find '__main__' module, the causes of it, and how to resolve the error in Python.
can't find '__main__' module Error in Python
We wrote a simple code to print in the PyCharm environment. As we see in the top right corner, the play button or running button is disabled, which means there is no way to run this code.
To be able to read this code, we need to add a configuration or add an interpreter, and then it gives us an execution. But if we click the play button, the program does not run.
The problem is that we don’t have an interpreter to run the codes on PyCharm, or we get an error when running the created interpreter. In our case, we created an interpreter, but still, the code doesn’t run.
So what caused the issue? The first reason is there is no virtual environment, and the second is the Python interpreter cannot find the project folder.
We need to check if we have installed Python on our system to resolve this issue. In the next step, we create a virtual environment for our project and specify the Python file we want to read.
To check the Python is installed or not, we open our terminal and type “python” and hit enter if you have Python installed on your system, it gives you the version of Python, and if nothing pops up, that means you do not have Python installed on our system. You need to download Python from here.
Let’s create a virtual environment for our project and create an empty folder. After that, we go to the PyCharm environment to delete the interpreter.
- Click on the top-left corner file.
- Go to settings. It will open up a project for you and then go to your project.
- We click on the plus(
+) button where we specifically tell the machine where we want to create our virtual environment.
- Once the virtual environment is created, you must select it.
In the next step, we will add configuration.
- For our project, click on
- Click on
add newand select
- After opening a new window, look at the script path where we have to select our project file, so you have to go through your project or wherever you saved your folder.
- Once you pick up the script path or the project file, it will automatically pick up the working directory. If it doesn’t, just click on the folder, go to the project folder, and pick it up yourself.
- Then click
- We will run the code to see if everything is working fine. Click on the play button, and the code is executed successfully here.
To make it short, when you get the
can't find '__main__' module error in Python. We resolve it by doing the following:
- Adding the main module to your script.
- Moving your script to the same directory as your main module.
We’ve discussed in this article how to resolve the
can't find '__main__' module error in Python.