What does import do?
Is it the same as copying the imported file into your new program?
for example:
-
import goodbyeworld
-
-
print "hello world"
-
so helloworld.py would have all of goodbyeworld then all the rest of helloworld?
Almost like that:
copies all the names (variables, function, classes, etc) into the namespace of the current module. So if you have module1:
- # file module1.py
-
def MyFucntion():
-
pass
and a second module, module2 imports it:
- # file module2.py
-
import module1 # import the module - actually runs the script
-
module1.MyFunction() # call a function in an imported module
If you have commands or assignments in a module that you import, those commands and assignments get run just like every other part of the script (don't forget the def is just a command the creates a function). So if module1 has
that will run when it is imported. I hope that's not too confusing.