Django/Python: Import once, use everywhere -
i have following structure views
directory.
views |--__init__.py |--a_management.py |--b_management.py |--c_management.py |--d_management.py |--e_management.py
and __init__.py
starts following:
from a_management import * b_management import * c_management import * d_management import * e_management import * ......... ...etc... .........
in each of files (a_management.py
,b_management.py
...) need write same code importing modules:
import sys,os django.shortcuts import render, redirect django.http import httpresponseredirect ......... ...etc... .........
is possible perform module imports in __init__.py
?
when tried, still seems imports not found , errors like: nameerror: global name 'sys' not defined
what using shared script system imports?
btw, agree import *
not greatest of idea. makes sense in use of importer, not sure in general setup. also, need careful circular imports.
so, answer geared towards i need write same code importing modules:, not towards whether setup whole makes sense.
proof of concept, importer care about.:
├── 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 mgmt_1 import * mgmt_2 import *
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
output:
sys <module 'sys' (built-in)> os <module 'os' '/opt/local/library/frameworks/python.framework/versions/2.7/lib/python2.7/os.pyc'> sys <module 'sys' (built-in)> os <module 'os' '/opt/local/library/frameworks/python.framework/versions/2.7/lib/python2.7/os.pyc'> foo_1 foo_2:sys ['__displayhook__', '__doc__', '__excepthook__', '__name__', '__package__']
Comments
Post a Comment