How to Fix "python.exe: can't find 'main' module" Error in Python
The error message python.exe: can't find '__main__' module in [path]
indicates that the Python interpreter couldn't locate or execute the main entry point of your script. This typically happens due to simple mistakes in file naming, execution context, or IDE configuration (especially in PyCharm).
This guide explains the common causes and provides clear solutions.
Common Causes and Solutions
The __main__
module is essentially the top-level environment where your script's execution begins. The error means Python can't identify this starting point. Here are the most frequent culprits:
File Not Saved
Surprisingly often, the error occurs because you've made changes to your script but haven't saved them before running.
Solution: Save your file (Ctrl+S
or Cmd+S
on macOS, or File -> Save
in most editors).
Incorrect File Extension
Python scripts must have the .py
extension. If you accidentally save your file as .txt
, .py.txt
, or without any extension, Python won't recognize it as a runnable script.
Solution: Rename your file to have the correct .py
extension (e.g., my_script.py
). Make sure your operating system isn't hiding file extensions; you might need to adjust your folder view settings.
Incorrect if __name__ == '__main__'
Block
The if __name__ == '__main__':
block is a standard way to define code that should only run when the script is executed directly (not when it's imported as a module). Problems here usually stem from syntax errors within this block, or improper use of __name__
.
def do_work():
print('tutorialreference.com')
if __name__ == '__main__':
do_work() # Correct: Code to execute when run directly
Solution:
- Ensure the
if __name__ == '__main__':
block is correctly formatted (colons, indentation). - Make sure there are no syntax errors within the
if __name__ == '__main__'
block. A syntax error inside this block can prevent__main__
from being defined correctly. - Double check the string
__main__
that you are comparing it with.
Incorrect Execution Directory/Current Working Directory
If your script tries to import other modules or access files using relative paths, and you're not running the script from the correct directory, Python might not be able to find your main script or its dependencies.
Solution:
- From the command line: Use the
cd
command to change directory to the directory containing your script before running it:
cd /path/to/your/script
python your_script.py
- Within an IDE (like PyCharm): Ensure your IDE's run configuration is set to execute the script from the correct working directory. (See the PyCharm section below for details).
PyCharm-Specific Issues
PyCharm's run configurations can sometimes be the source of this error.
Incorrect "Script Path" in Run Configuration
This is the most common PyCharm-related cause. PyCharm needs to know the exact file to execute.
Solution:
- Open Run Configuration: Click on
Run
in the top menu, then selectEdit Configurations...
. - Select Your Script: In the left sidebar, find the run configuration for your script (it will likely have the same name as your script).
- Verify "Script Path": Ensure the "Script path" field contains the full, correct path to your Python file, including the
.py
extension. It's often best to use the file browser button to the right of the field to select the file directly, avoiding typos. Do not put the directory containing the script here; put the full path to the script file itself. - Apply and OK: Click
Apply
and thenOK
.
Example (Correct):
Script path: /Users/myuser/Documents/my_project/my_script.py
Example (Incorrect):
Script path: /Users/myuser/Documents/my_project/ <-- This is a directory, not a file!
Script path: /Users/myuser/Documents/my_project/my_script <-- Missing .py extension!
Spaces or Special Characters in Script Path
While less common, spaces or special characters in your project's path can sometimes cause problems, although PyCharm usually handles them correctly.
- Solution (if you suspect this): As a test, try temporarily moving your project to a simple path (e.g.,
C:\test
on Windows or/tmp/test
on Linux/macOS) without any spaces or special characters. If this resolves the issue, you'll need to refactor your project's location.