Videos
I'm kinda confused about what the difference is between importing an entire module like:
import packers
and importing all functions from a module, like:
from packers import *
I know it's frowned upon to import all the functions in a module since it can interfere with existing functions, but is the main difference how you would call functions in the main code?
So when importing an entire module, you would use dot notation like:
packers.rodgers()
Whereas, when you import all functions in a module, you would have to use that function's original name (which could lead to those interference issues)?
I also know that the first method of calling entire modules makes it easier to import from specific packages, like:
from nfl import packers
I think this is the right idea but I just want to double check. Thanks for your help!
The recommended way for Python 2.7 and 3.1 and later is to use importlib module:
importlib.import_module(name, package=None)Import a module. The name argument specifies what module to import in absolute or relative terms (e.g. either
pkg.modor..mod). If the name is specified in relative terms, then the package argument must be set to the name of the package which is to act as the anchor for resolving the package name (e.g.import_module('..mod', 'pkg.subpkg')will importpkg.mod).
e.g.
my_module = importlib.import_module('os.path')
With Python older than 2.7/3.1, that's pretty much how you do it.
For newer versions, see importlib.import_module for Python 2 and Python 3.
Or using __import__ you can import a list of modules by doing this:
>>> moduleNames = ['sys', 'os', 're', 'unittest']
>>> moduleNames
['sys', 'os', 're', 'unittest']
>>> modules = map(__import__, moduleNames)
Ripped straight from Dive Into Python.