开发者

Get the path to Django itself

开发者 https://www.devze.com 2022-12-30 17:33 出处:网络
I\'ve got some code that runs on every (nearly) every admin request but doesn\'t have access to the \'request\' object.

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 pa开发者_C百科th 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, use import django; django.__file__.

Now, if Django hasn't been imported by the current Python process (eg, you're calling os.system("myscript.py") and myscript.py needs to determine Django's path), then import django will be a bit wasteful. But spawning a new process on each request is also fairly wasteful… So if efficiency is important, it might be better import myscript anyway.

*: actually it will set a value in a dictionary… But that's "nothing".

0

精彩评论

暂无评论...
验证码 换一张
取 消