python一个简单的打包例子
最近写了一些工具,想到分享给同事时好麻烦,并且自己每次用也是需要打开pycharm这些工具,感觉很麻烦,因此想到打包,网上有些例子,照做后又摸索很久方成,索性记录一下,以备不时之需.
主要参考:https://blog.csdn.net/llsmingyi/article/details/78691287
首先写一个setup.py文件
# !/usr/bin/env python # -*- coding:utf-8 -*- from setuptools import setup, find_packages setup( name = "setupdemo", version = "0.3", packages = find_packages(), description = "egg test demo", long_description = "egg test demo", author = "luhouxiang", author_email = "luhouxiang@hotmail.com", license = "GPL", keywords = ("setupdemo", "egg"), platforms = "Independant", url = "http://blog.csdn.net/hong201/", entry_points = { 'console_scripts': [ 'setupdemo = setupdemo.hello:main' ] } )
然后再写要打包的文件夹setupdemo, 使得setupdemo与setup.py在同一目录层级上
在setupdemo中新建一文件hello.py, 代码如下:
# !/usr/bin/env python # -*- coding:utf-8 -*- def main(): print('hello') if __name__ == '__main__': main()
再新建一个空文件__init__.py
整体的目录结构:
setupdemo
|_________ setupdemo
|___________ hello.py
|___________ __init__.py
|_________ setup.py
以上为最简单的一个setup.py打包示例, 假设你的工程在e:/work/python/setupdemo, 则打开命令行到setupdemo目录:
cd /d e:/work/python/setupdemo
然后运行命令:
python setup.py install
看到Finished processing的字样,说明包已经打好
然后运行已经打好包的文件setupdemo, 不出意外,会显示hello
示例代码: https://github.com/luhouxiang/setupdemo