1# Option 1: Works for Python 3.4 +
2from pathlib import Path
3Path(__file__).name # ScriptName.py
4Path(__file__).stem # ScriptName
5
6# Option 2: use `os` library
7import os
8os.path.basename(__file__) # ScriptName.py
9os.path.splitext(os.path.basename(__file__))[0] # ScriptName
1Use __file__. If you want to omit the directory part (which might be present), you can use os.path.basename(__file__)