zoukankan      html  css  js  c++  java
  • guxh的python笔记十:包和模块

    1,包和模块

    包package:本质就是一个文件夹/目录,必须带一个__init.__.py的文件

    模块module:.py结尾的python文件

    2,导入方法

    import pandas, collections  # 导入多个

    import pandas as pd # 起别名

    from module import fun  # 导入优化,可以直接使用fun

    import只能导入package或者module,不能直接import到对象,例如import module.fun

    import到package后,如果想能够访问module及module中的方法,需要package中的__init__函数支持,否则无法访问 

    可以通过__all__控制导出的类/方法列表 

    3,包pakage中__init__导入方法 - 单个package

    guxh/
    |-- init.py
    |-- main.py # 有main_fun()
     

    3.1,__init__函数没有import目标module

    """__init__函数中为空"""

    无法通过 import package自动获得package下的module,需手工指定module。

    使用guxh包时支持如下方式:

    from guxh import main
    guxh.main.main_fun() 
    
    import guxh.main
    guxh.main.main_fun() 

    典型样例:tkinter包下的__init__没有导入filedialog,所以使用时需要import tkinter.filedialog

    3.2,__init__函数import了目标module:

    from . import main  # 相对路径导入
    from guxh import main   # 绝对路径导入

    使用时必须带上main模块,通过main模块访问fun方法

    使用guxh包时支持如下方式:

    import guxh
    guxh.main.main_fun()
    
    from guxh import main_fun
    main.main_fun() 
    import guxh.main_fun
    guxh.main.main_fun()
    

     

    3.3,__init__函数import了目标module中的方法

    from .main import *   # 相对路径导入,导入所有方法
    from .main import main_fun  # 相对路径导入,指定导入main_fun方法
    from guxh.main import *    # 绝对路径导入,导入所有方法
    from guxh.main import main_fun    # 绝对路径导入,指定导入main_fun方法
    

    这样可以让package像module,不用带上main模块直接使用main_fun,同时也支持带上main模块访问main_fun。

    使用guxh包时支持如下方式:

    import guxh
    guxh.main_fun()   # guxh能直接访问到main_fun()
    guxh.main.main_fun()   # 带上main也行
    
    from guxh import main
    main.main_fun() 
    import guxh.main
    guxh.main_fun()   # guxh能直接访问到main_fun()
    guxh.main.main_fun()  # 带上main也行
    

    3.4,错误的导入方法

    只能import到package或者module,不能import到函数或类

    import guxh.main.main_fun

     另外__init__中导入其他模块或者模块中的方法,并且供第三方调用使用,应该:

    from . import main_fun
    from .main import main_fun
    

    但是如果是init自己导入执行,则应该:

    import main_fun
    from main import main_fun
    

    4,包pakage中__init__导入方法 - 2个package

    guxh/
    |-- __init__.py
    |-- main.py # 有main_fun()
    |
    |-- core/
    |   |-- __init__.py
    |   |-- sub.py # 有sub_fun()

    要想访问sub.py,完整的访问路径应该是guxh.core.sub.sub_fun(),如果想省略中间的package或者module,方法如下:

    4.1,guxh.core.sub.sub_fun()  

    两个package的init都import到module,访问时core(package)和sub(module)都不能省

    guxh/__init__.py:

    from guxh import core

    guxh/core/__init__.py:

    from . import sub
    

      

    4.2,guxh.sub.sub_fun()

    父package的init import *,可以省略core(package)

    guxh/__init__.py:

    from guxh.core import *

    guxh/core/__init__.py:

    from . import sub
    

    4.3,guxh.core.sub_fun()

    子package的init import *,可以省略sub(module)

    guxh/__init__.py:

    from guxh import core

    guxh/core/__init__.py:

    from .sub import *
    

      

    4.4,guxh.sub_fun() 

    父子package的init都import *,可同时省略core(package)和sub(module)

    guxh/__init__.py:

    from guxh.core import *

    guxh/core/__init__.py:

    from .sub import * 

    4.5,pandas实例

    pandas与本例类似,只不过子package(core)不是用的init而用是通过api中转,core中的init是个空文件

    pandas/
    |-- __init__.py
    |
    |-- core/
    |    |-- __init__.py
    |    |-- api.py
    |    |-- frame.py # 含有DataFrame()类

    pandas/__init__.py:

    from pandas.core.api import *
    

    pandas/core/api.py:

    from pandas.core.frame import DataFrame

    调用时支持:

    df = pandas.DataFrame()
    df = pandas.core.frame.DataFrame()
    

      

    5,动态导入

    方法一:

    lib = __import__('pandas.core.frame')   
    df = lib.DataFrame()

    方法二:

    import importlib
    lib = importlib.import_module('pandas.core.frame')
    df = lib.DataFrame()
    

    6,惰性导入

    只需要在实际需要时才加载组件,可以用惰性导入:

    def A():
        from .a import A
        return A()
    
  • 相关阅读:
    正则表达式
    面向对象(访问控制、继承、重写)
    你凌晨1点半会想什么
    10、android学习资源整理
    9、android开发之java.lang.verifyError(转载)
    8、android代码优化技术记录
    6、android 普通日志输出到SD卡
    刷机总结(阿里云os-->android4.2.2)注明:本文不是教程
    忘记树莓派密码怎么办?
    7、android的button如何平铺一张图片?
  • 原文地址:https://www.cnblogs.com/guxh/p/10240777.html
Copyright © 2011-2022 走看看