Importing Python Module in different method

Importing the python module can be done in different ways

  1. Import installed modules(available in site-packages)
  2. Import specific python file like “import file.py”.
  3. Import the folder instead of a specific python file.
  4. Based on run time values import python file.
  5. Import specific functions from python file.

Example1: Import installed modules (available in site-packages)

Import entire module: Module contains the function definition and statements. The module may be stand-alone python files that are installable python packages. Installed python modules present in site package, which is available in c:\ drive

# import the module
import math
print(dir(math)
# dir used to discover inside methods and members of the module
print(math.pi)

 

output:

[‘__doc__’, ‘__loader__’, ‘__name__’, ‘__package__’, ‘__spec__’, ‘acos’, ‘acosh’, ‘asin’, ‘asinh’, ‘atan’, ‘atan2’, ‘atanh’, ‘ceil’, ‘copysign’, ‘cos’, ‘cosh’, ‘degrees’, ‘e’, ‘erf’, ‘erfc’, ‘exp’, ‘expm1’, ‘fabs’, ‘factorial’, ‘floor’, ‘fmod’, ‘frexp’, ‘fsum’, ‘gamma’, ‘gcd’, ‘hypot’, ‘inf’, ‘isclose’, ‘isfinite’, ‘isinf’, ‘isnan’, ‘ldexp’, ‘lgamma’, ‘log’, ‘log10’, ‘log1p’, ‘log2’, ‘modf’, ‘nan’, ‘pi’, ‘pow’, ‘radians’, ‘remainder’, ‘sin’, ‘sinh’, ‘sqrt’, ‘tan’, ‘tanh’, ‘tau’, ‘trunc’]

3.141592653589793

Import entire module using from keyword: please look below example

# import the module
from math import *
print(dir(sqrt))
# dir will explore the sqrt
print(sqrt.__doc__)
print(pi)

output:

[‘__call__’, ‘__class__’, ‘__delattr__’, ‘__dir__’, ‘__doc__’, ‘__eq__’, ‘__format__’, ‘__ge__’, ‘__getattribute__’, ‘__gt__’, ‘__hash__’, ‘__init__’, ‘__init_subclass__’, ‘__le__’, ‘__lt__’, ‘__module__’, ‘__name__’, ‘__ne__’, ‘__new__’, ‘__qualname__’, ‘__reduce__’, ‘__reduce_ex__’, ‘__repr__’, ‘__self__’, ‘__setattr__’, ‘__sizeof__’, ‘__str__’, ‘__subclasshook__’, ‘__text_signature__’] Return the square root of x. 3.141592653589793

Import specific member/methods from the module: previous example discover the methods and members of the math module. Here we are importing specific methods from the modules using from…import… keywords. Here no scope except pi .

# import pi from the math module
from math import pi
print(dir(pi))
print(pi)
print(pi.__doc__)# print the docstri

output:

[‘__abs__’, ‘__add__’, ‘__bool__’, ‘__class__’, ‘__delattr__’, ‘__dir__’, ‘__divmod__’, ‘__doc__’, ‘__eq__’, ‘__float__’, ‘__floordiv__’, ‘__format__’, ‘__ge__’, ‘__getattribute__’, ‘__getformat__’, ‘__getnewargs__’, ‘__gt__’, ‘__hash__’, ‘__init__’, ‘__init_subclass__’, ‘__int__’, ‘__le__’, ‘__lt__’, ‘__mod__’, ‘__mul__’, ‘__ne__’, ‘__neg__’, ‘__new__’, ‘__pos__’, ‘__pow__’, ‘__radd__’, ‘__rdivmod__’, ‘__reduce__’, ‘__reduce_ex__’, ‘__repr__’, ‘__rfloordiv__’, ‘__rmod__’, ‘__rmul__’, ‘__round__’, ‘__rpow__’, ‘__rsub__’, ‘__rtruediv__’, ‘__set_format__’, ‘__setattr__’, ‘__sizeof__’, ‘__str__’, ‘__sub__’, ‘__subclasshook__’, ‘__truediv__’, ‘__trunc__’, ‘as_integer_ratio’, ‘conjugate’, ‘fromhex’, ‘hex’, ‘imag’, ‘is_integer’, ‘real’] 3.141592653589793 Convert a string or number to a floating point number, if possible.

2 thoughts on “Importing Python Module in different method

  • February 16, 2020 at 10:56 am
    Permalink

    I simply couldn’t leave your web site before suggesting that I really loved the usual information an individual provide in your guests? Is going to be back continuously in order to check for new posts, thanks!

    Reply
    • March 28, 2020 at 5:19 pm
      Permalink

      Thanks for your support.! I’ll update more info in the future.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *