Difference between “import X” and “from X import *

2019-01-11 22:46发布

This question already has an answer here:

In Python, I'm not really clear on the difference between the following two lines of code:

import X

or

from X import *

Don't they both just import everything from the module X? What's the difference?

标签: python import
7条回答
戒情不戒烟
2楼-- · 2019-01-11 23:51

Let's use this as an example of our module definition

def my_func():
    return 1

the from import will pull everything into the current namespace

from X import *
print my_func()

the normal import will create a module object called X in the current namespace

import X
print x.my_func()

in addition with from X import my_func can be used to only import that functionality.

查看更多
登录 后发表回答