Python
scripting
script name
current script
code tips

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.

python
1from pathlib import Path
2
3script_name = Path(__file__).name
4print(script_name)

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].

python
1import sys
2from pathlib import Path
3
4entry_name = Path(sys.argv[0]).name
5print(entry_name)

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.py as the program entry point'
  • 'helpers/reporting.py as an imported module'

Inside reporting.py:

  • '__file__ points to reporting.py'
  • 'sys.argv[0] usually points to main.py'

That distinction matters a lot in larger applications.

Example Showing Both Values

Here is a small script that prints both.

python
1import sys
2from pathlib import Path
3
4print("__file__:", Path(__file__).name)
5print("argv[0]:", Path(sys.argv[0]).name)

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.

python
import os

print(os.path.basename(__file__))

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:

python
1from pathlib import Path
2
3if '__file__' in globals():
4    print(Path(__file__).name)
5else:
6    print('No __file__ in this environment')

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.

python
1import sys
2from pathlib import Path
3
4
5def current_module_name() -> str | None:
6    return Path(__file__).name if '__file__' in globals() else None
7
8
9def entry_script_name() -> str:
10    return Path(sys.argv[0]).name
11
12
13print(current_module_name())
14print(entry_script_name())

Naming the functions around intent makes the code easier to use correctly.

Common Pitfalls

  • Assuming __file__ and sys.argv[0] always mean the same thing.
  • Using __file__ in interactive environments where it may not exist.
  • Splitting path strings manually instead of using pathlib or os.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(...).name or os.path.basename for 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.

Course illustration
Course illustration

All Rights Reserved.