Get name of current script in Python
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Python, the best way to get the current script name depends on what "current script" means in your context. If you want the file containing the current module, __file__ is usually the right tool. If you want the entry-point script that launched the process, sys.argv[0] is often the relevant value.
Use __file__ for the Current Module File
Inside a normal Python module, __file__ gives the path of that module's file.
This is usually the most direct answer when you want the filename of the source file currently being executed.
Use sys.argv[0] for the Entry Script
If you want the name of the main script that launched the process, use sys.argv[0].
This can differ from __file__ when the current code is running inside an imported module.
Why the Two Values Can Differ
Suppose you have:
- '
main.pyas the program entry point' - '
helpers/reporting.pyas an imported module'
Inside reporting.py:
- '
__file__points toreporting.py' - '
sys.argv[0]usually points tomain.py'
That distinction matters a lot in larger applications.
Example Showing Both Values
Here is a small script that prints both.
In a single-file script, these may look the same. In multi-module applications, they often do not.
Use Path Instead of Manual String Splitting
Use pathlib.Path or os.path.basename rather than splitting on path separators yourself.
This keeps the code portable across Windows, macOS, and Linux path conventions.
Interactive and Notebook Contexts
One important caveat is that __file__ is not always defined. In interactive shells, notebooks, and some embedded Python environments, it may be missing.
Example defensive check:
If you are in a Jupyter notebook, there usually is no meaningful script filename in the ordinary sense.
Packaged or Frozen Applications
In packaged applications built with tools such as PyInstaller, path behavior can differ from ordinary script execution. If your real goal is resource lookup rather than logging the script name, design for that specifically instead of assuming file-layout behavior will stay the same after packaging.
This is one reason developers should separate:
- diagnostic script naming
- runtime resource location
Those are related but not identical concerns.
A Small Utility Function
If you want a reusable helper, keep the semantics explicit.
Naming the functions around intent makes the code easier to use correctly.
Common Pitfalls
- Assuming
__file__andsys.argv[0]always mean the same thing. - Using
__file__in interactive environments where it may not exist. - Splitting path strings manually instead of using
pathliboros.path. - Using script-name logic for resource discovery in packaged apps without testing the packaged runtime.
- Forgetting that imported modules have their own
__file__values separate from the program entry point.
Summary
- Use
__file__when you want the filename of the current module file. - Use
sys.argv[0]when you want the script that started the process. - Use
Path(...).nameoros.path.basenamefor portable filename extraction. - Be careful in notebooks and interactive shells because
__file__may not exist. - Decide first whether you want the current module name or the process entry script, because they are often different.

