what is setuptools
Setuptools is a fully-featured, actively-maintained, and stable library designed to facilitate packaging Python projects, where packaging includes:
- Python package and module definitions
- Distribution package metadata
- Test hooks
- Project installation
- Platform-specific details
- Python 3 support
install setuptools
- for ubuntu
sudo apt-get install python-setuptools - for package install
wget http://peak.telecommunity.com/dist/ez_setup.py
sudo python ez_setup.py
examples
i will use a simple example to show how setuptools work
- Create new directory
mkdir demo
cd demo
- Create setup files
from setuptools import setup
package = 'name'
version = '0.1'
setup(name=package,
version=version,
description="description",
url='url')
- variables
- name is package name
- version is package current version
- description is pacckage description
then run
python setup.py bdist_egg
to build python-eggs for python test, you will find a eggs file in demo/dist directory. if you look into this eggs file you will find out that it is a zip file
cli:~/demo$ file dist/name-0.1-py2.7.egg
dist/name-0.1-py2.7.egg: Zip archive data, at least v2.0 to extract
cli:~/demo$ unzip -l dist/name-0.1-py2.7.egg
Archive: dist/name-0.1-py2.7.egg
Length Date Time Name
--------- ---------- ----- ----
1 2016-10-17 11:17 EGG-INFO/dependency_links.txt
176 2016-10-17 11:17 EGG-INFO/PKG-INFO
120 2016-10-17 11:17 EGG-INFO/SOURCES.txt
1 2016-10-17 11:17 EGG-INFO/zip-safe
1 2016-10-17 11:17 EGG-INFO/top_level.txt
--------- -------
299 5 files
- add some funtion into package
mkdir demo
cd demo
touch __init__.py
put these code into __init__.py
def test():
print "hello world!"
if __name__ == '__main__':
test()
and then run python setup.py bdist_eggs
again
cli:~/demo$ unzip -l dist/name-0.1-py2.7.egg
Archive: dist/name-0.1-py2.7.egg
Length Date Time Name
--------- ---------- ----- ----
1 2016-10-17 11:17 EGG-INFO/dependency_links.txt
176 2016-10-17 11:17 EGG-INFO/PKG-INFO
120 2016-10-17 11:17 EGG-INFO/SOURCES.txt
1 2016-10-17 11:17 EGG-INFO/zip-safe
1 2016-10-17 11:17 EGG-INFO/top_level.txt
--------- -------
299 5 files
nothing changed becasue you haven’t added search directory option into setup.py
edit setup.py
like below
from setuptools import setup,find_packages
package = 'name'
version = '0.1'
setup(name=package,
version=version,
description="description",
packages=find_packages(),
url='url')
resetup it again
then you will find something changed
cli:~/demo$ unzip -l dist/name-0.1-py2.7.egg
Archive: dist/name-0.1-py2.7.egg
Length Date Time Name
--------- ---------- ----- ----
137 2016-10-17 11:33 EGG-INFO/SOURCES.txt
176 2016-10-17 11:33 EGG-INFO/PKG-INFO
1 2016-10-17 11:33 EGG-INFO/dependency_links.txt
5 2016-10-17 11:33 EGG-INFO/top_level.txt
1 2016-10-17 11:33 EGG-INFO/zip-safe
75 2016-10-17 11:30 demo/__init__.py
341 2016-10-17 11:33 demo/__init__.pyc
--------- -------
736 7 files
we have __init__.py
in our package now
now install it using python setup.py install
, and import it in python
>>> import demo
>>> demo.test()
hello world
>>>
great it works now