Django / Python: import once, use everywhere

I have the following structure for my views directory.

 views |--__init__.py |--a_management.py |--b_management.py |--c_management.py |--d_management.py |--e_management.py 

and __init__.py starts with the following:

 from a_management import * from b_management import * from c_management import * from d_management import * from e_management import * ......... ...etc... ......... 

In each of the files ( a_management.py , b_management.py ...) I need to write the same code import modules:

 import sys,os from django.shortcuts import render, redirect from django.http import HttpResponseRedirect ......... ...etc... ......... 

Is it possible to perform all module import only in __init__.py ?

When I tried to, anyway it seems that the import can not be found, and I get errors such as: NameError: global name 'sys' is not defined

+6
source share
2 answers

How to use a common script for all import systems?

By the way, I agree that import * not the biggest idea. This makes sense when using the importer , but I'm not sure about your overall setup. In addition, you need to be careful about circular imports.

So, my answer is specifically intended only so that I need to write the same code import modules :, and not regarding the meaning of your installation as a whole.

Proof of concept, importer is what you really need .:

 β”œβ”€β”€ pack β”‚ β”œβ”€β”€ __init__.py β”‚ β”œβ”€β”€ importer.py β”‚ β”œβ”€β”€ mgmt_1.py β”‚ β”œβ”€β”€ mgmt_2.py └── test.py 

test.py

 import pack pack.foo_1() pack.foo_2() 

init.py from mgmt_1 import * from import mgmt_2 *

mgmt_1.py

 from .importer import * print "sys", sys print "os", os def foo_1(): print "foo_1" 

mgmt_2.py:

 from .importer import * print "sys", sys print "os", os def foo_2(): print "foo_2", dir(sys)[0:5] 

importer.py

 import sys import os 

exit:

 sys <module 'sys' (built-in)> os <module 'os' from '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.pyc'> sys <module 'sys' (built-in)> os <module 'os' from '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.pyc'> foo_1 foo_2:sys ['__displayhook__', '__doc__', '__excepthook__', '__name__', '__package__'] 
+2
source

If each management module needs access to sys , then all of them must import it. Nothing like this. (And indeed, if they all need it, then they all need to import it. It's not bad.)

You can save a bit of input by specifying __init__ import sys , os and whtever else, and each management module can execute from __init__ import * , thereby "inheriting" all imported modules from __init__ .

Well, if you can’t do it this way, because __init__ already imports material from the management modules, so the above sentence will result in cyclic imports that don't matter.

I don’t know the specifics of your application, but I have to believe that there is a better way to organize your modules in order to avoid so many reimports, and especially such use of import * . Generally, you want to use this as little as possible.

+3
source

All Articles