DreamerNo56
DreamerNo56

Reputation: 213

Imported module but still need to use full name

I've been able to successfully import QtWidgets from PyQt5 and this works fine in code however if I don't use the full QtWidgets name in the call I get an error. Below works...

import sys
from PyQt5 import  QtWidgets

app = QtWidgets.QApplication(sys.argv)

Yet if do...

import sys
from PyQt5 import  QtWidgets

app = QApplication(sys.argv)

I get ...

NameError: name 'QApplication' is not defined

Upvotes: 1

Views: 164

Answers (2)

Chris
Chris

Reputation: 22963

Your misunderstanding how Python import statements work. When importing a module in Python, only the module directly imported is include in the local symbol table.

Thus, if you have not directly imported a name, it cannot be used as a standalone identifier in the current namespace. If you want this behavior, directly import the QApplication name from the QtWidgets namespace:

from PyQt5.QtWidgets import QApplication

Upvotes: 5

Vibhutha Kumarage
Vibhutha Kumarage

Reputation: 1399

Import modules like this:

import sys
from PyQt5.QtWidgets import *

app = QApplication(sys.argv)

Upvotes: 0

Related Questions