Sometimes, when working with Python, you may need to know the name of the script you’re running. Whether it’s for debugging, logging, or just displaying it for reference, there are simple ways to retrieve this information. In this blog, we’ll explain simple methods to check the name of your Python script.
Table of contents:
Various Methods to Get the Name of the Current Script in Python
Method 1: Using ‘__file__’ to get the script name
The easiest way to get the name of the current script is by using the built-in ‘__file__’ attribute. This attribute contains the path of the script being executed. This attribute might not be available in interactive modes like Jupyter.
Example:
# script_name.py
# Get the name of the current script
script_name = __file__
# Print the script name
print(f"The name of the current script is: {script_name}")
Output:
The name of the current script is: /home/user/scripts/script_name.py
Explanation:
When you run this script, it will output the name of the script file with the file directory.
Method 2: Using “os.path.basename” for just the filename
If you only need the filename without the full path, you can use the `os.path.basename` function from the `os` module. Here’s how you can do it:
Example:
import os
# Get the name of the current script
script_name = os.path.basename(__file__)
# Print the script name
print(f"The name of the current script is: {script_name}")
Output:
The name of the current script is: script_name.py
Explanation: This will give only the file name as the output.
Method 3: Handling scripts run in interactive mode
If you’re running code in an interactive mode like the Jupyter Notebook the ‘__file__’ attribute might not be available. In such cases, you can use the following approach to handle it gracefully:
Example:
import os
import sys
try:
script_name = os.path.basename(__file__)
except NameError:
script_name = os.path.basename(sys.argv[0])
print(f"The name of the current script is: {script_name}")
Explanation:
This code tries to get the script name using `__file__`, if it’s unavailable, it falls back to `sys.argv[0]`, which contains the script’s name that was executed.
Method 4: Using inspect Module
The Inspect method will help you to get the name of the current script by using the inspect module. This module provides several useful functions to get information about live objects, including the current script. Let’s see an example.,
Example:
import inspect
import os
# Get the current frame
current_frame = inspect.currentframe()
# Get the file name from the frame
script_name = os.path.basename(current_frame.f_globals["__file__"])
# Print the script name
print(f"The name of the current script is: {script_name}")
Output:
The name of the current script is: script_name.py
Get 100% Hike!
Master Most in Demand Skills Now!
Why would you want to know the name of the current script?
Knowing the name of the current script can be helpful for various reasons.
1- For debugging: If you are working with multiple scripts, you might need to figure out which script is running.
2- For logging: Adding the script name to your log files makes it easier to track the source of log entries.
Conclusion
Getting the current script name in Python is a simple task, at least with the help of the __file__ attribute, os module, and even inspecting the inspect module. It could be for debugging, logging, or display. Hopefully, this blog will provide you with a clear understanding of how to get the name of the current Python script. If you want to excel in your career in Python, you may refer to our Python course.
FAQ’s
1. What if I want to get only the script name without the full path?
You can use os.path.basename(__file__) to get just the filename without the full path.
2. How do I handle getting the script name in an interactive mode like Jupyter Notebook?
You can use a try-except block to revert to `sys.argv[0]` when `__file__` is unavailable.
3. Can I use other modules to get the script name?
Yes, you can use the inspect module to obtain the script name and other useful information about live objects.