12

I've generally been told that the following is bad practice.

from module import *

The main reasoning (or so I've been told), is that you could possibly import something you didn't want, and it could shadow a similarly named function or class from another module.

However, what about PyQt

from PyQt4.QtCore import *

Every example I've ever seen is written this way, mainly because everything exported from Qt starts with "Q", so it's not going to shadow anything.

What's the concensus? Is it always bad to use * imports?

EDIT:

Just to be clear, this question is specifically in regards to using PyQt4. It has nothing to do with the way I am designing some other project.

Basically, I've found that coding to PEP8 has improved my code readability, except with regards to importing PyQt4, and so I've disregarded the frowns from purists until now. But now my dev group is deciding on one convention and I'm wondering if this is a scenario "where practicality beats purity", or if I should just suck it up and deal with monstrous PyQt4 imports

from PyQt4.QtGui import QComboBox, QLineEdit, QLayout, Q;lakdfaf.......
2
  • “Although certain modules are designed to export only names that follow certain patterns when you use import *, it is still considered bad practise in production code.” docs.python.org/tutorial/modules.html
    – Josh Lee
    May 4, 2011 at 0:38
  • 4
    yeah, I would prefer import PyQt4.QtGui as QT to that potentially ever-growing list of symbols being pulled into the local namespace.
    – samplebias
    May 4, 2011 at 0:54

8 Answers 8

9

This can sort of turn into a religious war. It's a matter of whether you want to be explicit or whether you want to avoid being too verbose. In general, following the Zen of Python, it's better to be explicit, but sometimes people just don't find it practical to list every import from particular module.

1
  • So don't import them - reference them indirectly via the module (and give it a short name as an alias)
    – ncoghlan
    May 4, 2011 at 7:22
5

My general rule is that if I didn't write the module, I don't import it all. My biggest fear is actually over writing local variables that might have been defined in the imported module. So to keep from having to type in long module names, I use the import as feature. Using your module as an example I would do the following:

import PyQt4.QtCore as qt

That being said, I have many support modules that I write that I will import everything. Like the pyqt module, I name them with a descriptive name that helps show which module it came from.

Edit per comment
When I use import*, my support modules do not contain classes or anything that can create a new instance. They tend to be groups of functions that modify existing instances only. To help clarify my opinion: If I am the owner of the source code and I will be the primary maintainer, I will use the import* otherwise I would use the import as.

Another reason that I use the import as feature is to allow me to mock modules for debugging purposes. In a project that I am working on now, I use pyVisa to talk to a number of GPIB devices. When I'm not connected to the devices GPIB network, I can use a dummy_visa module to write to the stdout(to verify I am sending the correct format) and return a random number (to test my application). See below

if visa_debug:
    import dummy_visa as visa
else:
    import visa
gpib = visa.Instrument("GPIB0::10")
gpib.write("MEAS:VOLT?")
3
  • You kind of contradicted yourself. With PyQt, unless you're naming your classes QClass, you're not going to overwrite anything, which seems to be your sole reason for not using from x import *. If you are sure it's not going to overwrite anything, would you or would you not use *?
    – user297250
    May 4, 2011 at 1:17
  • I suppose I should have been a bit more concise in my answer. After re-reading it, it does sound that i contradicted my self :). See edit.
    – Adam Lewis
    May 4, 2011 at 1:30
  • I see the benefit of import as for testing; though, I would normally just change my environment or pythonpath for something like this. It's also unlikely I will be testing my own special builds of PyQt this way.
    – user297250
    May 4, 2011 at 2:23
5

Making an explicit exception for modules that already include a namespace in their naming convention (such as the Q* of PyQT) is perfectly reasonably. However, I recommend being clear that the default is still "don't use it" and simply list this exception in your coding guidelines.

import * is also acceptable when it is used as a namespace manipulation trick within an application (the two forms of that I am familiar with are optional C acceleration modules that are imported at the end of the pure Python version, and "flattening" a package namespace in __init__). The key point is that the importing module and the module being imported are under the control of the same set of developers, and hence avoiding namespace clashes is completely within their control.

The final exception is for convenience at the interactive prompt.

In other situations, it is best to either import specific names or reference them indirectly through the module name (or, if there are some commonly reference items, do both:

import module # Can access anything from module import a, b, c # But we reference these a lot, so retrieve them directly

3

Tutorial, chapter 6:

Note that in general the practice of importing * from a module or package is frowned upon, since it often causes poorly readable code. However, it is okay to use it to save typing in interactive sessions.

Tutorial, chapter 10:

Be sure to use the import os style instead of from os import *. This will keep os.open() from shadowing the built-in open() function which operates much differently.

So it seems that it is definitely a bad idea sometimes; not-the-best idea most of the time; and an acceptable shortcut in cases where you'd want to type less, eg in interactive sessions.

0
3

I find import * gets abused, and can become a maintenance headache, so I avoid it for this and the other reasons you state. That said I feel it's okay for short interactive sessions, e.g. from pylab import *.

In production code, for packages like PyQt4.QtCore where you plan to use many of the symbols, I'd use one of the following syntaxes which make it explicit which namespace the symbols come from:

from PyQt4 import QtCore
# explicit where the symbol came from
QtCore.QTime()

import PyQt4.QtCore as QT
# less desirable since you need to look in the header to find out what QT is
# but I still prefer it to import *
QT.QTime()
5
  • I once got yelled at for using import ... as.
    – Josh Lee
    May 4, 2011 at 0:39
  • @jleedev, samplebias, funny, I see import numpy as np all the time on stackoverflow, and have been a bit surprised not to see anyone complain.
    – senderle
    May 4, 2011 at 0:48
  • I hear ya. :-) If you are using ~50 different symbols from a module with a long namespace, import .. as can save some typing (long as that convention is accepted by the other developers you work with).
    – samplebias
    May 4, 2011 at 0:50
  • @senderle, well then I'll be the first complainer. I experience strong abhorrence from expressions written with numpy functions prefixed with obscure and ugly np.prefix. It reminds me the habit of specifying variable's type through identifier prefix: nWidth, lpszName. Ugly and unneeded. Either you decide to be explicit and use clear numpy. prefix, or you decide to bear the danger of namespace clashes and import *.
    – ulidtko
    May 4, 2011 at 1:21
  • 2
    Ugly and unneeded as import numpy as np is, I can't see it as preferable to import *. I count over 500 symbols in numpy.*, including fun ones like min, max, etc.
    – samplebias
    May 4, 2011 at 1:37
3

In general, if you're going to use from X import Y, it's a good idea to be explicit about what you're importing. That's not just because it's safer, but also because it makes your code more readable (and upgrades to the third-party modules you're using won't have as much potential to incidentally break your code).

In some code examples demonstrating big packages, like Qt or matplotlib, the examples will use from module import * because they're often only importing from one module, and it saves typing and lets their example code get to the point. There's no reason you can't do it in your code, but at least use it in moderation, especially if it's in big source files or other people will be looking at your code.

3
  • 1
    Also, besides global variables, quite a few modules import re or os or whatever in the main namespace, and that could potentially lead to strange results, like if they do import my_cool_new_re as re. Just a thought.
    – Mu Mind
    May 4, 2011 at 0:47
  • 1
    that's what __all__ is for.
    – ulidtko
    May 4, 2011 at 1:24
  • @ulidtko, right, if you're writing the imported code you should use __all__, but my point is that you're depending on the module author to set __all__ properly when you import like this, and many module authors don't.
    – Mu Mind
    May 4, 2011 at 1:49
2

The PyQt design is water under the bridge. I'm not sure it's the best thing, but it's probably influenced by the way Qt was designed. AFAIK, Qt didn't use C++ namespaces historically (I'm not sure if it uses them now), and therefore had to use prefixes. But all of those design decisions probably happened more than 10 years ago, and that shouldn't affect your design decisions now.

I know that if I was designing a library now, I would definitely choose to use package namespaces (which you would import explicitly in Python) over prefixes. Let's say my prefix is Pfx - if the library user doesn't care writing PfxFunc() every time he needs a function, he certainly wouldn't care about the extra character in Pfx.Func(), especially when he can use import with specific symbol names to shorten that even more.

1
  • I'm only talking about PyQt4 not designing a library.
    – user297250
    May 4, 2011 at 0:53
1

Consider this case

from foo import *
from bar import *

x=baz()

Now suppose foo has a function called baz() that we are using in our code and everything is working fine. Months or years pass, the author of bar adds a function called baz(). Someone updates the egg ang bingo - we have a potentially hard to detect bug in the program.

Not to mention that just looking at those three lines of code, I can't tell where baz comes from without going and looking at foo and bar

The only time I'd use import * would be in the interpreter to save some typing

1
  • Thanks, but read the EDIT, we're specifically referring to PyQt4. This "shadowing" concern is the same one most often mentioned, but it does not apply to PyQt4, we know that import * is safe (at least from the shadowing part) and its very easy to see that QString is part of the PyQt4 import. I'm looking for other reasons, besides the "it might shadow an existing module" to not use import *
    – user297250
    May 4, 2011 at 2:05

Your Answer

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