Python Import
A module
is a file containing Python definitions and statements. Python modules have a filename
and end with the extension .py.
Definitions
inside a module can be imported to another module or the interactive interpreter
in Python. We use the import keyword to do this.
Python
provides two ways to import modules.
>>> import math
>>> print math
<module 'math' (built-in)>
>>> print math.pi
3.14159265359
If you
import math, you get a module object named math. The module object contains constants
like pi and functions like sin and exp.
But if
you try to access pi directly, you get an error.
>>> print pi
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'pi' is not defined
As an
alternative, you can import an object from a module like this:
>>> from math import pi
Now you
can access pi directly, without dot notation.
>>> print pi
3.14159265359
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.