Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to get only the module name ModuleNotFoundError

How only to print the module name with ModuleNotFoundError arguments in python.

my code:

try:
    import requests
except ModuleNotFoundError as error1:
    print(error1)

In this case I am getting output as below:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

No module named ‘requests’

I want to be printed only the name of the module that met the exception.

output I need:

‘requests’

Regards,
Vishnu

>Solution :

The module name in an ModuleNotFoundError is accessed by the variable name

try:
    import this_module_doesnt_exist
except ModuleNotFoundError as error1:
    print(error1.name)

Output:

this_module_doesnt_exist
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading