6.17. Using Python

The Ports Collection supports parallel installation of multiple Python versions. Ports must use a correct python interpreter, according to the user-settable PYTHON_VERSION. Most prominently, this means replacing the path to python executable in scripts with the value of PYTHON_CMD.

Ports that install files under PYTHON_SITELIBDIR must use the pyXY- package name prefix, so their package name embeds the version of Python they are installed into.

PKGNAMEPREFIX=	${PYTHON_PKGNAMEPREFIX}
Table 6.25. Most Useful Variables for Ports That Use Python
USES=pythonThe port needs Python. The minimal required version can be specified with values such as 2.7+. Version ranges can also be specified by separating two version numbers with a dash: USES=python:3.2-3.3
USE_PYTHON=distutilsUse Python distutils for configuring, compiling, and installing. This is required when the port comes with setup.py. This overrides the do-build and do-install targets and may also override do-configure if GNU_CONFIGURE is not defined. Additionally, it implies USE_PYTHON=flavors.
USE_PYTHON=autoplistCreate the packaging list automatically. This also requires USE_PYTHON=distutils to be set.
USE_PYTHON=concurrentThe port will use an unique prefix, typically PYTHON_PKGNAMEPREFIX for certain directories, such as EXAMPLESDIR and DOCSDIR and also will append a suffix, the python version from PYTHON_VER, to binaries and scripts to be installed. This allows ports to be installed for different Python versions at the same time, which otherwise would install conflicting files.
USE_PYTHON=flavorsThe port does not use distutils but still supports multiple Python versions. FLAVORS will be set to the supported Python versions. See Section 7.4, “USES=python and Flavors” for more information.
USE_PYTHON=optsuffixIf the current Python version is not the default version, the port will gain PKGNAMESUFFIX=${PYTHON_PKGNAMESUFFIX}. Only useful with flavors.
PYTHON_PKGNAMEPREFIXUsed as a PKGNAMEPREFIX to distinguish packages for different Python versions. Example: py27-
PYTHON_SITELIBDIRLocation of the site-packages tree, that contains installation path of Python (usually LOCALBASE). PYTHON_SITELIBDIR can be very useful when installing Python modules.
PYTHONPREFIX_SITELIBDIRThe PREFIX-clean variant of PYTHON_SITELIBDIR. Always use %%PYTHON_SITELIBDIR%% in pkg-plist when possible. The default value of %%PYTHON_SITELIBDIR%% is lib/python%%PYTHON_VERSION%%/site-packages
PYTHON_CMDPython interpreter command line, including version number.

Table 6.26. Python Module Dependency Helpers
PYNUMERICDependency line for numeric extension.
PYNUMPYDependency line for the new numeric extension, numpy. (PYNUMERIC is deprecated by upstream vendor).
PYXMLDependency line for XML extension (not needed for Python 2.0 and higher as it is also in base distribution).
PY_ENUM34Conditional dependency on devel/py-enum34 depending on the Python version.
PY_ENUM_COMPATConditional dependency on devel/py-enum-compat depending on the Python version.
PY_PATHLIBConditional dependency on devel/py-pathlib depending on the Python version.
PY_IPADDRESSConditional dependency on net/py-ipaddress depending on the Python version.
PY_FUTURESConditional dependency on devel/py-futures depending on the Python version.

A complete list of available variables can be found in /usr/ports/Mk/Uses/python.mk.

Important:

All dependencies to Python ports using Python flavors (either with USE_PYTHON=distutils or USE_PYTHON=flavors) must have the Python flavor appended to their origin using @${PY_FLAVOR}. See Example 6.25, “Makefile for a Simple Python Module”.

Example 6.25. Makefile for a Simple Python Module
PORTNAME=	sample
DISTVERSION=	1.2.3
CATEGORIES=	devel

MAINTAINER=	john@doe.tld
COMMENT=	Python sample module

RUN_DEPENDS=	${PYTHON_PKGNAMEPREFIX}six>0:devel/py-six@${PY_FLAVOR}

USES=		python
USE_PYTHON=	autoplist distutils

.include <bsd.port.mk>

Some Python applications claim to have DESTDIR support (which would be required for staging) but it is broken (Mailman up to 2.1.16, for instance). This can be worked around by recompiling the scripts. This can be done, for example, in the post-build target. Assuming the Python scripts are supposed to reside in PYTHONPREFIX_SITELIBDIR after installation, this solution can be applied:

(cd ${STAGEDIR}${PREFIX} \
  && ${PYTHON_CMD} ${PYTHON_LIBDIR}/compileall.py \
   -d ${PREFIX} -f ${PYTHONPREFIX_SITELIBDIR:S;${PREFIX}/;;})

This recompiles the sources with a path relative to the stage directory, and prepends the value of PREFIX to the file name recorded in the byte-compiled output file by -d. -f is required to force recompilation, and the :S;${PREFIX}/;; strips prefixes from the value of PYTHONPREFIX_SITELIBDIR to make it relative to PREFIX.

All FreeBSD documents are available for download at https://download.freebsd.org/ftp/doc/

Questions that are not answered by the documentation may be sent to <freebsd-questions@FreeBSD.org>.
Send questions about this document to <freebsd-doc@FreeBSD.org>.