Reputation: 41
I keep noticing blocks of code starting with import string, import re or import sys.
I know that you must import a module before you can use it. Is the import based on the object?
Upvotes: 0
Views: 2221
Reputation: 4522
Python has modules that give the code more functionalities. import re
gives access to the re
module which gives RegEx support. If you type help()
at the Python interpreter and then type modules, it will return a list of all the modules.
Upvotes: 1
Reputation: 137342
import sys
Will have the effect of adding a sys
variable to your local namespace (usually at the module level). Because sys
is a module with it's own attributes, then you can say sys.something()
, and Python will be able to reference the local name sys
, and then the attribute something
, and then call it ()
.
from os.path import join
This will look inside the os package, inside the path subpackage, and create a local reference to the join
function in your namespace. That way, you can simply refer to it as:
join('a', 'b')
Suggest you look at a couple of tutorials that cover importing.
Upvotes: 0
Reputation: 798804
The import is based on what module you want to access the names of.
Upvotes: 2