Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 importing modules:

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

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

When I tried, it still seems like the imports are not found and I get errors like: NameError: global name 'sys' is not defined

like image 521
Jedi Avatar asked Jul 15 '16 18:07

Jedi


2 Answers

If each management module needs access to sys, then they all must import it. No way around that. (And really, if they all need it, then they all should import it. It's not a bad thing.)

You could save a bit of typing by having __init__ import sys, os and whtever else is needed, and then each management module can do from __init__ import *, thus "inheriting" all the imported modules from __init__.

Well, except you can't do it this way, because __init__ already imports stuff from the management modules, so the above suggestion would result in circular imports, which are a no-no.

I don't know the specifics of your application, but I have to believe that there's a better way to organize your modules to avoid so much repeated importing, and especially so much use of import *. Generally you want to use that as little as possible.

like image 193
John Gordon Avatar answered Oct 23 '22 03:10

John Gordon


What about using a shared script to do all the system imports?

BTW, I agree that import * is not the greatest of idea. It makes sense in my use of importer, but I am not so sure in your general setup. Also, you need to careful about circular imports.

So, my answer is specifically only geared towards I need to write the same code importing modules:, not towards whether your setup as a whole makes sense.

Proof of concept, importer is what you really 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 from mgmt_1 import * from 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' 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__']
like image 36
JL Peyret Avatar answered Oct 23 '22 04:10

JL Peyret