0
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()).

1 Answer 1

1

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

qw = "QtWidgets"
print(dir(getattr(PyQt5, qw)))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.