tags:

views:

127

answers:

2

Hi.

So I just met a strange so-called bug. Because this work on my other .py files, but just on this file it suddenly stopped working.

from tuttobelo.management.models import *

The above used to work, but it stopped working all of a sudden, and I had to replace it with the bottom.

from tuttobelo.management.models import Preferences, ProductVariant, UserSeller, ProductOwner, ProductModel, ProductVariant
from tuttobelo.management.models import ProductMeta, ShippingMethods

I know the following is the better way of coding, however ALL of the models mentioned in models are used, so my question is, what possible reasons can wildcard stop working?

The error I got was that the model I was trying to import does not exist, only if I remove the wildcard and import the name of the model could I get it imported properly.

Thanks!

+4  A: 

Maybe the models module has an __all__ which does not include what you're looking for. Anyway, from ... import * is never a good idea in production code -- we always meant the import * feature for interactive exploratory use, not production use. Specifically import the module you need -- use that name to qualify names that belong there -- and you'll be vastly happier in the long run!-)

Alex Martelli
What if i have like 100 names to import?Thing is import * works on other py files just not on that file. Thanks for your input tho ;)
nubela
import * is BAD, don't use it. Instead use `import tuttobelo.management.models as tmm; tmm.ProductMeta ...`. Fix your code, and the problem will get fixed as well.
SpliFF
You won't -- as a practical matter -- have 100 names to import. That kind of model is bad.
S.Lott
@nubela, you have ONE "thing" to import: "from tuttobelo.management import models". Then you use models.Preferences, models.ProductMeta, and so on, instead of barenames. When a module's name is too long (not the case here, IMHO) you can use the "as m" clause in the import and "m." instead of "models." later.
Alex Martelli
+1  A: 

There are some cases in Python where importing with * will not yield anything. In your example, if tuttobelo.management.models is a package (i.e. a directory with an __init__.py) with the files Preferences.py, ProductVariant.py, etc in it, importing with star will not work, unless you already have imported it explicitly somewhere else.

This can be solved by putting in the __init__.py:

__all__ = ['Preferences', 'ProductVariant', 'UserSeller', <etc...> ]

This will make it possible to do import * again, but as noted, that's a horrible coding style for several reasons. One, tools like pyflakes and pylint, and code introspection in your editor, stops working. Secondly, you end up putting a lot of names in the local namespace, which in your code you don't know where they come from, and secondly you can get clashes in names like this.

A better way is to do

from tuttobelo.management import models

And then refer to the other things by models.Preferences, models.ProductVariant etc. This however will not work with the __all__ variable. Instead you need to import the modules from the __init__.py:

import Preferences, ProductVariant, UserSeller, ProductOwner, <etc...>

The drawback of this is that all modules get imported even if you don't use them, which means it will take more memory.

Lennart Regebro