Python的egg包

转载:http://www.litrin.net/2015/06/10/python%E7%9A%84egg%E5%8C%85/


尽管现在有了wheel这类更为“先进”的包装方式,但无可否认的是egg包+easy_install方式是最为热门的Python扩展包安装方式。何况python的意思是“蝰蛇”,蛇的繁殖方式当然应该是蛋(egg)咯!这些天恰巧我正在制作一系列的Python扩展,这里就简单说说如何制作一个自己的egg包。

首先一个问题是“Python的package是如何定义的?”这个似乎是面试Python程序员最常出现的题目。简而言之,在目录下放置一个__init__.py文件,这个目录就会成为一个package。

首先是制作一个干净的目录用于打包,本例中就简单粗暴的定义为~/project目录,要打包的package就命名为spawn-egg(注:这里只是用来Demo的名字,命名方式并不适合所有的python环境)。目录结构如下:

ilab@iLab-Dev:~/project$ find ./
./
./spawn-egg
./spawn-egg/Base.pyc
./spawn-egg/hello.py
./spawn-egg/Base.py
./spawn-egg/__init__.py

然后就是制作一个setup.py脚本,与其说是脚本,这个文件更像是一个配置文件。

vi ~/project/setup.py

from setuptools import setup, find_packages

setup(
 name = "spawn-egg",
 version="0.1.0",
 packages = find_packages(),
 description = "test how to make eggs",
 author = "Litrin J.",
 author_email = "[email protected]",

 license = "GPL",
 keywords = ("test", "python"),
 platforms = "Independant",
 url = "",
 )

就是一个setup函数,入参真心不少,好在字面上很容易理解它的用途,总结起来就这几个比较常用:

  • name:就是名字了
  • version:版本号
  • packages:包含的package,setuptools自带了一个find_packages()工具可以自动从name同名的folder下找到package。
  • description:对于这个包的描述,如果描述内容很长,可以把这里当成摘要,更详细的内容使用long_description参数
  • author/author_email:作者和邮箱
  • keywords:关键字,便于发布到pip上,用于搜索。

更详细的解释请见

扫描二维码关注公众号,回复: 867370 查看本文章

准备打包环境。打包环境需要setuptools,对于Ubuntu用户来说,直接

apt-get install python-setuptools

生成egg,激动人心的时刻来了!

ilab@iLab-Dev:~/project$ python setup.py bdist_egg
running bdist_egg
running egg_info
creating spawn_egg.egg-info
writing spawn_egg.egg-info/PKG-INFO
writing top-level names to spawn_egg.egg-info/top_level.txt
writing dependency_links to spawn_egg.egg-info/dependency_links.txt
writing manifest file 'spawn_egg.egg-info/SOURCES.txt'
reading manifest file 'spawn_egg.egg-info/SOURCES.txt'
writing manifest file 'spawn_egg.egg-info/SOURCES.txt'
installing library code to build/bdist.linux-x86_64/egg
running install_lib
running build_py
creating build
creating build/lib.linux-x86_64-2.7
creating build/lib.linux-x86_64-2.7/spawn-egg
copying spawn-egg/hello.py -> build/lib.linux-x86_64-2.7/spawn-egg
copying spawn-egg/Base.py -> build/lib.linux-x86_64-2.7/spawn-egg
copying spawn-egg/__init__.py -> build/lib.linux-x86_64-2.7/spawn-egg
creating build/bdist.linux-x86_64
creating build/bdist.linux-x86_64/egg
creating build/bdist.linux-x86_64/egg/spawn-egg
copying build/lib.linux-x86_64-2.7/spawn-egg/hello.py -> build/bdist.linux-x86_64/egg/spawn-egg
copying build/lib.linux-x86_64-2.7/spawn-egg/Base.py -> build/bdist.linux-x86_64/egg/spawn-egg
copying build/lib.linux-x86_64-2.7/spawn-egg/__init__.py -> build/bdist.linux-x86_64/egg/spawn-egg
byte-compiling build/bdist.linux-x86_64/egg/spawn-egg/hello.py to hello.pyc
byte-compiling build/bdist.linux-x86_64/egg/spawn-egg/Base.py to Base.pyc
byte-compiling build/bdist.linux-x86_64/egg/spawn-egg/__init__.py to __init__.pyc
creating build/bdist.linux-x86_64/egg/EGG-INFO
copying spawn_egg.egg-info/PKG-INFO -> build/bdist.linux-x86_64/egg/EGG-INFO
copying spawn_egg.egg-info/SOURCES.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying spawn_egg.egg-info/dependency_links.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying spawn_egg.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/spawn_egg-0.1.0-py2.7.egg' and adding 'build/bdist.linux-x86_64/egg' to it
removing 'build/bdist.linux-x86_64/egg' (and everything under it)

OK,egg文件已经在dist文件夹下生成好了,其实就是一个zip文件包,可以简单地用unzip命令check一下!

ilab@iLab-Dev:~/project/dist$ unzip -l spawn_egg-0.1.0-py2.7.egg
Archive: spawn_egg-0.1.0-py2.7.egg
 Length Date Time Name
--------- ---------- ----- ----
 474 2015-06-10 15:31 spawn-egg/Base.pyc
 429 2015-06-10 15:31 spawn-egg/hello.pyc
 102 2015-06-10 15:04 spawn-egg/hello.py
 137 2015-06-10 15:31 spawn-egg/__init__.pyc
 44 2015-06-10 15:04 spawn-egg/Base.py
 0 2015-06-10 15:02 spawn-egg/__init__.py
 10 2015-06-10 15:31 EGG-INFO/top_level.txt
 199 2015-06-10 15:31 EGG-INFO/SOURCES.txt
 227 2015-06-10 15:31 EGG-INFO/PKG-INFO
 1 2015-06-10 15:31 EGG-INFO/dependency_links.txt
 1 2015-06-10 15:31 EGG-INFO/zip-safe
--------- -------
 1624 11 files

尝试安装一下

ilab@iLab-Dev:~/project/dist$ sudo easy_install spawn_egg-0.1.0-py2.7.egg
[sudo] password for ilab:
Processing spawn_egg-0.1.0-py2.7.egg
creating /usr/local/lib/python2.7/dist-packages/spawn_egg-0.1.0-py2.7.egg
Extracting spawn_egg-0.1.0-py2.7.egg to /usr/local/lib/python2.7/dist-packages
Adding spawn-egg 0.1.0 to easy-install.pth file

Installed /usr/local/lib/python2.7/dist-packages/spawn_egg-0.1.0-py2.7.egg
Processing dependencies for spawn-egg==0.1.0
Finished processing dependencies for spawn-egg==0.1.0

没有报错!这个时候/usr/local/lib/python2.7/dist-packages/就是您安装好的python包,你可以在这里删除/修改您的安装包。


猜你喜欢

转载自blog.csdn.net/feng98ren/article/details/80332938
egg