I’ve got some code that runs on every (nearly) every admin request but doesn’t have access to the ‘request’ object.
I need to find the path to Django installation. I could do:
import django
django_path = django.__file__
but that seems rather wasteful in the middle of a request.
Does putting the import at the start of the module waste memory? I’m fairly sure I’m missing an obvious trick here.
So long as Django has already been
imported in the Python process (which it has, if your code is, for example, in a view function), importing it again won’t do “anything”* — so go nuts, useimport django; django.__file__.Now, if Django hasn’t been imported by the current Python process (eg, you’re calling
os.system("myscript.py")andmyscript.pyneeds to determine Django’s path), thenimport djangowill be a bit wasteful. But spawning a new process on each request is also fairly wasteful… So if efficiency is important, it might be betterimport myscriptanyway.*: actually it will set a value in a dictionary… But that’s “nothing”.