首页 > 编程语言 >python基础-模块和包

python基础-模块和包

时间:2022-12-09 12:13:07浏览次数:35  
标签:test2 模块 python py 基础 two func test import

1.什么是python的包

  包就是一个文件夹,里面放着一个个py文件或子包;

  在包中可以被调用的一个个py文件,我们叫做模块;

  

  如上,test就是一个包、two.py就是test下的一个模块,child是子包,结构和test包一样;

  包的身份证

    可以看到test下还有一个__init__.py命名的文件,是python包中必须存在的文件,是和普通文件夹的区分标志、类似包的身份证;

 

2.包的导入import

  import package 

  一般用于两种场景,拿到某包下__init__.py文件中中的功能或同级模块下的功能;

  

  如上结构,在three.py文件中可以通过import导入test/__init__.py中的方法;

# test/__init__.py

def test_init_func():
    print('test_init_func')
# test2/three.py

import test
test.test_init_func()  # test_init_func

  还可以在three.py中导入同级模块one.py中的功能;

# test2/one.py

def test2_one_func():
    print('test2_one_func')
# test2/three.py

import one
one.test2_one_func()  # test2_one_func

 

3.模块的导入from .. import ..

  通过某个包找到对应的模块;from package import module

  3.1 导入同级包中的某模块

    eg:在main.py中导入test/two.py模块

    

# test/two.py

def test_two_fun():
    print('test_two_func')
# main.py

# 方式一 仍然使用import
import test.two
test.two.test_two_fun()  # test_two_func
# main.py

# 方式二 使用from...import...

from test import two
two.test_two_fun()  # test_two_func
# main.py

# 也可以直接导入方法
from test.two import test_two_fun
test_two_fun()  # test_two_func

  此时要导入模块中的类时,与方法的导入类似,导入后实例化一下即可;

# test/two.py

class Two(object):

    @staticmethod
    def test_two_fun():
        print('test_two_func')
# main.py

from test import two
t = two.Two()
t.test_two_fun()  # test_two_func
# 也可以直接导入类
from test.two import Two

t = Two()
t.test_two_fun()  # test_two_func

# python的模块导入形式比较灵活,按需使用即可

  3.2 导入同级包中子包某模块

    eg: 在main.py中导入test/test2/one.py模块中的方法;

    

# test/test2/one.py

def test_test2_one_func():
    print('打印test_test2_one_func')
# main.py

from test.test2 import one
one.test_test2_one_func()  # 打印test_test2_one_func
# main.py

# 也可以直接导入函数

from test.test2.one import test_test2_one_func
test_test2_one_func()  # 打印test_test2_one_func

  可以优化下上面from test.test2.one import 的写法,在test/__init__.py中提前导入子包;

# test/__init__.py

from .test2.one import test_test2_one_func  # init文件中导入同级子包中方法,需使用 .子包名 的方式
# main.py

# 此时可以直接用import方式导入,书写上方便的多
import test
test.test_test2_one_func()  # 打印test_test2_one_func

  3.3 导入同级包中不同子包各自模块

    

    此时main.py中要同时导入两个one.py模块;

# test/test2/one.py

def test_test2_one_func():
    print('打印test_test2_one_func')
# test/test3/one.py


def test_test3_one_func():
    print('打印test_test3_one_func')
# main.py

from test.test2 import one as test2_one
from test.test3 import one as test3_one  # 有模块名重复时,为了区分,可以使用as重命名下

test2_one.test_test2_one_func()
test3_one.test_test3_one_func()
'''
打印test_test2_one_func
打印test_test3_one_func
'''

  同样也可以借助init文件,简写调用方式;

# test/__init__.py

from .test2.one import test_test2_one_func
from .test3.one import test_test3_one_func
# main.py

from test import test_test2_one_func, test_test3_one_func  # 同一模块导入多个方法时,可以合并到一行写
test_test2_one_func()
test_test3_one_func()
'''
打印test_test2_one_func
打印test_test3_one_func
'''

 

总结

  

标签:test2,模块,python,py,基础,two,func,test,import
From: https://www.cnblogs.com/white-list/p/16962450.html

相关文章

  • PYTHON 异常处理
    1.1异常处理有时可将程序错误(Error)称作程序异常(Exception)。出现错误程序终止。出现异常程序终止,也可以捕捉异常和撰写异常处理程序,处理完程序可以继续运行。1.1......
  • python中os.system(cmd)函数的返回值:python中的os.system(cmd)的返回值与linux命令返
    前言①在实际开发过程中,经常会遇到在Python代码中调用shell脚本,再获取脚本返回的返回值的情况: os.system(cmd) ②由于系统环境的问题, os.system(cmd) 函数执行命令后......
  • Python基础语法
    1.continue语句#!/usr/bin/python#-*-coding:UTF-8-*-n=100whilen>0:n-=1ifn%2==0:continueprint(n)#n-......
  • 基于Python+Django+Vue+MYSQL的社团管理系统
    OverridetheentrypointofanimageIntroducedinGitLabandGitLabRunner9.4.Readmoreaboutthe extendedconfigurationoptions.Beforeexplainingtheav......
  • 【推荐收藏】Python 常见报错以及解决方案
    OverridetheentrypointofanimageIntroducedinGitLabandGitLabRunner9.4.Readmoreaboutthe extendedconfigurationoptions.Beforeexplainingtheav......
  • OpenCL:图像处理基础note
    使用图像对象的理由虽然对于图像也可以把它的像素数据当做一般的缓存数据来处理,但是如果把它当做图像来处理有如下好处:在GPU中,图像数据是保存在特殊的全局内存中,即纹理......
  • [python] ThreadPoolExecutor线程池
    初识Python中已经有了threading模块,为什么还需要线程池呢,线程池又是什么东西呢?在介绍线程同步的信号量机制的时候,举得例子是爬虫的例子,需要控制同时爬取的线程数,例子中......
  • 机器学习的基础图表!学习记录笔记
    文章目录​​机器学习的基础图表!​​​​*一、机器学习概览​​​​1.什么是机器学习?​​​​2.机器学习和人工智能的关系​​​​3.机器学习的工作方式​​​​4.机器......
  • 用Python代码将XML转为JSON(或dict,字典)
    1.下面的Python代码将任意XML格式文件转化为JSON格式(字典)。除Python自带的模块外,不需要依赖其他任何第三方库。2.XML文件的读取使用Python自带的XML模块。3.关键代码如下......
  • git submodule .gitmodules 子模块
    初始化gitsubmoduleinit添加子项目gitsubmoduleadd-bxxxx{path}-b指定分支path可为空(不知为何,直接写在.gitmodules或者.git/config中编......