setuptools module
Date: January 21st 2016
Last updated: January 22nd 2016
The setuptools module provides tools for packaging and distributing python code. setuptools was created to overcome some of the limitations in the distutils package (Not covered here. See this forum post for more info). setuptools is easy to use but requires several things to be done before implementation:
- place all files in its on directory (called distribution root directory)
- python packages go in subdirectories
- python packages need to have an empty "__init__.py" file
- Include a file named setup.py to the distribution root (holds metadata)
Directory structure:
└── distribution
├── pyhello
│ ├── hello.py
│ └── __init__.py
└── setup.py
hello.py
#!/home/ray/anaconda2/bin/python
def hello():
"""
Hello from python.
"""
print 'hello'
setup.py
from setuptools import setup
setup(
name = "pyhello",
version = "0.0.1",
packages = ['pyhello'],
long_description = "Say hello from python",
install_requires = [''],
# additional metadata
author = "Ray",
author_email = "[email protected]",
description = "This is my test package",
keywords = "hello",
url = "", # project home page
)
Run setup:
# create python egg
python setup.py bdist_egg
# create standalone distribution
# python setup.py sdist
Updated directory structure:
distribution
├── build
│ ├── bdist.linux-x86_64
│ └── lib
│ └── pyhello
│ ├── hello.py
│ └── __init__.py
├── dist
│ └── pyhello-0.0.1-py2.7.egg
├── pyhello
│ ├── hello.py
│ ├── hello.pyc
│ └── __init__.py
├── pyhello.egg-info
│ ├── dependency_links.txt
│ ├── PKG-INFO
│ ├── SOURCES.txt
│ └── top_level.txt
└── setup.py
Install:
python setup.py install
Package version:
pip freeze | grep "pyhello"
# Output
# pyhello==0.0.1
Use the package:
# from package import module as acronym
from pyhello import hello as h
# use the method called hello
h.hello()
# output
hello
Uninstalling:
pip uninstall pyhello
#Uninstalling pyhello-0.0.1:
# /home/ray/anaconda2/lib/python2.7/site-packages/pyhello-0.0.1-py2.7.egg
Proceed (y/n)? y
# Successfully uninstalled pyhello-0.0.1