2

Inside the parent class basehandler.py, there are several import statements, a constant, and a class:

import os
import sys
import cgi
import json

JINJA_ENVIRONMENT = jinja2.Environment(foobar)

class BaseHandler(webapp2.RequestHandler):
    pass

Another module, main.py then imports this parent module with from basehandler import *

If we use from basehandler import BaseHandler or import basehandler so as to avoid the from foo import * statement, then the modules that the parent class imports are not received and the program throws an exception.

How do I avoid from foo import * while still correctly importing the parent module with the modules that it imports?

Ben
  • 5,085
  • 9
  • 39
  • 58
  • When asking about an error or exception thrown by your program, always include the full text and traceback of the exception, please. – Abe Karplus Mar 20 '14 at 16:00
  • Well, you could just ``import BaseHandler, os, sys`` but that requires you to know which modules are included in the ``basehandler`` module. Is there a reason why you don't just ``import os, sys`` in both files? – Moritz Mar 20 '14 at 16:02
  • @moritz:: taht is not how python works - the O.P. is probably making something else wrong. – jsbueno Mar 20 '14 at 16:41
  • That should just work - please put your "main.py" import listing, and the stacktrace (error message containign filenames and linenumbers) – jsbueno Mar 20 '14 at 16:42
  • @jsbueno maybe I'm not understanding the question correctly. OP wants to import the imports from the parent module without explicitly writing them out in ``main.py``, right? – Moritz Mar 20 '14 at 17:56
  • @Moritz: it is right up to there - the "why" the OP might want to do that is the "wrong" part - Python does not need you to import a module's imports in order to work. Your answer is fine otherwise. – jsbueno Mar 21 '14 at 18:56

3 Answers3

1

Referencing this answer, you can define a function in basehandler.py that yields all imported modules:

import os,sys,cgi,json
import types

def get_imports():
    for name, val in globals().items():
        if isinstance(val, types.ModuleType):
            yield val.__name__

...and then import that function in main.py and use exec to import the relevant modules:

from basehandler import BaseHandler, get_imports

for i in get_imports():
    exec "import %s" % i

print os.getcwd() #this will work

Sort of a gnarly way of doing it though, is there a specific reason you're not just re-importing the list of modules in main.py?

Community
  • 1
  • 1
Moritz
  • 4,565
  • 2
  • 23
  • 21
  • don't use `exec` unless you must (you don't need it here). The modules are already imported; you don't need to import them again e.g., they are available as `basehandler.os`, etc if you used `import basehandler`. If you need to import modules then use `__import__` or `importlib.import_module` functions. See [my answer for a cleaner solution](http://stackoverflow.com/a/22603325/4279) – jfs Mar 24 '14 at 07:34
1

then the modules that the parent class imports are not received and the program throws an exception.

If your program uses a module then you should import it in the module itself, not in some other module e.g., use import os in your program instead of from foo import * where foo module imports os inside.


To answer the question in the title: to make all imported modules from another module available without a wild-card import:

import inspect 
import foo # your other module

# get all modules from `foo` and make the names available
globals().update(inspect.getmembers(foo, inspect.ismodule)) #XXX don't do it

It is like from foo import * that imports only modules. The same warnings against wild-card imports are applicable here.

jfs
  • 399,953
  • 195
  • 994
  • 1,670
0

I say there is something else wrong there - Importing your "basehandler" module from elsewhere should just work: the names, modules, classes and objects it needs are bound to its own namespace, and are present in the globals dictionary of any code defined in that module - regardless of you getting it with a from basehandler import BaseHandler statement.

Please, post your full traceback so that people can find out what is actually happening there.

jsbueno
  • 99,910
  • 10
  • 151
  • 209