1

How do you print a module name in Python?

I tried to import a module and print it, then it gives me <module 'time' (built-in)>.

import time
print(time)  # <module 'time' (built-in)>

How to print just the module name?

1
  • Quick tip, you can check what members a variable has with dir(x), e.g. dir(time). In this case you would see that is has a __name__ attribute Commented Apr 10, 2022 at 8:51

2 Answers 2

4

The name of a module as a string is available as its __name__ attribute.

>>> import time
>>> print(time.__name__)
time

This is shown in the Tutorial, by the way.

Sign up to request clarification or add additional context in comments.

Comments

-2

Simply import a module and print its name using _name

3 Comments

umm sorry it gives me an error time has no attribute name
import random as a print(a.__name__) Does this works for you?
yes ofc :D this answer is already answered in the upper one

Your Answer

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