from multiple packages import * in python

时间:2015-11-12 12:04:06

标签: python python-2.7

I know this is allowed in Python2

from module1 import *
from module2 import *
...

I was wondering if there was a way I could do the same in one line. Doesn't look like this is allowed :

from module1, module2 ... import *

I don't want something like

import module1, module2 ...

because that would need me to access functions like

module_name.function_name(...)

I want to access them directly by name. If this has been asked before, please do point me in the right direction. Thanks a lot !

2 个答案:

答案 0 :(得分:2)

我真的建议你不要导入*

  • for module1我们有className1
  • 对于module2,我们有className1

然后命名冲突!!

推介

import pandas as pd
import numpy as np

答案 1 :(得分:2)

我建议一般反对from module import *,绝对反对在一行中完成所有操作。只是为了好玩,你可以这样做:

from itertools import chain
modnames = 'os sys pandas collections'.split()
locals().update(chain.from_iterable(__import__(modname).__dict__.iteritems() for modname in modnames))