简体   繁体   中英

How to print(dir(module)) using a variable in Python?

import PyQt5
from PyQt5.QtWidgets import *

print(dir(PyQt5.QtWidgets))

That above code runs perfectly. But when I use a variable it gives an error.

qw="QtWidgets"
print(dir(PyQt5.qw))

AttributeError: module 'PyQt5' has no attribute 'qw' . Is there anyway I can do that? The reason I use variable is because I want a user input to choose what modules to print(dir()).

The getattr built-in function will let you look up an attribute by name:

qw = "QtWidgets"
print(dir(getattr(PyQt5, qw)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM