1、背景

查看flower的源码,首先看到flower的主程序如下:

#!/usr/local/sinasrv2/bin/python2.7
# EASY-INSTALL-ENTRY-SCRIPT: 'flower==0.7.3','console_scripts','flower'
__requires__ = 'flower==0.7.3'
import sys
from pkg_resources import load_entry_point if __name__ == '__main__':
sys.exit(
load_entry_point('flower==0.7.3', 'console_scripts', 'flower')()

load_entry_point,它的信息来源是entry_points.txt

entry_points.txt来源呢?

setup.py里面有entry_points 信息,会根据这些信息生成egg info目录,里面有entry_points.txt文件, 里面的内容就是setup.py里的entry_points信息

setup(
name='flower',
version=get_package_version(),
description='Celery Flower',
long_description=open('README.rst').read(),
author='Mher Movsisyan',
author_email='mher.movsisyan@gmail.com',
url='https://github.com/mher/flower',
license='BSD',
classifiers=classifiers,
packages=find_packages(exclude=['tests', 'tests.*']),
install_requires=install_requires,
test_suite="tests",
tests_require=get_requirements('test.txt'),
package_data={'flower': ['templates/*', 'static/**/*', 'static/*.*']},
entry_points={
'console_scripts': [
'flower = flower.__main__:main',
],
'celery.commands': [
'flower = flower.command:FlowerCommand',
],
},
)

  

然后将封装一个python脚本

说明flower脚本实际调用的是:

两种启动方式

[celery.commands]
flower = flower.command:FlowerCommand [console_scripts]
flower = flower.__main__:main

2、如何制作一个egg包以及对应的setup.py如何写呢?

2.1制作一个空的egg包目录以及setup.py文件

首先建立工程目录egg-demo,初始化一个setup.py文件:

[root@typhoeus79 ice_test_m 20141022]# tree -l
.
`-- egg-demo
`-- setup.py 1 directory, 1 file
[root@typhoeus79 ice_test_m 20141022]# more ./egg-demo/setup.py
#!/usr/bin/env python
#-*- coding:utf-8 -*- from setuptools import setup setup()

写到这里,一个空的egg配置文件就写好了。

setup的帮助文档:

Help on function setup in module distutils.core:

setup(**attrs)
The gateway to the Distutils: do everything your setup script needs
to do, in a highly flexible and user-driven way. Briefly: create a
Distribution instance; find and parse config files; parse the command
line; run each Distutils command found there, customized by the options
supplied to 'setup()' (as keyword arguments), in config files, and on
the command line. The Distribution instance might be an instance of a class supplied via
the 'distclass' keyword argument to 'setup'; if no such class is
supplied, then the Distribution class (in dist.py) is instantiated.
All other arguments to 'setup' (except for 'cmdclass') are used to set
attributes of the Distribution instance. The 'cmdclass' argument, if supplied, is a dictionary mapping command
names to command classes. Each command encountered on the command line
will be turned into a command class, which is in turn instantiated; any
class found in 'cmdclass' is used in place of the default, which is
(for command 'foo_bar') class 'foo_bar' in module
'distutils.command.foo_bar'. The command class must provide a
'user_options' attribute which is a list of option specifiers for
'distutils.fancy_getopt'. Any command-line options between the current
and the next command are used to set attributes of the current command
object. When the entire command-line has been successfully parsed, calls the
'run()' method on each command object in turn. This method will be
driven entirely by the Distribution object (which each command object
has a reference to, thanks to its constructor), and the
command-specific options that became attributes of each command
object.

setuptools的帮助文档:

Help on package setuptools:

NAME
setuptools - Extensions to the 'distutils' for large or complex distributions FILE
/usr/local/sinasrv2/lib/python2.7/site-packages/distribute-0.6.28-py2.7.egg/setuptools/__init__.py PACKAGE CONTENTS
archive_util
command (package)
depends
dist
extension
package_index
sandbox
script template
script template (dev)
tests (package)

  

2.2执行命令生成egg包

python setup.py bdist_egg

setup.py可以支持很多命令:

[root@typhoeus79 ice_test_m egg-demo]# python setup.py --help-commands
Standard commands:
build build everything needed to install
build_py "build" pure Python modules (copy to build directory)
build_ext build C/C++ extensions (compile/link to build directory)
build_clib build C/C++ libraries used by Python extensions
build_scripts "build" scripts (copy and fixup #! line)
clean clean up temporary files from 'build' command
install install everything from build directory
install_lib install all Python modules (extensions and pure Python)
install_headers install C/C++ header files
install_scripts install scripts (Python or otherwise)
install_data install data files
sdist create a source distribution (tarball, zip file, etc.)
register register the distribution with the Python package index
bdist create a built (binary) distribution
bdist_dumb create a "dumb" built distribution
bdist_rpm create an RPM distribution
bdist_wininst create an executable installer for MS Windows
upload upload binary package to PyPI Extra commands:
saveopts save supplied options to setup.cfg or other config file
compile_catalog compile message catalogs to binary MO files
develop install package in 'development mode'
easy_install Find/get/install Python packages
init_catalog create a new catalog based on a POT file
test run unit tests after in-place build
update_catalog update message catalogs from a POT file
setopt set an option in setup.cfg or another config file
install_egg_info Install an .egg-info directory for the package
rotate delete older distributions, keeping N newest files
egg_info create a distribution's .egg-info directory
alias define a shortcut to invoke one or more commands
extract_messages extract localizable strings from the project code
bdist_egg create an "egg" distribution

  

输出日志为:

[root@typhoeus79 ice_test_m egg-demo]# python setup.py bdist_egg
running bdist_egg
running egg_info
creating UNKNOWN.egg-info
writing UNKNOWN.egg-info/PKG-INFO
writing top-level names to UNKNOWN.egg-info/top_level.txt
writing dependency_links to UNKNOWN.egg-info/dependency_links.txt
writing manifest file 'UNKNOWN.egg-info/SOURCES.txt'
reading manifest file 'UNKNOWN.egg-info/SOURCES.txt'
writing manifest file 'UNKNOWN.egg-info/SOURCES.txt'
installing library code to build/bdist.linux-x86_64/egg
running install_lib
warning: install_lib: 'build/lib' does not exist -- no Python modules to install
creating build
creating build/bdist.linux-x86_64
creating build/bdist.linux-x86_64/egg
creating build/bdist.linux-x86_64/egg/EGG-INFO
copying UNKNOWN.egg-info/PKG-INFO -> build/bdist.linux-x86_64/egg/EGG-INFO
copying UNKNOWN.egg-info/SOURCES.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying UNKNOWN.egg-info/dependency_links.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying UNKNOWN.egg-info/top_level.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
zip_safe flag not set; analyzing archive contents...
creating dist
creating 'dist/UNKNOWN-0.0.0-py2.6.egg' and adding 'build/bdist.linux-x86_64/egg' to it
removing 'build/bdist.linux-x86_64/egg' (and everything under it)

再次查看egg-demo目录:

[root@typhoeus79 ice_test_m egg-demo]# tree
.
|-- UNKNOWN.egg-info
| |-- PKG-INFO
| |-- SOURCES.txt
| |-- dependency_links.txt
| `-- top_level.txt
|-- build
| `-- bdist.linux-x86_64
|-- dist
| `-- UNKNOWN-0.0.0-py2.6.egg
`-- setup.py

在dist文件夹下,有一个egg文件:UNKNOWN-0.0.0-py2.6.egg,这说明产蛋成功!

dist/
`-- UNKNOWN-0.0.0-py2.6.egg

  

egg文件是什么格式的呢?

[root@typhoeus79 ice_test_m dist]# file UNKNOWN-0.0.0-py2.6.egg
UNKNOWN-0.0.0-py2.6.egg: Zip archive data, at least v2.0 to extract

就是一个zip压缩包!看看其内部的结构

[root@typhoeus79 ice_test_m dist]# unzip UNKNOWN-0.0.0-py2.6.egg
Archive: UNKNOWN-0.0.0-py2.6.egg
inflating: EGG-INFO/zip-safe
inflating: EGG-INFO/top_level.txt
inflating: EGG-INFO/dependency_links.txt
inflating: EGG-INFO/PKG-INFO
inflating: EGG-INFO/SOURCES.txt
[root@typhoeus79 ice_test_m dist]# tree ./EGG-INFO/
./EGG-INFO/
|-- PKG-INFO
|-- SOURCES.txt
|-- dependency_links.txt
|-- top_level.txt
`-- zip-safe

只有一个EGG-INFO文件夹,内含五个egg信息文件。

这个egg名称未知,版本0.0.0。这是因为我们在setup里什么也没有设置。

显然,这个egg什么也不能做。

2.3 在egg包增加新功能-制作demo package

在setup.py中,setup函数接收一系列属性作为配置参数。

  • name name是egg包的名称,也是寻找要打包的文件夹的名称,默认是UNKNOWN。
  • version 版本号,默认0.0.0
  • packages 这里要用到setuptools的另一个函数find_packages,顾名思义,find_packages用来将指定目录下的文件打包。
  • zip_safe 默认是False,这样在每次生成egg包时都会检查项目文件的内容,确保无误。

还有一些描述性的属性,如description,long_description,author,author_email,license,keywords,platform,url等。

[root@typhoeus79 ice_test_m egg-demo]# more setup.py
#!/usr/bin/env python
#-*- coding:utf-8 -*- from setuptools import setup, find_packages setup(
name = "demo",
version="0.1.0",
packages = find_packages(),
zip_safe = False, description = "egg test demo.",
long_description = "egg test demo, haha.",
author = "amoblin",
author_email = "amoblin@ossxp.com", license = "GPL",
keywords = ("test", "egg"),
platforms = "Independant",
url = "",
)

在egg-demo目录下建立和上述name名称相同的目录demo,demo目录下写__init__.py文件:

 

[root@typhoeus79 ice_test_m egg-demo]# tree ./demo/
./demo/
`-- __init__.py 0 directories, 1 file
[root@typhoeus79 ice_test_m egg-demo]# more ./demo/__init__.py
#!/usr/bin/env python
#-*- coding:utf-8 -*- def test():
print "Hello, I'm amoblin." if __name__ == '__main__':
test()

  

再次生成egg包以后查看egg包信息:

[root@typhoeus79 ice_test_m egg-demo]# ll
总计 20
drwxr-xr-x 4 root root 4096 10-22 14:34 build
drwxr-xr-x 2 root root 4096 10-22 14:33 demo
drwxr-xr-x 2 root root 4096 10-22 14:34 demo.egg-info
drwxr-xr-x 2 root root 4096 10-22 14:34 dist
-rw-r--r-- 1 root root 496 10-22 14:31 setup.py

 

新的egg包里面的信息如下:

[root@typhoeus79 ice_test_m dist]# unzip demo-0.1.0-py2.6.egg
Archive: demo-0.1.0-py2.6.egg
inflating: EGG-INFO/not-zip-safe
inflating: EGG-INFO/top_level.txt
inflating: EGG-INFO/dependency_links.txt
inflating: EGG-INFO/PKG-INFO
inflating: EGG-INFO/SOURCES.txt
inflating: demo/__init__.pyc
inflating: demo/__init__.py

  

现在可以安装了体验一下!!

[root@typhoeus79 ice_test_m egg-demo]# python setup.py install
running install
running bdist_egg
running egg_info
writing demo.egg-info/PKG-INFO
writing top-level names to demo.egg-info/top_level.txt
writing dependency_links to demo.egg-info/dependency_links.txt
reading manifest file 'demo.egg-info/SOURCES.txt'
writing manifest file 'demo.egg-info/SOURCES.txt'
installing library code to build/bdist.linux-x86_64/egg
running install_lib
running build_py
creating build/bdist.linux-x86_64/egg
creating build/bdist.linux-x86_64/egg/demo
copying build/lib/demo/__init__.py -> build/bdist.linux-x86_64/egg/demo
byte-compiling build/bdist.linux-x86_64/egg/demo/__init__.py to __init__.pyc
creating build/bdist.linux-x86_64/egg/EGG-INFO
copying demo.egg-info/PKG-INFO -> build/bdist.linux-x86_64/egg/EGG-INFO
copying demo.egg-info/SOURCES.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying demo.egg-info/dependency_links.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying demo.egg-info/not-zip-safe -> build/bdist.linux-x86_64/egg/EGG-INFO
copying demo.egg-info/top_level.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
creating 'dist/demo-0.1.0-py2.6.egg' and adding 'build/bdist.linux-x86_64/egg' to it
removing 'build/bdist.linux-x86_64/egg' (and everything under it)
Processing demo-0.1.0-py2.6.egg
creating /usr/lib/python2.6/site-packages/demo-0.1.0-py2.6.egg
Extracting demo-0.1.0-py2.6.egg to /usr/lib/python2.6/site-packages
Adding demo 0.1.0 to easy-install.pth file Installed /usr/lib/python2.6/site-packages/demo-0.1.0-py2.6.egg
Processing dependencies for demo==0.1.0
Finished processing dependencies for demo==0.1.0

  

安装完毕!接下来我们就可以直接通过import来使用啦!

>>> from demo import test
>>> help(test) >>> test()
Hello, I'm amoblin.

  

源程序都放在src目录下,所以接下来将demo文件夹移动到src里。但这样也要修改setup.py文件,修改find_packages函数中参数为’src’,同时增加package_dir参数:

packages=find_packages('src'),
package_dir = {'':'src'}

这样告诉setuptools在src目录下找包,而不是原来默认的工程根目录。

2.4如何删除egg包

找到package放的位置

>>> print sys.path[1]
/usr/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg

存在这个egg包  

[root@typhoeus79 ice_test_m site-packages]# ll /usr/lib/python2.6/site-packages |grep demo
drwxr-xr-x 4 root root 4096 10-22 14:36 demo-0.1.0-py2.6.egg
[root@typhoeus79 ice_test_m site-packages]# cat easy-install.pth|grep demo
./demo-0.1.0-py2.6.egg

  

卸载egg文件很简单,首先将包含此egg的行从easy-install.pth中删除,然后删除egg文件夹即可。

3、参考资料

1、http://www.cnblogs.com/itech/archive/2011/02/13/1953268.html

2、http://zhiwei.li/text/2011/06/load_entry_point%E5%92%8Csetup-egg/

3、http://django-china.cn/topic/90/

4、http://blog.habnab.it/blog/2013/07/21/python-packages-and-you/

Python的egg包的更多相关文章

  1. python的egg包的安装和制作]

    Defining Python Source Code Encodings Python egg 的安装 egg文件制作与安装 2011-06-10 14:22:50|  分类: python |   ...

  2. Python中的包ImportError

    前言 Python中的包给我提供了很好的代码组织,相似的功能模块放在同一个包内,不仅代码结构清晰,而且调用起来也比较方便(可以用*导入) 但是,我们在刚开始使用Python包的时候总是会遇到导入错误& ...

  3. Python黑帽编程1.3 Python运行时与包管理工具

    Python黑帽编程1.3  Python运行时与包管理工具 0.1  本系列教程说明 本系列教程,采用的大纲母本为<Understanding Network Hacks Attack and ...

  4. python中引入包的时候报错AttributeError: module 'sys' has no attribute 'setdefaultencoding'解决方法?

    python中引入包的时候报错:import unittestimport smtplibimport timeimport osimport sysimp.reload(sys)sys.setdef ...

  5. Python之扩展包安装

    读者朋友,在比较新的版本(Python 2 >=2.7.9 or Python 3 >=3.4)中,pip或者easy_install 扩展包命令已经默认安装(可查看   你的安装目录\p ...

  6. [resource]23个python的机器学习包

    23个python的机器学习包,从常见的scikit-learn, pylearn2,经典的matlab替代orange, 到最新最酷的Theano(深度学习)和torch 7 (well,其实lua ...

  7. Python学习 之 包和模块

    1.rpm -ql python #查看python在计算机中安装了哪些文件 2.模块是一个可以导入的Python脚本文件 包是一堆按目录组织的模块和子包,目录下的__init__.py文件存放了包的 ...

  8. 使用pip install 或者easy_install安装Python的各种包出现cc failed with exit status 1

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  9. 繁简转换OpenCC,autogb 和 autob5,iconv,python的jianfan包

    OpenCC OpenCC 是跨平台.多语言的开放中文转换库,除了基本的简繁转换功能外,用户还可以选择对不同用词习惯和异体字的处理方式. OpenCC 还提供方便的网页转换界面. OpenOffice ...

随机推荐

  1. QQ--基于TCP/UDP协议的通讯原理

    QQ是一个基于TCP/UDP协议的通讯软件  发送消息的时候是UDP打洞,登陆的时候使用HTTP~因为登陆服务器其实就是一个HTTP服 务器,只不过不是常用的那些,那个服务器是腾讯自行开发的!   一 ...

  2. SQL 2008 外网访问说明

    1.  安装SQL2008 . 安装SQL2008之前,必须预先安装.NET Framework 3.5,和Windows Installer 4.5 Redistributable. 可能产生错误: ...

  3. 学习总结---SNAT和DNAT

    1.SNAT是结合源ip+源端口号变化的NAT功能. DNAT是将目的ip直接转换成私有的目的ip.(是否转换目的端口号?) 2.SNAT的应用场景:公司内部访问互联网时,使用公共的公网ip.从内到外 ...

  4. d01

    基础 <head>   <meta http-equiv="Content-Type" content="text/html; charset=utf- ...

  5. C#仪器数据文件解析-Word文件(doc、docx)

    不少仪器数据报告输出为Word格式文件,同Excel文件,Word文件doc和docx的存储格式是不同的,相应的解析Word文件的方式也类似,主要有以下方式: 1.通过MS Word应用程序的DCOM ...

  6. 【node】使用nvm管理node版本

    写在前面 nvm(nodejs version manager)是nodejs的管理工具,如果你想快速更新node版本,并且不覆盖之前的版本:或者想要在不同的node版本之间进行切换: 使用nvm来安 ...

  7. win10 uwp DataContext

    本文告诉大家DataContext的多种绑法. 适合于WPF的绑定和UWP的绑定. 我告诉大家很多个方法,所有的方法都有自己的优点和缺点,可以依靠自己喜欢的用法使用.当然,可以在新手面前秀下,一个页面 ...

  8. 张高兴的 Windows 10 IoT 开发笔记:HC-SR04 超声波测距模块

    HC-SR04 采用 IO 触发测距.下面介绍一下其在 Windows 10 IoT Core 环境下的用法. 项目运行在 Raspberry Pi 2/3 上,使用 C# 进行编码. 1. 准备 H ...

  9. 开始Java8之旅(四) --四大函数接口

    前言   Java8中函数接口有很多,大概有几十个吧,具体究竟是多少我也数不清,所以一开始看的时候感觉一脸懵逼,不过其实根本没那么复杂,毕竟不应该也没必要把一个东西设计的很复杂. 几个单词   在学习 ...

  10. Java 获取SQL查询语句结果

    step1:构造连接Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnec ...