python制作egg包

egg包是目前最流行的python应用打包部署方式。

制作my_test.egg包:

环境:debian8.10,python2.7(默认包含setuptools)

首先建立工程目录egg_env,初始化一个setup.py文件:
setup.py,setup函数接收一系列属性作为配置参数。
name:name是egg包的名称,也是寻找要打包的文件夹的名称,默认是UNKNOWN。
version:版本号,默认0.0.0
packages:这里要用到setuptools的另一个函数find_packages,用来将指定目录下的文件打包。
zip_safe:默认是False,这样在每次生成egg包时都会检查项目文件的内容,确保无误。
还有一些其他描述性的属性,如description,long_description,author,author_email等。
root@debian:~# mkdir egg_env
root@debian:~# cd egg_env
root@debian:~/egg_env# vi setup.py
root@debian:~/egg_env# cat setup.py 
#!/usr/bin/python
#-*- coding:utf-8 -*-

from setuptools import setup, find_packages

setup(
name = "egg_demo",
version="1.1.0",
packages = find_packages(),
zip_safe = False,

description = "egg demo.",
long_description = "egg demo, hello.",
author = "jason",
author_email = "jason@csdn.com",

license = "GPL",
keywords = ("test", "egg"),
platforms = "Independant",
url = "",
)
root@debian:~/egg_env# 
创建我们的demo项目egg_demo,初始化一个__init__.py文件:
root@debian:~/egg_env# mkdir egg_demo
root@debian:~/egg_env# cd egg_demo/
root@debian:~/egg_env/egg_demo# vi __init__.py 
root@debian:~/egg_env/egg_demo# cat __init__.py 
#!/usr/bin/python
#-*- coding:utf-8 -*-

def test():
print("hello, world")

if __name__ == "__main__":

test()
root@debian:~/egg_env/egg_demo#
root@debian:~/egg_env/egg_demo# ls
__init__.py
root@debian:~/egg_env/egg_demo# 
制作egg包:
root@debian:~/egg_env/egg_demo# cd ..
root@debian:~/egg_env# ls
egg_demo  setup.py
root@debian:~/egg_env# python setup.py bdist_egg
running bdist_egg
running egg_info
creating egg_demo.egg-info
writing egg_demo.egg-info/PKG-INFO
writing top-level names to egg_demo.egg-info/top_level.txt
writing dependency_links to egg_demo.egg-info/dependency_links.txt
writing manifest file 'egg_demo.egg-info/SOURCES.txt'
reading manifest file 'egg_demo.egg-info/SOURCES.txt'
writing manifest file 'egg_demo.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/egg_demo
copying egg_demo/__init__.py -> build/lib.linux-x86_64-2.7/egg_demo
creating build/bdist.linux-x86_64
creating build/bdist.linux-x86_64/egg
creating build/bdist.linux-x86_64/egg/egg_demo
copying build/lib.linux-x86_64-2.7/egg_demo/__init__.py -> build/bdist.linux-x86_64/egg/egg_demo
byte-compiling build/bdist.linux-x86_64/egg/egg_demo/__init__.py to __init__.pyc
creating build/bdist.linux-x86_64/egg/EGG-INFO
copying egg_demo.egg-info/PKG-INFO -> build/bdist.linux-x86_64/egg/EGG-INFO
copying egg_demo.egg-info/SOURCES.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying egg_demo.egg-info/dependency_links.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying egg_demo.egg-info/not-zip-safe -> build/bdist.linux-x86_64/egg/EGG-INFO
copying egg_demo.egg-info/top_level.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
creating dist
creating 'dist/egg_demo-1.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)
root@debian:~/egg_env# 
制作完成,
root@debian:~/egg_env# ls
build  dist  egg_demo  egg_demo.egg-info  setup.py
root@debian:~/egg_env# ls dist/
egg_demo-1.1.0-py2.7.egg
root@debian:~/egg_env# 
可以看到多了三个文件夹。而在dist文件夹下,有一个egg文件:egg_demo-1.1.0-py2.7.egg。
产蛋成功!先看看这个egg文件是什么格式的:
root@debian:~/egg_env# file dist/egg_demo-1.1.0-py2.7.egg 
dist/egg_demo-1.1.0-py2.7.egg: Zip archive data, at least v2.0 to extract
root@debian:~/egg_env# 
原来是一个zip压缩包,看看内部构造:
root@debian:~/egg_env# unzip -l dist/egg_demo-1.1.0-py2.7.egg
Archive:  dist/egg_demo-1.1.0-py2.7.egg
  Length      Date    Time    Name
---------  ---------- -----   ----
  194  2018-06-25 19:46   egg_demo/__init__.pyc
   85  2018-06-25 19:44   egg_demo/__init__.py
  217  2018-06-25 19:46   EGG-INFO/PKG-INFO
  188  2018-06-25 19:46   EGG-INFO/SOURCES.txt
1  2018-06-25 19:46   EGG-INFO/dependency_links.txt
1  2018-06-25 19:46   EGG-INFO/not-zip-safe
9  2018-06-25 19:46   EGG-INFO/top_level.txt
---------                     -------
  695                     7 files
root@debian:~/egg_env# 
由上可见,已经生成了一个完整的egg包,egg_demo可以修改,重新生成egg包即可。
安装egg_demo包:
root@debian:~/egg_env# python setup.py install
running install
running bdist_egg
running egg_info
writing egg_demo.egg-info/PKG-INFO
writing top-level names to egg_demo.egg-info/top_level.txt
writing dependency_links to egg_demo.egg-info/dependency_links.txt
reading manifest file 'egg_demo.egg-info/SOURCES.txt'
writing manifest file 'egg_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/egg_demo
copying build/lib.linux-x86_64-2.7/egg_demo/__init__.py -> build/bdist.linux-x86_64/egg/egg_demo
byte-compiling build/bdist.linux-x86_64/egg/egg_demo/__init__.py to __init__.pyc
creating build/bdist.linux-x86_64/egg/EGG-INFO
copying egg_demo.egg-info/PKG-INFO -> build/bdist.linux-x86_64/egg/EGG-INFO
copying egg_demo.egg-info/SOURCES.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying egg_demo.egg-info/dependency_links.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying egg_demo.egg-info/not-zip-safe -> build/bdist.linux-x86_64/egg/EGG-INFO
copying egg_demo.egg-info/top_level.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
creating 'dist/egg_demo-1.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)
Processing egg_demo-1.1.0-py2.7.egg
creating /usr/local/lib/python2.7/dist-packages/egg_demo-1.1.0-py2.7.egg
Extracting egg_demo-1.1.0-py2.7.egg to /usr/local/lib/python2.7/dist-packages
Adding egg-demo 1.1.0 to easy-install.pth file

Installed /usr/local/lib/python2.7/dist-packages/egg_demo-1.1.0-py2.7.egg
Processing dependencies for egg-demo==1.1.0
Finished processing dependencies for egg-demo==1.1.0
root@debian:~/egg_env#
安装完毕!接下来我们就可以直接通过import来使用啦!
可以在/usr/local/lib/python2.7/dist-packages看到我们安装的egg_demo文件夹,
并且会有在easy-install.pth下会有egg_demo信息。
root@debian:/usr/local/lib/python2.7/dist-packages# ls
easy-install.pth  egg_demo-1.1.0-py2.7.egg  uncompyle2 uncompyle2-1.1.egg-info
root@debian:/usr/local/lib/python2.7/dist-packages# 
root@debian:~# python -c "from egg_demo import test;test()"
hello, world
root@debian:~#  
root@debian:~# python
Python 2.7.9 (default, Jun 29 2016, 13:08:31) 
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import egg_demo
>>> egg_demo.test()
hello, world
>>> 
卸载egg包:
root@debian:/usr/local/lib/python2.7/dist-packages# easy_install -m egg_demo
Searching for egg-demo
Best match: egg-demo 1.1.0
Processing egg_demo-1.1.0-py2.7.egg
Removing egg-demo 1.1.0 from easy-install.pth file

Using /usr/local/lib/python2.7/dist-packages/egg_demo-1.1.0-py2.7.egg

Because this distribution was installed --multi-version, before you can
import modules from this package in an application, you will need to
'import pkg_resources' and then use a 'require()' call similar to one of
these examples, in order to select the desired version:

pkg_resources.require("egg-demo")  # latest installed version
pkg_resources.require("egg-demo==1.1.0")  # this exact version
pkg_resources.require("egg-demo>=1.1.0")  # this version or higher

Processing dependencies for egg-demo
Finished processing dependencies for egg-demo
root@debian:/usr/local/lib/python2.7/dist-packages# 


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
是的,Pythonegg可以含多个层级的Python。这意味着你可以将多个Python模块和组织到一个egg中,方便分发和安装。以下是一个简单的例子: 假设你有以下的Python结构: ``` mypackage/ __init__.py module1.py module2.py subpackage/ __init__.py submodule1.py submodule2.py ``` 要将这个成一个egg,可以使用setuptools库中的`setup()`函数。在`setup()`函数中,需要指定的名称、版本号、作者、描述等信息,以及含的Python模块和。下面是一个示例setup.py文件的示例,可以将上述Python成一个名为`mypackage`、版本号为`1.0.0`的egg: ```python from setuptools import setup, find_packages setup( name='mypackage', version='1.0.0', author='Your Name', description='A description of my package', packages=find_packages(), include_package_data=True, ) ``` 在上面的`setup()`函数中,`name`和`version`指定了的名称和版本号,`author`和`description`分别是作者和的描述信息。`packages=find_packages()`指定了要含的Python,`include_package_data=True`表示要中的数据文件(如配置文件、图片等)。 要将这个Pythonegg,可以在命令行中进入的根目录,然后执行以下命令: ``` python setup.py bdist_egg ``` 这将在`dist`目录下生成一个名为`mypackage-1.0.0-py3.7.egg`的egg文件。可以将这个egg文件分发给其他人,并通过pip等工具安装。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值