本文目录(以下内容收集自互联网):
1.编写要发布的模块
2.使用已发布的模块
1.编写要发布的模块:
1.mymodule目录结构体如下:
.
├── setup.py
├── suba
│ ├── aa.py
│ ├── bb.py
│ └── __init__.py
└── subb
├── cc.py
├── dd.py
└── __init__.py
2.编辑setup.py文件
py_modules需指明所需包含的py文件
from distutils.core import setup
setup(name="dongGe", version="1.0", description="dongGe's module", author="dongGe", py_modules=['suba.aa', 'suba.bb', 'subb.cc', 'subb.dd'])
3.构建模块
python setup.py build
构建后目录结构
.
├── build
│ └── lib.linux-i686-2.7
│ ├── suba
│ │ ├── aa.py
│ │ ├── bb.py
│ │ └── __init__.py
│ └── subb
│ ├── cc.py
│ ├── dd.py
│ └── __init__.py
├── setup.py
├── suba
│ ├── aa.py
│ ├── bb.py
│ └── __init__.py
└── subb
├── cc.py
├── dd.py
└── __init__.py
4.生成发布压缩包
python setup.py sdist
打包后,生成最终发布压缩包dongGe-1.0.tar.gz , 目录结构
.
├── build
│ └── lib.linux-i686-2.7
│ ├── suba
│ │ ├── aa.py
│ │ ├── bb.py
│ │ └── __init__.py
│ └── subb
│ ├── cc.py
│ ├── dd.py
│ └── __init__.py
├── dist
│ └── dongGe-1.0.tar.gz
├── MANIFEST
├── setup.py
├── suba
│ ├── aa.py
│ ├── bb.py
│ └── __init__.py
└── subb
├── cc.py
├── dd.py
└── __init__.py
2.使用已发布的模块:
1.安装的方式
- 找到模块的压缩包
- 解压
- 进入文件夹
- 执行命令
python setup.py install
注意:
- 如果在install的时候,执行目录安装,可以使用
python setup.py install --prefix=安装路径
2.模块的引入
在程序中,使用from import 即可完成对安装的模块使用
from 模块名 import 模块名或者*