python高级-包(15)

1、引入包

1.1 有2个模块功能有些联系python

 

receiveMsg.py和sendMsg.py都在msg文件夹里面。函数

 

1.二、使用import 文件.模块的方式导入ui

在桌面建立demo.py文件,并把receiveMsg.py和sendMsg.py使用import文件.模块的方式导入demo.py文件。url

receive.py模块spa

def receiveMessage(): print("正在接受消息...")

send.py模块设计

def sendMessage(): print("正在发送消息...")

demo.py模块code

import msg.send import msg.receive msg.send.sendMessage() msg.receive.receiveMessage()

运行结果为:blog

正在发送消息...
正在接受消息...

 

1.3使用from文件夹import模块的方式导入ip

demo.py模块rem

from msg import * send.sendMessage() receive.receiveMessage()

运行结果为:

Traceback (most recent call last): File "C:\Users\Se7eN_HOU\Desktop\deno.py", line 2, in <module> send.sendMessage() NameError: name 'send' is not defined

这个时候咱们使用msg下面的模块失败,提示咱们没有定义模块

 

1.四、在msg文件夹下建立 _ _init_ _.py 文件

 

1.五、在_ _init_ _.py文件中写入

__all__ = ["send","receive"]

 

1.6从新使用from文件夹import模块的方式导入

from msg import * send.sendMessage() receive.receiveMessage()

运行结果为:

正在发送消息...
正在接受消息...

总结:

  • 包将有联系的模块组织在一块儿,即放到同一个文件夹下,而且在这个文件夹建立一个名字为__init__.py 文件,那么这个文件夹就称之为包
  • 有效避免模块名称冲突问题,让应用组织结构更加清晰

 

2、_ _init_ _.py文件

  1. _ _init_ _.py控制着包导入行为
  2. _ _init_ _.py为空,意味着仅仅把这个包导入,不会导入包中的模块
  3. _ _init_ _.py文件中,定义一个_ _all_ _变量,它控制着from包名import*是导入的模块
  4. 能够在_ _init_ _.py文件中编写内容,当导入时,这些语句就会被执行

__init__.py模块

__all__ = ["send","receive"] print("你导入的msg包") def test(): print("这里是msg包里面的test")

demo.py模块

import msg msg.test()

运行结果为:

你导入的msg包
这里是msg包里面的test

 

3、嵌套的包

假定咱们的包的例子有以下的目录结构:

A/#包A
    __init__.py a1.py sub_B/#包B
        __init__.py b1.py b2.py sub_C/#包C
        __init__.py c1.py c2.py sub_D/#包D
        __init__.py d1.py d2.py

A是最顶层的包,sub_B等是它的子包,咱们能够这样导入子包:

import A.sub_B.b1

你也可以使用 from-import 实现不一样需求的导入

第一种方法是只导入顶层的子包,而后使用属性点操做符向下引用子包树:

from A import sub_B sub_b.b2

此外,咱们能够还引用更多的子包:

from A.sub_B import b1

事实上,你能够一直沿子包的树状结构导入

在咱们上边的目录结构中,咱们能够发现不少的 __init__.py 文件。这些是初始化模块,from-import 语句导入子包时须要用到它。 若是没有用到,他们能够是空文件。

 

4、包发布

1.myModule目录结构体以下:

./ setup.py __init__.py test.py sub_A/
        __init__.py a.py sub_B/
        __init__.py b.py

 

 

2.编辑setup.py文件

py_modules需指明所需包含的py文件

from distutils.core import setup setup(name = "Se7eN_HOU",version = "1.0",description = "Se7eN_HOU's module",author = "Se7eN_HOU",py_modules = ["sub_A.a","sub_B.b"])

 

3.构建模块

使用控制台在setup.py文件的同目录下执行python setup.py build

C:\Users\Se7eN_HOU\Desktop\myModule>python setup.py build running build running build_py copying sub_A\a.py -> build\lib\sub_A copying sub_B\b.py -> build\lib\sub_B C:\Users\Se7eN_HOU\Desktop\myModule>

 

构建后目录结构:

./ setup.py __init__.py test.py sub_A/
        __init__.py a.py sub_B/
        __init__.py b.py build/ lib/ sub_A/
                __init__.py a.py sub_B/
                __init__.py b.py

 

4.生成发布压缩包

同目录下执行python setup.py sdist

 

C:\Users\Se7eN_HOU\Desktop\myModule>python setup.py sdist running sdist running check warning: check: missing required meta-data: url warning: check: missing meta-data: if 'author' supplied, 'author_email' must be supplied too warning: sdist: manifest template 'MANIFEST.in' does not exist (using default file list) warning: sdist: standard file not found: should have one of README, README.txt, README.rst writing manifest file 'MANIFEST' creating Se7eN_HOU-1.0 creating Se7eN_HOU-1.0\sub_A creating Se7eN_HOU-1.0\sub_B making hard links in Se7eN_HOU-1.0... hard linking setup.py -> Se7eN_HOU-1.0 hard linking sub_A\__init__.py -> Se7eN_HOU-1.0\sub_A hard linking sub_A\a.py -> Se7eN_HOU-1.0\sub_A hard linking sub_B\__init__.py -> Se7eN_HOU-1.0\sub_B hard linking sub_B\b.py -> Se7eN_HOU-1.0\sub_B creating dist Creating tar archive removing 'Se7eN_HOU-1.0' (and everything under it) C:\Users\Se7eN_HOU\Desktop\myModule>

打包后,生成最终发布压缩包Se7eN_HOU-1.0tar.gz,目录结构

./ setup.py __init__.py test.py sub_A/
        __init__.py a.py sub_B/
        __init__.py b.py build/ lib/ sub_A/
                __init__.py a.py sub_B/
                __init__.py b.py MANIFEST dist/ Se7eN_HOU-1.0.tar.gz

 

5、包的安装、使用

一、安装的方式

  1. 找到模块的压缩包
  2. 解压
  3. 进入文件夹
  4. 执行命令python setup.py install

注意:

  • 若是在install的时候,执行目录安装,可使用python setup.py install –prefix=安装路径

二、模块的引入

在程序中,使用from import 便可完成对安装的模块使用

from 模块名 import 模块名或者*

 

6、循环导入

1. 什么是循环导⼊

A.py

from B import b print("这是A模块") def a(): print("hello a") b() a()

B.py

from A import a print("这是B模块") def b(): print("Hello b") a() b()

运⾏python a.py

Traceback (most recent call last): File "C:\Users\Se7eN_HOU\Desktop\A.py", line 1, in <module>
    from B import b File "C:\Users\Se7eN_HOU\Desktop\B.py", line 1, in <module>
    from A import a File "C:\Users\Se7eN_HOU\Desktop\A.py", line 1, in <module>
    from B import b ImportError: cannot import name 'b' from 'B' (C:\Users\Se7eN_HOU\Desktop\B.py)

像这样A里面引用了B,B里面又引用了A,这样就叫作循环引用

2. 怎样避免循环导⼊

  • 1. 程序设计上分层,下降耦合
  • 2. 导⼊语句放在后⾯须要导⼊时再导⼊,例如放在函数体内导⼊
相关文章
相关标签/搜索