Get files, functions, line numbers running in python

Because an error occurs in python3 From frame = inspect.currentframe (depth + 1) Fixed to frame = inspect.currentframe (). f_back


import inspect
import os

def location(depth=0):
  frame = inspect.currentframe().f_back
  return os.path.basename(frame.f_code.co_filename), frame.f_code.co_name, frame.f_lineno

def func1():
  print(location())

def main():
  print(location())
  func1()

if __name__ == '__main__':
  main()
  exit(0)
('a.py', 'main', 13)
('a.py', 'func1', 10)

reference

Look up the current filename and line number in Python. --k Somehow Diary http://d.hatena.ne.jp/kwatch/20100410/1270851205

Python Tips: I want to get the name and arguments of the current function --Life with Python http://www.lifewithpython.com/2015/11/python-get-function-name.html

I want to get the name of the function / method being executed --Qiita http://qiita.com/megmogmog1965/items/0b4ea3d58e34f1854158

Recommended Posts

Get files, functions, line numbers running in python
[Python] Get the files in a folder with Python
Get options in Python from both JSON files and command line arguments
Get date in Python
Prime numbers in Python
[Python] Get the numbers in the graph image with OCR
How to add page numbers to PDF files (in Python)
Get YouTube Comments in Python
Get last month in python
Overriding library functions in Python
Get Terminal size in Python
Explicitly get EOF in python
Get Evernote notes in Python
Wrapper running Hadoop in Python
Python functions learned in chemoinformatics
Handle complex numbers in Python
Get Japanese synonyms in Python
Get Leap Motion data in Python.
Transpose CSV files in Python Part 1
Make python segfault in one line
Testing with random numbers in Python
[python] Manage functions in a list
Get data from Quandl in Python
Get the desktop path in Python
Download Google Drive files in Python
Get the script path in Python
Using global variables in python functions
Sort large text files in Python
Get, post communication memo in Python
Read files in parallel with Python
Export and output files in Python
Dynamically define functions (methods) in Python
Get the desktop path in Python
Law of large numbers in python
Get the host name in Python
I tried Line notification in Python
[Introduction] Insert line breaks in Python 3
Implemented in 1 minute! LINE Notify in Python
Get started with Python in Blender
Extract strings from files in Python
How to use functions in separate files Perl and Python versions
How to get a string from a command line argument in python
Get a list of files in a folder with python without a path
[Python3] Dynamically define global variables in functions
CGI server (1) python edition in one line
Get exchange rates from open exchange rates in Python
Get Suica balance in Python (using libpafe)
Find files like find on linux in Python
Easily use your own functions in Python
Pharmaceutical company researchers summarized functions in Python
Type annotations for Python2 in stub files!
Python functions
Get Google Fit API data in Python
Referencing INI files in Python or Ruby
How to get a stacktrace in python
Get Youtube data in Python using Youtube Data API
Read the file line by line in Python
Read the file line by line in Python
Get battery level from SwitchBot in Python
Modules of frequently used functions in Python (such as reading external files)
Read and write JSON files in Python