1

Why do neither of these statements import QtCore, QtGui, QtNetwork or any of the others? I've searched so long and can't find anyone to answer such a simple question. Or at least that's what I think it is.

import PyQt4
from PyQt4 import *

Instead I have to do:

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtNetwork import *

Plus a bunch more. Any shortcuts to grab them all?

Edit: My solutions was to go through the libraries that I was using (for Py2exe) and just copy their imports. That worked. Still, but not as tedious.

User
  • 23,729
  • 38
  • 124
  • 207
  • possible duplicate of [Why does from scipy import spatial work, while scipy.spatial doesn't work after import scipy?](http://stackoverflow.com/questions/21071715/why-does-from-scipy-import-spatial-work-while-scipy-spatial-doesnt-work-after) – falsetru Jan 24 '14 at 03:50
  • @falsetru I don't know if its a duplicate. The asker wants to know if they can import *everything* in the PyQt4 namespace into the global namespace. –  Jan 24 '14 at 03:54
  • @LegoStormtroopr, OP also asked **why** `from pkg import *` does not import subpackages. – falsetru Jan 24 '14 at 04:01
  • @falsetru A true, I glossed over that sentence and jumped right to "no". –  Jan 24 '14 at 04:05

2 Answers2

3

Don't use import *, namespaces exist for a good reason.

Import the modules you need, in the modules you need them in.

This code might require a few extra letters, but you'd only know QUrl was a Qt module by convention, not surety.

 from PyQt import QtCore
 u = QtCore.QUrl

The above code means when you are examining the second line you know for certain that it is a Qt object from a specific module and nothing else.

Community
  • 1
  • 1
3

If you want to import all PyQt4 classes into a single namespace, you can do:

from PyQt4 import Qt
ekhumoro
  • 115,249
  • 20
  • 229
  • 336