python.exe: can't find '__main__' module in Path

avatar
Borislav Hadzhiev

Last updated: Apr 13, 2024
3 min

banner

# python.exe: can't find '__main__' module in Path

The error "python.exe: can't find '__main__' module in Path" occurs for multiple reasons:

  1. Forgetting to save your Python script before running it.
  2. Setting the extension of the Python module to be something other than .py.
  3. Not adding the if __name__ == "__main__": conditional correctly.
  4. Setting the "Script path:" setting incorrectly in PyCharm.

# Make sure you've saved your file before running it

The first two things you should check are that:

  1. You've saved your file. In most code editors, you can press Ctrl + S or Cmd + S (on macOS).

  2. You've given your Python script a .py extension, e.g. example.py or app.py.

Try to rerun your file after making sure that it has a .py extension and has been saved.

# Make sure you've added the if __name__ == "__main__": conditional correctly

Another thing you should check is that you've added the if __name__ == "__main__": conditional correctly.

main.py
def do_work(): print('bobbyhadz.com') if __name__ == '__main__': # Call functions or run logic here 👇️ do_work()

make sure name equals main-added correctly

The code for this article is available on GitHub

The if statement that checks if __name__ is equal to '__main__' ensures that the if block only runs if the file is run directly with python script_name.py.

For example, if your Python script is named main.py, the code in the ifblock would only run if your script is run with python main.py.

The __name__ global variable is only set to '__main__' when the file is run directly with python your_script.py and not when it's imported into a different file.

# Make sure your "Script path" is set correctly in PyCharm

If you use the PyCharm IDE, make sure your "Script path" is set correctly.

  1. Click on Run in the top menu and select "Edit Configurations".

click run edit configurations

  1. Select your script from the menu on the left and make sure the "Script path" is set correctly and points to the Python script.

make sure script path is set correctly in pycharm

The Script Path should be the complete path to your Python script, including the filename and extension.

If you still run into issues, make sure your Script path doesn't contain spaces or special characters.

You can click on the folder icon next to the Script Path option to select your Script Path by navigating directories instead of manually typing it in.

Make sure to click on Apply and OK once you've selected the correct Script Path.

If that doesn't work, press Alt + Shift + F10.

press alt shift f10

Select your module's name and hit Enter.

Once you've selected your module's name you can press Shift + F10 on subsequent runs.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.