案例:如何将source code制作成一个egg包,并且生成可以直接运行的命令
目录结构:
(build02) louis@jenkins:~/.virtualenv/pactest$ tree ci_test/
ci_test/
目录层级:
(build02) louis@jenkins:~/.virtualenv/pactest$ tree ci_test/
ci_test/
├── hello.py
├── __init__.py
└── utils
├── bye.py
└── __init__.py
1 directory, 4 files
1.hello.py
from .utils import saybye
def main():
print('hello')
saybye()
if __name__ == '__main__':
main()
2.ci_test/__init__.py
空
3.utils/__init__.py
from .bye import saybye
4. utils/bye.py:
def saybye():
print('bye')
5. setup.py 与ci_test/ 同级
from setuptools import setup, find_packages
setup(
name = "eggtest",
version = "0.1",
packages = find_packages(),
description = "egg test demo",
long_description = "egg test demo",
author = "lidehong",
author_email = "idehong@gmail.com",
license = "GPL",
keywords = ("test", "egg"),
platforms = "Independant",
url = "http://blog.youkuaiyun.com/hong201/",
entry_points = {
'console_scripts': [
'say = ci_test.hello:main',
]
}
)
6.执行命令 python setup.py install
则会在当前python解释器所在的目录下面生成,say 可执行文件
./say 运行:
hello
bye
以上是实验成功的一种打包方式。错误的打包方式不说了(本人蹲了一天的坑)。直接生成可执行的命令。在运行的时候以常见的shell命令的样子调用,比起python script.py调用的方式更加优雅。重要的是在一个比较大的项目里面可能要提供很多类似的console 命令行形式,这时候这种打包安装的方式就更加有效,也容易管理维护。