Python Error Can't Find Main Module

Python Error Can't Find Main Module

  1. Resolve the can't find '__main__' module Error in Python
  2. Conclusion

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.

Resolve the 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.

cannot run the code in PyCharm

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.

cannot run the program in pycharm after configuration

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.

steps to resolve the error

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.

  1. Click on the top-left corner file.
  2. Go to settings. It will open up a project for you and then go to your project.
  3. We click on the plus(+) button where we specifically tell the machine where we want to create our virtual environment.
  4. Once the virtual environment is created, you must select it.
  5. Click "OK" then "Apply".

create a virtual environment in pycharm

In the next step, we will add configuration.

  1. For our project, click on add configuration.
  2. Click on add new and select Python.
  3. 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.
  4. 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.
  5. Then click "Apply" and "OK".
  6. We will run the code to see if everything is working fine. Click on the play button, and the code is executed successfully here.

run the code after adding configuration in pycharm

To make it short, when you get the can't find '__main__' module error in Python. We resolve it by doing the following:

  1. Adding the main module to your script.
  2. Moving your script to the same directory as your main module.

Conclusion

We’ve discussed in this article how to resolve the can't find '__main__' module error in Python.

Salman Mehmood avatar Salman Mehmood avatar

Hello! I am Salman Bin Mehmood(Baum), a software developer and I help organizations, address complex problems. My expertise lies within back-end, data science and machine learning. I am a lifelong learner, currently working on metaverse, and enrolled in a course building an AI application with python. I love solving problems and developing bug-free software for people. I write content related to python and hot Technologies.

LinkedIn

Related Article - Python Error

  • Python PermissionError: [WinError 5] Access Is Denied
  • Python TypeError: 'DataFrame' Object Is Not Callable
  • Python TypeError: Can't Convert 'List' Object to STR
  • Local Variable Referenced Before Assignment Error in Python
  • Python Handling Socket.Error: [Errno 104] Connection Reset by Peer
  • Python Is Not Recognized in Windows 10