Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obtain just the current module's name in Python

How can you just obtain the current module's name in Python.

 print(sys.modules[__name__])

Results in output of the form:

<module 'MODULE_NAME' from 'C:\\file_path_to_module_name\\MODULE_NAME.py'>

I just want to print MODULE_NAME

like image 627
kyrenia Avatar asked Feb 05 '23 20:02

kyrenia


1 Answers

Use __file__ which gives full path of the module file (if saved to disk) and transform it using os.path functions:

import os

print(os.path.splitext(os.path.basename(__file__))[0])
like image 50
Jean-François Fabre Avatar answered Feb 07 '23 18:02

Jean-François Fabre