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. HDU1403Longest Common Substring

    明天写 超时代码: #include<cstdio> #include<cstdlib> #include<iostream> #include<cstrin ...

  2. Sql Server 数据库中调用dll文件

    1.首先新建一个空的解决方案,并添加一个类库,代码如下,编译并生产dll using System; using System.Collections.Generic; using System.Da ...

  3. git的使用(入门篇)

    1.Git 的安装 Window 下的安装 从 http://git-scm.com/download 上下载window版的客户端,然后一直下一步下一步安装git即可,请注意,如果你不熟悉每个选项的 ...

  4. [js高手之路]html5 canvas动画教程 - 自己动手做一个类似windows的画图软件

    这个绘图工具,我还没有做完,不过已经实现了总架构,以及常见的简易图形绘制功能: 1,可以绘制直线,圆,矩形,正多边形[已完成] 2,填充颜色和描边颜色的选择[已完成] 3,描边和填充功能的选择[已完成 ...

  5. css笔记-文本样式

    聊聊text-decoration.text-indent.text-transform.letter-spacing.word-spacing.vertical-align.下面是一些常用设置文本样 ...

  6. OpenSCAD 建模:矿泉水瓶盖

    下载地址:https://github.com/ZhangGaoxing/openscad-models/tree/master/BottleCap 代码: module screw(r=){ ::] ...

  7. 数据结构与算法--从平衡二叉树(AVL)到红黑树

    数据结构与算法--从平衡二叉树(AVL)到红黑树 上节学习了二叉查找树.算法的性能取决于树的形状,而树的形状取决于插入键的顺序.在最好的情况下,n个结点的树是完全平衡的,如下图"最好情况&q ...

  8. 进程池与线程池(concurrent.futures)

    from concurrent.futures import ProcessPoolExecutor import os,time,random def task(n): print('%s is r ...

  9. LINUX 笔记-vmstat命令

    procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu----- r b swpd free buff ...

  10. Python 的装饰器

    Python 在语言级别提供了装饰器模式的实现,代码中Python内置的 functools.wraps 会完成包括函数名属性处理替换 #!/usr/bin/env python3 #--coding ...