Python 模块的构建与发布

来源:互联网 发布:php session原理 编辑:程序博客网 时间:2024/04/28 07:02


所谓 Python 模块,就是带有Python代码的文本文件

发布Python模块前,首先要对模块进行测试, 打开IDLE编辑器,加在模块,按F5运行,测试模块是否有效


发布步骤:

1、发布准备: 首先创建一个文件夹, 将Python模块放入该文件加中,并在该文件夹中创建一个setup.py文件

如: nester---

                    --- nester.py    setup.py

from distutils.core import setupsetup(        name        = "nester",        version     ="1.0.0",        py_modules  =['nester'],        author      ='Fanchang Meng',        author_email='mfch1207@163.com',        url         ='http://www.baidu.com',        description ='A simple printer of nested lists',    )

2、构建发布:

1) 创建发布: 将cmd 命令重口进入到nester文件夹路径下, 输入 命令 python setup.py sdist ,如下


生成文件:dist文件夹内存放  构建后的发布包: nester-1.0.0.zip


2) 将发布安装到Python本地副本, 命令: python setup.py install


安装后,本地生成 build ---  lib --- nester.py    文件内容为  原来的Python源码模块



3、发布包的导入与使用: 2种方式:

1、import nesternester.print_lol()2、from nester import print_lol()
第一种方式,函数前必须带有命名空间限定




0 0
原创粉丝点击