Domen Kožar / @iElectric @ nixcon 2015
History
setup.py
from distutils.core import setup
setup(name="sadness", version="0.1", py_modules=["pure_sadness.py"])
$ cat setup.py
$ sudo python setup.py install
Build and install packages
2000: INCLUDED in Python standard library
Build C extensions
$ python setup.py build_ext
No dependency management
No way to reproduce an installation
Create a tarball
$ python setup.py sdist
No download capabilities (PyPi)
from setuptools import setup, find_packages
setup(name="sadness", version="0.1", packages=find_packages())
$ cat setup.py
$ easy_install Django
Installer for PyPI
Eggs (zipfile binary format)
$ python setup.py build_egg
No sanity in code whatsoever
No release for years since 2009
Monkeypatches distutils :-(
$ python setup.py sdist
No uninstall
Whole packaging process from a single setup.py
easy_install parses all links on https://pypi.python.org/pypi/Django/1.8
and for each link parsed HTML for a possible tarball release
pip 1.5 (2014-01-01)
stops parsing links
$ virtualenv --no-site-packages test/
New python executable in test/bin/python2.7
Also creating executable in test/bin/python
Installing setuptools...done.
$ ls test
bin include lib
$ source bin/activate
$ pip install Django
$ pip uninstall Django
Ever been frustrated with easy_install? Interested in an alternative? - Ian Bicking
UX friendly
separates build/install phases
"freeze" dependency versions
Authors will have to write a setup.cfg file and run a few commands to package and distribute their code - Tarek Ziadé
Burnout (2012). Distutils2 abandoned, distribute merges back into setuptools
Providing a backward compatible version to replace Setuptools and make all distributions that depend on Setuptools work as before, but with less bugs and behaviorial issues - Tarek Ziadé
setup(name='mr.bob',
version='0.1.3',
description='Bob renders directory structure templates',
long_description=read('README.rst') + '\n' + read('HISTORY.rst'),
classifiers=[
"Programming Language :: Python :: Implementation :: CPython",
"Programming Language :: Python :: Implementation :: PyPy",
"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.3",
"Programming Language :: Python :: 3.4",
],
author='Domen Kozar, Tom Lazar',
url='https://github.com/iElectric/mr.bob.git',
license='BSD',
packages=find_packages(),
install_requires=['six>=1.2.0'],
extras_require={
'test': [
'nose',
'coverage<3.6dev',
'flake8>2.0',
'mock',
],
'development': [
'zest.releaser',
'Sphinx',
],
},
entry_points="""
[console_scripts]
mrbob = mrbob.cli:main
""",
include_package_data=True)
$ nix-shell -p pythonPackages.pbr --command 'python -c "import sys;print(sys.path)"'
['',
'/nix/store/9cb0j2y4rsh3mvdizhv22maibp8ifawf-python2.7-pbr-0.9.0/lib/python2.7/site-packages',
'/nix/store/qhnd3bmlvqqvqw2rdy7fk3rc12mw4z2h-python-recursive-pth-loader-1.0/lib/python2.7/site-packages',
'/nix/store/49zg30nx308mad45yd9vd4pk3jlddkks-python-2.7.10/lib/python2.7/site-packages',
'/nix/store/v930s7l861my7haprl8r2w0d9aar29cs-python2.7-setuptools-18.2/lib/python2.7/site-packages',
'/nix/store/v930s7l861my7haprl8r2w0d9aar29cs-python2.7-setuptools-18.2/lib/python2.7/site-packages/setuptools-18.2-py2.7.egg',
'/nix/store/49zg30nx308mad45yd9vd4pk3jlddkks-python-2.7.10/lib/python27.zip',
'/nix/store/49zg30nx308mad45yd9vd4pk3jlddkks-python-2.7.10/lib/python2.7',
'/nix/store/49zg30nx308mad45yd9vd4pk3jlddkks-python-2.7.10/lib/python2.7/plat-linux2',
'/nix/store/49zg30nx308mad45yd9vd4pk3jlddkks-python-2.7.10/lib/python2.7/lib-tk',
'/nix/store/49zg30nx308mad45yd9vd4pk3jlddkks-python-2.7.10/lib/python2.7/lib-old',
'/nix/store/49zg30nx308mad45yd9vd4pk3jlddkks-python-2.7.10/lib/python2.7/lib-dynload']
$ nix-shell -p pythonPackages.pbr --command "echo \$PYTHONPATH"
/nix/store/9cb0j2y4rsh3mvdizhv22maibp8ifawf-python2.7-pbr-0.9.0/lib/python2.7/site-packages:
/nix/store/qhnd3bmlvqqvqw2rdy7fk3rc12mw4z2h-python-recursive-pth-loader-1.0/lib/python2.7/site-packages:
/nix/store/49zg30nx308mad45yd9vd4pk3jlddkks-python-2.7.10/lib/python2.7/site-packages:
/nix/store/v930s7l861my7haprl8r2w0d9aar29cs-python2.7-setuptools-18.2/lib/python2.7/site-packages
setup_requires (build time dependencies)
but (sometimes) ...
rules what files to include into tarball
$ cat MANIFEST.in
graft mypackage
but ..
import sys
if sys.version_info < (2, 7):
install_requires.append("argparse")
try:
import argparse
except ImportError:
install_requires.append('argparse')
argparse; python_version < '2.7'(PEP496)
# if Homebrew is installed, use its lib and include directories
import subprocess
try:
prefix = subprocess.check_output(
['brew', '--prefix']
).strip().decode('latin1')
except:
# Homebrew not installed
prefix = None
ft_prefix = None
if prefix:
# add Homebrew's include and lib directories
_add_directory(library_dirs, os.path.join(prefix, 'lib'))
_add_directory(include_dirs, os.path.join(prefix, 'include'))
ft_prefix = os.path.join(prefix, 'opt', 'freetype')
if ft_prefix and os.path.isdir(ft_prefix):
# freetype might not be linked into Homebrew's prefix
_add_directory(library_dirs, os.path.join(ft_prefix, 'lib'))
_add_directory(
include_dirs, os.path.join(ft_prefix, 'include'))
else:
# fall back to freetype from XQuartz if
# Homebrew's freetype is missing
_add_directory(library_dirs, "/usr/X11/lib")
_add_directory(include_dirs, "/usr/X11/include")
https://github.com/python-pillow/Pillow/blob/master/setup.py
Nix: buildPythonPackage
if disabled
then throw "${name} not supported for interpreter ${python.executable}"
else
python.stdenv.mkDerivation (builtins.removeAttrs attrs ["disabled"] // {
inherit doCheck;
name = namePrefix + name;
buildInputs = [
wrapPython
(distutils-cfg.override { extraCfg = distutilsExtraCfg; })
] ++ buildInputs ++ pythonPath
++ (lib.optional (lib.hasSuffix "zip" attrs.src.name or "") unzip);
propagatedBuildInputs = propagatedBuildInputs ++ [ recursivePthLoader python setuptools ];
configurePhase = attrs.configurePhase or ''
runHook preConfigure
export DETERMINISTIC_BUILD=1
sed -i '0,/import distutils/s//import setuptools;import distutils/' setup.py
sed -i '0,/from distutils/s//import setuptools;from distutils/' setup.py
runHook postConfigure
'';
checkPhase = attrs.checkPhase or ''
runHook preCheck
${python}/bin/${python.executable} setup.py test
runHook postCheck
'';
buildPhase = attrs.buildPhase or ''
runHook preBuild
${python}/bin/${python.executable} setup.py build \
${lib.concatStringsSep " " setupPyBuildFlags}
runHook postBuild
'';
installPhase = attrs.installPhase or ''
runHook preInstall
mkdir -p "$out/${python.sitePackages}"
export PYTHONPATH="$out/${python.sitePackages}:$PYTHONPATH"
${python}/bin/${python.executable} setup.py install \
--install-lib=$out/${python.sitePackages} \
--old-and-unmanageable \
--prefix="$out" ${lib.concatStringsSep " " setupPyInstallFlags}
eapth="$out/${python.sitePackages}/easy-install.pth"
if [ -e "$eapth" ]; then
mv "$eapth" $(dirname "$eapth")/${name}.pth
fi
rm -f $out/${python.sitePackages}/site.py*
runHook postInstall
'';
postFixup = attrs.postFixup or ''
wrapPythonPrograms
createBuildInputsPth build-inputs "$buildInputStrings"
for inputsfile in propagated-build-inputs propagated-native-build-inputs; do
if test -e $out/nix-support/$inputsfile; then
createBuildInputsPth $inputsfile "$(cat $out/nix-support/$inputsfile)"
fi
done
'';
shellHook = attrs.shellHook or ''
${preShellHook}
if test -e setup.py; then
tmp_path=$(mktemp -d)
export PATH="$tmp_path/bin:$PATH"
export PYTHONPATH="$tmp_path/${python.sitePackages}:$PYTHONPATH"
${python.interpreter} setup.py develop --prefix $tmp_path
fi
${postShellHook}
'';
collision between
`/nix/store/...-logilab-astng-0.24.3/lib/python2.7/site-packages/logilab/__init__.py' and
`/nix/store/...-logilab-common-0.61.0/lib/python2.7/site-packages/logilab/__init__.py'
$ cat setup.py
setup(
name="logilab.astng",
# ...
namespace_packages=['logilab'],
)
$ cat logilab/__init__.py
__import__('pkg_resources').declare_namespace(__name__)
If Django 1.6 and Django 1.7 are in closure,
the first one in $PYTHONPATH will be used
$ nix-env -i python2.7 python2.7-numpy
$ python -C "import numpy"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named numpy
is this expected?
python3 doesn't have $out/bin/python, but $out/bin/python3
sed -i 's@python@${python.interpreter}@' .testr.conf
tests depend on binaries from package being installed
doCheck = false;
doInstallCheck = true;
installCheckPhase = ''
export PATH=$PATH:$out/bin
${python.interpreter} setup.py test
'';
propagatedBuildInputs = optionals (!isPyPy) [ cffi ];
propagatedBuildInputs = optionals (pythonOlder "3.4") self.enum34;
some interpreters already ship with a library
python decodes files from filesystem based on locale
buildInputs = [ pkgs.glibcLocales ];
preCheck = ''
export LC_ALL="en_US.UTF-8"
'';
installation is looking for a specific version of the package
substituteInPlace setup.py --replace "httplib2==0.8" "httplib2"
ImportError for readline, sqlite3, curses, crypt
propagatedBuildInputs = [ python.modules.curses ];
python setup.py bdist_wheel
python setup.py build
pip install *.whl
python setup.py install
A wheel is a ZIP-format archive with a specially formatted filename and the .whl extension
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.