6

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

kyrenia
  • 5,431
  • 9
  • 63
  • 93

1 Answers1

11

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])
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219