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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
After
import x, you can refer to things inxlikex.something. Afterfrom x import *, you can refer to things inxdirectly just assomething. Because the second form imports the names directly into the local namespace, it creates the potential for conflicts if you import things from many modules. Therefore, thefrom x import *is discouraged.You can also do
from x import something, which imports just thesomethinginto the local namespace, not everything inx. This is better because if you list the names you import, you know exactly what you are importing and it’s easier to avoid name conflicts.