This is strictly a Python question, nothing Django-specific, but I've found
this site to be helpful in explaining the different ways to import things:

http://effbot.org/zone/import-confusion.htm

In general, using 'from blah import *' is frowned upon except in very
specific cases. While it is easier up front, it makes the code hard to read
later on, since you aren't explicitly dating where the functions or classes
you are using are coming from.

In general, you'll want to be as specific as possible when importing things
to keep your code clean and easy to read, even if it does make it a bit
longer.

For example:

from CatAppA import *
from CatAppB import *

my_cat = get_cat('Katy Purry')

If we were inspecting this code, we wouldn't know whether or not get_cat()
comes from CatApp A or B, which makes troubleshooting more difficult,
especially if both apps (or modules) define a get_cat() function. It would
be much better to say:

from CatAppA import get_cat
from CatAppB import say_meow

my_cat = get_cat('Katy Purry')

Now we know that get_cat() comes from CatAppA.

The other aspect to consider is memory use. If a module defines 500
functions, doing 'import modulename' will load all 500 in memory, rather
than just picking out the one or two you might use.

Aside from that, all of your references would be prefixed with
'modulename', which may or may not make the code more readable.

In general, I import everything explicitly, and I use relative import paths
for imports in files that are in the same directory:

from .models import Cat

Yes, it makes my import lines much longer, but having them more explicit
makes the code much easier to read on larger chunks of code.

There it's some contention as to whether or not to use relative imports,
some devs prefer to use the more explicit appname.models version. Both do
the same thing.

Check out the Django source code to see how they handle imports, and
definitely look at PEP8:

https://www.python.org/dev/peps/pep-0008/#imports

-James
On Jun 19, 2015 7:32 PM, "Peith Vergil" <peith.ver...@gmail.com> wrote:

> It depends on how your project files are structured. If you have a file
> structure that looks like this:
>
> app/
>     __init__.py
>     models.py
>     views.py
>     urls.py
>
> Then, you can simply use relative imports.
>
> In views.py:
> from models import *
>
> In urls.py:
> from views import *
>
> If, for example, your models live in a separate Django app (i.e. a
> separate Python package):
>
> app1/
>     __init__.py
>     views.py
>     urls.py
> app2/
>     __init__.py
>     models.py
>
> Then, you have to import your models using its full path so Python will
> know where to look for it.
>
> In app1/views.py:
> from app2.models import *
>
> You should familiarize yourself on how Python imports work since this is
> more of a Python issue rather than a Django issue.
> On Jun 20, 2015 09:10, <jorrit...@gmail.com> wrote:
>
>> This is mostly a cosmetic question, and I could be completely wrong
>> because I'm fairly new to Django, or it could be that there is a perfectly
>> logical explanation for this, but here goes:
>>
>> It seems the code required to import views in urls.py and models in
>> views.py is inconsistent (and in the case of urls.py perhaps redudant).
>>
>> To import views in urls.py I have to use
>>
>> from <appname> import views
>>
>> ...while in views.py I can simply write
>>
>> from models import *
>>
>> Why do I need to reference the appname in urls.py but not in views.py? Or
>> is there a reason for this?
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to django-users+unsubscr...@googlegroups.com.
>> To post to this group, send email to django-users@googlegroups.com.
>> Visit this group at http://groups.google.com/group/django-users.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/03eb4740-6503-4757-ae09-9183ac2b8502%40googlegroups.com
>> <https://groups.google.com/d/msgid/django-users/03eb4740-6503-4757-ae09-9183ac2b8502%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>  --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAMHY-KdkaS0RBdS4F4mi%3DaGHTP9uZThJVCesXM9%3DGbVbZoteaw%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CAMHY-KdkaS0RBdS4F4mi%3DaGHTP9uZThJVCesXM9%3DGbVbZoteaw%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CA%2Be%2BciV2z%2BqwWjynw40NeqUuW3JzJ7x%2Bx63pgPzZDSVXpj5O-Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to