the5fire

关注Python、Django、Vim、Linux、Web开发、团队管理和互联网--Life is short, we need Python.


python程序使用setup打包安装

作者:the5fire | 标签:     | 发布:2012-08-13 11:53 p.m. | 阅读量: 17888, 17171
关于python程序打包的介绍就不多说了,大部分的python基础书上都有介绍。这里就直接演练。

只是一个简单的demo,一个demo项目中,有一个hello文件,文件中有一个函数hello,函数的作用是读取testdd.txt文件中的数据然后输出。

这个项目中还有其他的一些东西,以演示打包。

整个项目结构如下:

singledemo
demo
model
__init__.py
entity.py
static
test.txt
hello.py
MANIFEST.in
setup.py


hello.py中的代码:

def hello():
f = open('testdd.txt', 'r')
content = f.read()
f.close()
print content


setup.py中的代码如下:

#coding:utf-8
'''

'''
import os
import sys

from setuptools import setup, find_packages


setup(
name = "demo",
version = "0.0.1",
packages = find_packages(),

include_package_data = True,

entry_points = {
'console_scripts' : [
'demo = demo.hello:hello'
],
},
package_data = {
'demo':['*.txt']
},
author = "the5fire",
author_email = '嘻嘻嘻嘻嘻嘻嘻@email.com',
url = "http://www.the5fire.com",
description = 'a demo for setuptools',
)


还有一个文件需要注意,MANIFEST.in:

recursive-include demo *.txt


虽然只有一句话,但是是要通过它来包括你要打包的非py文件。

打包时候的命令有两个,
一个是打包成egg文件:python setup.py bdist_egg 。执行完成后,会在同目录下多了两个文件夹:demo.egg-info和dist,egg文件就在dist中,这个文件可以上传到pypi.python.com上,供大家下载。或者上传到某网盘,通过pip install --no--index find-links=[url]来下载。
另外一种是打包成压缩文件形式:python setup.py sdist 。执行结果同上,不过文件格式不同。

打包完成之后,当然要安装了,上一篇介绍了virtualenv,创建一个虚拟环境以供测试。然后执行python setup.py install 就会在你的虚拟环境的bin下创建一个demo的可执行文件,你在虚拟环境中运行:demo,输出结果。

很简单的东西,但是需要参考。
- from the5fire.com
----EOF-----

微信公众号:Python程序员杂谈


其他分类: