226

How can I get the file name and line number in a Python script?

Exactly the file information we get from an exception traceback. In this case without raising an exception.

0

15 Answers 15

258

Thanks to mcandre, the answer is:

#python3
from inspect import currentframe, getframeinfo

frameinfo = getframeinfo(currentframe())

print(frameinfo.filename, frameinfo.lineno)
5
  • 4
    Does using this method have any performance impact (like minor increase in run time or more CPU needed ) ?
    – gsinha
    Commented Dec 14, 2014 at 5:41
  • 11
    @gsinha: Every function call has performance impact. You have to measure if this impact is acceptable for you.
    – omikron
    Commented Nov 9, 2015 at 14:45
  • 19
    So, if you like "one line" type answers use: import inspect inspect.getframeinfo(inspect.currentframe()).lineno
    – 1-ijk
    Commented Sep 21, 2017 at 18:11
  • 2
    To expand on this, at what point is the line number "evaluated", in the second or third line? I.e does frameinfo.lineno give you the line numer when you evaluate it, or when you created it with getframeinfo(currentframe())?
    – Marses
    Commented Mar 26, 2018 at 13:48
  • 2
    @LimokPalantaemon it happens when currentframe() is called, which means you can't simplify this any more than getframeinfo(currentframe()).lineno (if you only care about the line number and not the file name). See docs.python.org/2/library/inspect.html#inspect.currentframe Commented Sep 21, 2018 at 17:20
100

Whether you use currentframe().f_back depends on whether you are using a function or not.

Calling inspect directly:

from inspect import currentframe, getframeinfo

cf = currentframe()
filename = getframeinfo(cf).filename

print "This is line 5, python says line ", cf.f_lineno 
print "The filename is ", filename

Calling a function that does it for you:

from inspect import currentframe

def get_linenumber():
    cf = currentframe()
    return cf.f_back.f_lineno

print "This is line 7, python says line ", get_linenumber()
5
  • 11
    Plus one, for providing a solution in a callable function. Very nice!
    – MikeyE
    Commented Oct 28, 2017 at 2:43
  • 3
    Always wanted to call from a function - this helps. THANK YOU Commented Oct 21, 2020 at 4:33
  • golang log style def log(*args, **kwargs): cf = inspect.currentframe() print(f"{inspect.stack()[1][1]}:{cf.f_back.f_lineno}", *args, **kwargs) Commented Dec 8, 2020 at 7:38
  • i'm using a lambda line = lambda : currentframe().f_back.f_lineno with your solution, very nice Commented Apr 8, 2021 at 19:36
  • the sys equivalent in other answers is faster if performance matters (e.g. you're calling it a lot); on my newish windows pc this takes ~800ns while the sys version takes 500ns. Commented Jul 5, 2024 at 5:48
38

Handy if used in a common file - prints file name, line number and function of the caller:

import inspect
def getLineInfo():
    print(inspect.stack()[1][1],":",inspect.stack()[1][2],":",
          inspect.stack()[1][3])
0
31

Better to use sys also-

import sys
print(dir(sys._getframe()))
print(dir(sys._getframe().f_lineno)
print(sys._getframe().f_lineno)

The output is:

['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'f_back', 'f_builtins', 'f_code', 'f_exc_traceback', 'f_exc_type', 'f_exc_value', 'f_globals', 'f_lasti', 'f_lineno', 'f_locals', 'f_restricted', 'f_trace']
['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__', '__delattr__', '__div__', '__divmod__', '__doc__', '__float__', '__floordiv__', '__format__', '__getattribute__', '__getnewargs__', '__hash__', '__hex__', '__index__', '__init__', '__int__', '__invert__', '__long__', '__lshift__', '__mod__', '__mul__', '__neg__', '__new__', '__nonzero__', '__oct__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator', 'imag', 'numerator', 'real']
14
2
  • Should use sys._getframe().f_back.f_lineno, especialy when used in a function, otherwise it will print the line where it's being called in the function.
    – synkro
    Commented May 15, 2023 at 21:34
  • I wouldn't say it's better. According to the documentation: This function should be used for internal and specialized purposes only. It is not guaranteed to exist in all implementations of Python. IntelliJ will throw a warning about using a protected member.
    – Cezariusz
    Commented May 2, 2024 at 13:07
23

Filename:

__file__
# or
sys.argv[0]

Line:

inspect.currentframe().f_lineno

(not inspect.currentframe().f_back.f_lineno as mentioned above)

4
  • 1
    NameError: global name '__file__' is not defined on my Python interpreter: Python 2.7.6 (default, Sep 26 2014, 15:59:23). See stackoverflow.com/questions/9271464/…
    – bgoodr
    Commented May 5, 2017 at 17:41
  • 1
    a function version, def __LINE__() -> int: return inspect.currentframe().f_back.f_lineno def __FILE__() -> str: return inspect.currentframe().f_back.f_code.co_filename
    – hanshenrik
    Commented May 10, 2022 at 21:25
  • @hanshenrik, copilot agrees with you
    – oldpride
    Commented Jul 18, 2023 at 22:50
  • sys.argv[0] gives the command used to invoke
    – JohnK
    Commented Jan 10, 2024 at 15:30
23

In Python 3 you can use a variation on (This has been improved after a comment by Claudio):

def Deb(msg=""):
  print(f"Debug {sys._getframe().f_back.f_lineno}: {msg}")

In code, you can then use:

Deb("Some useful information")
Deb()

To produce:

123: Some useful information
124:

Where the 123 and 124 are the lines that the calls are made from.

4
  • 3
    This is the simplest and the best, especially when sys is already imported. inspect from other answers is overkill for my use case. Commented Jun 4, 2021 at 14:26
  • Hint: eliminate unnecessary code: if you change Deb(msg=None) to Deb(msg='') you can replace {msg if msg is not None else ''} with {msg}.
    – user7711283
    Commented Feb 16, 2023 at 2:48
  • Good point, thanks - I've revised the answer. Commented Feb 17, 2023 at 13:35
  • I like that this gives the line number of the caller without an exotic import. But it doesn't give the module/filename of the caller, which makes it less helpful.
    – JohnK
    Commented Jan 10, 2024 at 15:27
9

Here's a short function that prints the file name and line number.

from inspect import currentframe, getframeinfo


def HERE(do_print=True):
    ''' Get the current file and line number in Python script. The line 
    number is taken from the caller, i.e. where this function is called. 

    Parameters
    ----------
    do_print : boolean
        If True, print the file name and line number to stdout. 

    Returns
    -------
    String with file name and line number if do_print is False.

    Examples
    --------
    >>> HERE() # Prints to stdout

    >>> print(HERE(do_print=False))
    '''
    frameinfo = getframeinfo(currentframe().f_back)
    filename = frameinfo.filename.split('/')[-1]
    linenumber = frameinfo.lineno
    loc_str = 'File: %s, line: %d' % (filename, linenumber)
    if do_print:
        print('HERE AT %s' % (loc_str))
    else:
        return loc_str

Usage:

HERE() # Prints to stdout
# Output: HERE AT File: model.py, line: 275

print(HERE(False)) # Retrieves string and prints it.
# Output: File: model.py, line: 276
1
  • very good idea.
    – alexzander
    Commented Dec 30, 2021 at 21:45
8

Just to contribute,

there is a linecache module in python, here is two links that can help.

linecache module documentation
linecache source code

In a sense, you can "dump" a whole file into its cache , and read it with linecache.cache data from class.

import linecache as allLines
## have in mind that fileName in linecache behaves as any other open statement, you will need a path to a file if file is not in the same directory as script
linesList = allLines.updatechache( fileName ,None)
for i,x in enumerate(lineslist): print(i,x) #prints the line number and content
#or for more info
print(line.cache)
#or you need a specific line
specLine = allLines.getline(fileName,numbOfLine)
#returns a textual line from that number of line

For additional info, for error handling, you can simply use

from sys import exc_info
try:
     raise YourError # or some other error
except Exception:
     print(exc_info() )
7
import inspect    

file_name = __FILE__
current_line_no = inspect.stack()[0][2]
current_function_name = inspect.stack()[0][3]

#Try printing inspect.stack() you can see current stack and pick whatever you want 
1
1

Here's what works for me to get the line number in Python 3.7.3 in VSCode 1.39.2 (dmsg is my mnemonic for debug message):

import inspect

def dmsg(text_s):
    print (str(inspect.currentframe().f_back.f_lineno) + '| ' + text_s)

To call showing a variable name_s and its value:

name_s = put_code_here
dmsg('name_s: ' + name_s)

Output looks like this:

37| name_s: value_of_variable_at_line_37
1
  • Thanks @Steph! I came up with the following for my code: def dbgline(): print(f"{inspect.currentframe().f_back.f_globals['file']}:{inspect.currentframe().f_back.f_lineno}") Commented Mar 3, 2023 at 13:38
1

Golang style

import inspect
import sys
import atexit

ERR_FILE = open('errors.log', 'w+', encoding='utf-8')
LOG_FILE = open('log.log', 'w+', encoding='utf-8')

def exit_handler():
    # ctrl + C works as well
    log("Exiting")
    ERR_FILE.close()
    LOG_FILE.close()

# close files before exit
atexit.register(exit_handler)

def log(*args, files=[sys.stdout, LOG_FILE]):
    # can also add timestamps etc.
    cf = inspect.currentframe()
    for f in files:
        print("DEBUG", f"{inspect.stack()[1][1]}:{cf.f_back.f_lineno}", *args, file=f)
        f.flush()

def log_err(*args, files=[ERR_FILE, sys.stderr]):
    cf = inspect.currentframe()
    for f in files:
        print("ERROR", f"{inspect.stack()[1][1]}:{cf.f_back.f_lineno}", *args, file=f)
        f.flush()

log("Hello World!")
log_err("error")

Output

DEBUG sample.py:29 Hello World!
ERROR sample.py:30 error
DEBUG sample.py:9 Exiting
0

Inspired by many of the above answers and the console.log() function of JavaScript, I have deployed a pip package to serve this purpose. Please check this out here.

How to install?

Just install the package using the command:

pip3 install print_position

A simple example is (test.py):

from printPosition.printPosition import printPosition as print 
print("Test on line 2 from test.py")
print("Test on line 3 from test.py")



print("Test on line 7 from test.py")

The output of the above code is:

@/home/pranav/Desktop/GitHub/PrintPosition-pip/print-log/test.py: 2 
Test on line 2 from test.py
@/home/pranav/Desktop/GitHub/PrintPosition-pip/print-log/test.py: 3 
Test on line 3 from test.py
@/home/pranav/Desktop/GitHub/PrintPosition-pip/print-log/test.py: 7 
Test on line 7 from test.py

Please let me know if it helped you! Also feel free to raise any issues/requests, everything is mentioned on the pip page here.

0

People familiar with PHP's __FILE__ and __LINE__ might appreciate

import inspect

def __FILE__() -> str:
    # ptyhon has a native __file__ 
    return inspect.currentframe().f_back.f_code.co_filename

def __LINE__() -> int:
    # python has no native __line__, the closest thing I could find was: sys._getframe().f_lineno
    return inspect.currentframe().f_back.f_lineno

unfortunately they must be used with (), sample usage:

print("file: " + __FILE__() + " line: " + str(__LINE__()) );

(both are part of my collection of php-apis-ported-to-python, https://github.com/divinity76/phppy/blob/main/php.py )

3
  • This didn't work for me
    – con
    Commented Aug 9, 2023 at 14:49
  • @con what happens when you try it? an error? wrong line number? wrong file? fwiw here is the code running on Wandbox: wandbox.org/permlink/WABqQbLtEJdltVwV
    – hanshenrik
    Commented Aug 9, 2023 at 15:26
  • maybe I didn't implement the code correctly, a minimal example seems to work, the much larger project that I edited before didn't work, likely my own fault
    – con
    Commented Aug 10, 2023 at 16:25
0

sys._getframe().f_lineno is BY FAR the most performant out of the 3 below execution line retrieval methods according to timeit() results.

# Prints the line # the call was executed from
print(sys._getframe().f_lineno) # METHOD #1
print(getframeinfo(currentframe()).lineno) # METHOD #2
print(inspect.stack()[0][2]) # METHOD #3

# Number used for timeit() iterations
num = 10000

# Below Line: METHOD #1 - Fastest - 0.0011057000083383173 (~2000x faster than below)
print(timeit.timeit('sys._getframe().f_lineno', setup = 'from __main__ import sys', number = num))
# Below Line: METHOD #2 - 2nd Fastest - 2.3906398999970406 (~2.5x faster than below)
print(timeit.timeit('(getframeinfo(currentframe()).lineno)', setup = 'from __main__ import currentframe, getframeinfo', number = num))
# Below Line: METHOD #3 - Slowest - 6.436119699996198 (~6,000x slower than fastest)
print(timeit.timeit('inspect.stack()[0][2]', setup = 'from __main__ import inspect', number = num))

I'm not super familiar with these methods so please comment if there are reasons that this test scenario doesn't reflect real-world performance, although based on the fact that if you change the number arg from 10,000 to 1,000 the results are also divided by 10 it does seem that these are accurate.

0

The sys._getframe().f_back.f_lineno expression above does the job nicely. I’ve wrapped it into a simple logging function:

def dbug(*args, **kwargs):
    print(f'{sys._getframe().f_back.f_lineno}: ', *args, **kwargs)

I use the dbug() function as a replacement for the print() function to print out values at strategic points in the code I’m trying to trouble-shoot, and it’s handy to be able to include the line number.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.