9

I have several different modules, and I need to import one of them depending on different situations, for example:

if check_situation() == 1:
    import helper_1 as helper
elif check_situation() == 2:
    import helper_2 as helper
elif ...
    ...
else:
    import helper_0 as helper

these helpers contain same dictionaries dict01, dict02, dict03...but have different values to be called in different situations.

But this has some problems:

  1. import sentences are all written in the top of a file, but check_situation() function here needs prerequisites so that it's now far from top.
  2. more than 1 file needs this helper module, so it's hard and ugly to use this kind of import.

So, how to re-arrange these helpers?

4 Answers 4

5

Firstly, there is no strict requirement that import statements need to be at the top of a file, it is more a style guide thing.

Now, importlib and a dict can be used to replace your if/elif chain:

import importlib

d = {1: 'helper_1', 2: 'helper_2'}
helper = importlib.import_module(d.get(check_situation(), 'helper_0'))

But it's just syntactic sugar really, I suspect you have bigger fish to fry. It sounds like you need to reconsider your data structures, and redesign code.

Anytime you have variables named like dict01, dict02, dict03 it is a sure sign that you need to gear up a level, and have some container of dicts e.g. a list of them. Same goes for your 'helper' module names ending with digits.

2
  • yes I think it really need redesign. as original script, if import i can just use helper.dictA, helper.dictB without aware of which helper used. How to achieve this effect?
    – ThunderEX
    Mar 13, 2013 at 3:20
  • You could alias them in the modules, i.e. my_dict = dictA in one helper module and my_dict = dictB in the other. Then access with helper.my_dict. But I've got to stress that this solves a technical problem, not a design problem.
    – wim
    Mar 13, 2013 at 3:31
5

You can use __import__(), it accepts a string and returns that module:

helper=__import__("helper_{0}".format(check_situation()))

example :

In [10]: mod=__import__("{0}math".format(raw_input("enter 'c' or '': ")))
enter 'c' or '': c             #imports cmath

In [11]: mod.__file__
Out[11]: '/usr/local/lib/python2.7/lib-dynload/cmath.so'

In [12]: mod=__import__("{0}math".format(raw_input("enter 'c' or '': ")))
enter 'c' or '': 

In [13]: mod.__file__
Out[13]: '/usr/local/lib/python2.7/lib-dynload/math.so'

As pointed out by @wim and from python3.x docs on __import__():

Import a module. Because this function is meant for use by the Python interpreter and not for general use it is better to use importlib.import_module() to programmatically import a module.

5
  • 2
    -1 because read the docstring of __import__ - "this function is meant for use by the Python interpreter and not for general use it is better to use importlib.import_module() to programmatically import a module"
    – wim
    Mar 13, 2013 at 5:28
  • also, you've kind of ignored the "problem behind the problem", i.e. that the OP is using data in their variable names
    – wim
    Mar 13, 2013 at 5:40
  • @wim I can't seem to find the same thing in the docstring or the docs, from the docs: This function is invoked by the import statement. Direct use of __import__() is rare, except in cases where you want to import a module whose name is only known at runtime. Mar 13, 2013 at 17:44
  • You are probably using an older version of python. Check in __import__.__doc__ on v3.2.3 (it seems this snippet of information is not there in 2.7.3)
    – wim
    Mar 14, 2013 at 3:23
  • @wim I've added a note about that, but my above example works fine in py3.x Mar 14, 2013 at 4:05
1

Solve it myself, refered to @Michael Scott Cuthbert

# re_direct.py

import this_module
import that_module

wanted = None


# caller.py
import re-direct

'''
many prerequisites
'''
def imp_now(case):
    import re_direct
    if case1:
        re_direct.wanted = re_direct.this_module
    elif case2:
        re_direct.wanted = re_direct.that_module

then, if in caller, I call that imp_now, then wanted, no matter called in caller file or other file calling this wanted, will all be re-directed to this_or_that_module.

also, for I import re_direct only in a function, so you will not see this module anywhere else, but only see wanted.

2
  • Also note that once you import wanted you can't change it by assigning to it from re_direct.wanted. The import redirection is totally unnecessary here.
    – Davide R.
    Mar 13, 2013 at 9:11
  • @DavideR. yes i find it still don't work well. I don't care about load all modules or it can't change any more. Then how can I do to make all callers import this_module or that_module?
    – ThunderEX
    Mar 14, 2013 at 0:50
1

I agree that the approaches given in the other answers are closer to the main question posed in your title, but if the overhead on importing modules is low (as importing a couple of dictionaries likely is) and there are no side-effects to importing, in this case, you may be better off importing them all and selecting the proper dictionary later in the modules:

import helper_0
import helper_1
...
helperList = [helper_0, helper_1, helper_2...]
...
helper = helperList[check_situation()]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.