zoukankan      html  css  js  c++  java
  • Python import Statement

    Python import statement enables the user to import particular modules in the corresponding program.

    It resembles the #include header_file in C/C++.

    As soon as the interpreter encounters the import statement in a particular code, it searches for the same in the local scope and imports the module, if present in the search path.

     
     
     
     
     
    2.7M
     
    How to Upgrade Ubuntu 19.04 to Ubuntu 19.10
     
     
    Next
    Stay
     

    It searches for a particular module in its built-in modules section at first. If it’s not found, it searches those modules in its current directory.

    A module is loaded only once in a particular program, without being affected by the number of times the module is imported.

    Syntax:

    import module_name

    Example:

    import collections

    1. Importing class/functions from a module

    We can import classes/functions from a module using the syntax:

    from {module} import {class/function}

    Example:

    from collections import OrderedDict
    from os import path
    from math import pi
    print(pi)

    Output:

    3.141592653589793

    2. The import * Statement

    All the methods and constants of a particular module can be imported using import * operator.

    from math import *
    print(pi)
    print(floor(3.15))

    Output:

    3.141592653589793
    3

    3. Python’s import as Statement

    The import as statement helps the user provide an alias name to the original module name.

    # python import as
    import math as M
     
    print(M.pi)
    print(M.floor(3.18))

    Output:

    3.141592653589793
    3

    4. Importing user-defined modules

    We can import the functions of one program into another using its name.

    Initially, we need to create a python code.

    test.py

    def sub(a, b):
        return int(a) - int(b)
     
    def lower_case(str1):
        return str(str1).lower()

    Then create another python script, wherein we need to import the above create test.py script.

    test2.py

    import test
     
    print(test.sub(5,4))
    print(test.lower_case('SafA'))

    Output:

    1
    safa

    5. Importing from another directory

    The importlib library is used to import a script from another directory.

    Initially, we need to create a python script and define functions in it.

    test1.py

    def sub(a, b):
        return int(a) - int(b)
     
    def lower_case(str1):
        return str(str1).lower()

    Then, we will create another python script and save it into another directory and then import the functionalities from test1.py (which resides into another directory).

    design.py

    import importlib, importlib.util
     
    def module_directory(name_module, path):
        P = importlib.util.spec_from_file_location(name_module, path)
        import_module = importlib.util.module_from_spec(P)
        P.loader.exec_module(import_module)
        return import_module
     
    result = module_directory("result", "../inspect_module/test1.py")
     
    print(result.sub(3,2))
    print(result.lower_case('SaFa'))

    Output:

    1
    safa

    Another alternative way is to add the module directory to the sys.path list.


    6. Importing class from another file

    tests.py

    class Employee:
        designation = ""
     
        def __init__(self, result):
            self.designation = result
     
        def show_designation(self):
            print(self.designation)
     
     
    class Details(Employee):
        id = 0
     
        def __init__(self, ID, name):
            Employee.__init__(self, name)
            self.id = name
     
        def get_Id(self):
            return self.id

    design.py

    import importlib, importlib.util
     
    def module_directory(name_module, path):
        P = importlib.util.spec_from_file_location(name_module, path)
        import_module = importlib.util.module_from_spec(P)
        P.loader.exec_module(import_module)
        return import_module
     
    result = module_directory("result", "../Hello/tests.py")
     
    a = result.Employee('Project Manager')
    a.show_designation()
     
    x = result.Details(4001,'Safa')
    x.show_designation()
    print(x.get_Id())

    Output:

    Project Manager
    Safa
    Safa

    Conclusion

    Thus, in this article, we have understood the functionalities offered by the import statement.

  • 相关阅读:
    C# 建立快捷方式
    ae中gp执行 Error HRESULT E_FAIL has been returned from a call to a COM component错误
    AE编辑点要素编辑
    噱头还是革命 云计算“泡沫”五年后改变世界? 狼人:
    分析:英特尔收购McAfee的三大意义 狼人:
    云安全:防护“工具”还是攻击“利器” 狼人:
    热点:安全问题是否能将DNS推入云服务 狼人:
    迈克菲收购tenCube 打造新一代移动安全平台 狼人:
    戴尔推免费浏览器安全工具 可隔离恶意软件 狼人:
    黑帽大会:HTTPS和SSL协议存在安全漏洞 狼人:
  • 原文地址:https://www.cnblogs.com/weifeng1463/p/15779583.html
Copyright © 2011-2022 走看看