The (major part of the work of) import is NOT done every time through. On subsequent passes, the import statement merely looks up the module in sys.modules. This is only slightly more of a performance hit than a direct dictionary look up (depending on how much the compiler inlines when it sees an import statement) - that is, it's not worth worrying about.
It is often possible to refactor code so that these techniques aren't necessary, for example, breaking one module, A, into A1 and A2 modules, where A1 has no code that depends on B, but does have what B needs from A (so both A2 and B import A1). But this often obfuscates the code. Readable code is always a priority for me. Bill On Mon, Mar 7, 2011 at 9:55 AM, Brian Bouterse <bmbou...@gmail.com> wrote: > I too "protect" code from being interpreted at import time by putting import > statements directly in the tasks that need them. I don't think there is > anything wrong with this approach the from a Python perspective other than > possibly these: > * imports are parsed at runtime and thus an import occurs for each time the > code is run <=== performance problem > * import errors aren't discovered until runtime > I too am looking for a better way to do this. > Brian > On Mon, Mar 7, 2011 at 8:25 AM, Shawn Milochik <sh...@milochik.com> wrote: >> >> If there's a "correct" way to do this I'd love to hear it also. >> >> At present I do the model imports in tasks.py within the task that >> requires the model. This is because in my models.py I import tasks >> from tasks.py. >> >> Shawn >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Django users" group. >> To post to this group, send email to django-users@googlegroups.com. >> To unsubscribe from this group, send email to >> django-users+unsubscr...@googlegroups.com. >> For more options, visit this group at >> http://groups.google.com/group/django-users?hl=en. >> > > > > -- > Brian Bouterse > ITng Services > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-users@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.