Tuesday, May 28, 2013

[ Level 2 ] Create an egg for Python in Ubuntu.

How to create an egg file for Python.
First of all, you must have setuptools module.
$ sudo apt-get -y install python-setuptools

Now, you could try to create an empty egg now.
$ mkdir /tmp/demo
$ cd /tmp/demo
$ cat &ht; ./setup.py <<EOF
#!/bin/env python
#-*- coding:utf-8 -*-
from setuptools import setup
setup()
EOF
$ python setup.py bdist_egg ## bdist_egg is the option for creating egg.
$ ls -ALb
build  dist  setup.py  UNKNOWN.egg-info
You could find, we have three more directories after you execute setup.py
build ->
dist -> final egg file
UNKNOW.egg-info -> egg info

Now, we could give setuptools more information about egg.
cat > ./setup.py <<EOF
#!/bin/env python
#-*- coding:utf-8 -*-
from setuptools import setup, find_packages
setup(
        name = "my_first_egg",
        version="0.0.1",
        packages = find_packages(),
        zip_safe = False,

        description = "my first egg.",
        long_description = "long description of my first egg.",
        author = "Stanley Huang",
        author_email = "wenlien1001 at gmail.com",

        license = "GPLv2",
        keywords = ("demo", "python", "egg"),
        platforms = "Independant",
        url = "http://stanley-huang.blogspot.tw/",
        )
EOF
And you could create a sub folder named as "name" attribute (my_first_egg) and put your source code into ti.
$ mkdir ./my_first_app
$ touch ./my_first_app/__init__.py
$ cat > ./my_first_app/helloworld.py <<EOF
#!/bin/env python
#-*- coding:utf-8 -*-
print 'Hello World!'
EOF
$ chomd a+x ./my_first_app/helloworld.py
$ python setup.py bdist_egg
$ file ./dist/my_first_egg-0.0.1-py2.7.egg 
./dist/my_first_egg-0.0.1-py2.7.egg: Zip archive data, at least v2.0 to extract
$ unzip -l ./dist/my_first_egg-0.0.1-py2.7.egg
Archive:  ./dist/my_first_egg-0.0.1-py2.7.egg
  Length      Date    Time    Name
---------  ---------- -----   ----
        1  2013-05-28 08:40   EGG-INFO/dependency_links.txt
      239  2013-05-28 08:40   EGG-INFO/SOURCES.txt
        1  2013-05-28 08:40   EGG-INFO/not-zip-safe
       13  2013-05-28 08:40   EGG-INFO/top_level.txt
      295  2013-05-28 08:40   EGG-INFO/PKG-INFO
       38  2013-05-28 08:40   my_first_egg/helloworld.py
        0  2013-05-28 08:39   my_first_egg/__init__.py
      164  2013-05-28 08:40   my_first_egg/helloworld.pyc
      140  2013-05-28 08:40   my_first_egg/__init__.pyc
---------                     -------
      891                     9 files

Usually we put source file in src folder, therefore, you may want to change finding direcotory.
You could change the setting in setup.py
packages=find_packages('src'),
package_dir = {'':'src'}
You could try it!
Wish this helps.

regards,
Stanley Huang