Build Python package from source
Note
CatBoost Python package supports only CPython Python implementation.
Warning
This page describes the new approach for building using standard Python tools that works since this commit.
For building with earlier versions see these pages:
Source code
CatBoost source code is stored as a Git repository on GitHub at https://github.com/catboost/catboost/. You can obtain a local copy of this Git repository by running the following command from a command line interpreter (you need to have Git command line tools installed):
git clone https://github.com/catboost/catboost.git
Later in this document $CATBOOST_SRC_ROOT refers to the root dir of the local working copy of the source code cloned from the GitHub CatBoost repository.
Dependencies and requirements
-
As CatBoost Python package has a native extension library as its' core build environment setup for CMake is required.
-
setuptoolsPython package, version 64.0+. Installed by default for Python < 3.12, an explicit installation is needed for Python 3.12+. -
Other setup dependencies that can be formulated as python packages are listed in
pyproject.toml'sbuild-system.requiresand insetup.pyin standardsetup_requiresparameter and processed using standard Python tools.Note
For some reason Python 3.12 fails to automatically resolve build/setup dependencies in a way that they are buildable so it is recommended to install the following packages using pip explicitly:
- setuptools
- wheel
- jupyterlab (3.x, 4.x is not supported yet, see the relevant issue)
- conan (2.4.1+, for revisions before 21a3f85 only conan 1.x with versions 1.62.0+ is supported) -
For building CatBoost visualization widget bundled together with the python package (enabled by default) additional setup is required:
- Node.js installation with
npmcommand accessible from the shell. rimrafNode.js package installed withnpm's--globaloption (this wayrimrafcommand will be accessible from the shell).yarnpackage manager, version from 1.x series,1.22.10or later. Installed withnpm's--globaloption (this wayyarncommand will be accessible from the shell)
An example command to install:npm install --global yarn@1.22.10.
If you don't need CatBoost visualization widget support you can disable it's building and bundling with the CatBoost python package by passing
--no-widgetbuild/installation option. - Node.js installation with
-
Installation dependencies are listed in
setup.pyin standardinstall_requiresparameter and processed using standard Python tools. -
User-defined functions
If you want to use custom metrics or objectives implemented in your own python code you should install
numbapackage to speed up the code execution using JIT compilation.If you want to use custom metrics or objectives on GPUs with CUDA support you must install
numbapackage for JIT compilation of CUDA code.
Installation ofnumba-cudapackage is also encouraged.
CUDA itself (not only drivers) must be installed on machines where this code is executed.
SeenumbaCUDA support documentation for more details.These packages are not listed in package requirements that are installed automatically because they are not needed for other functionality.
Training or inference on CUDA-enabled GPUs requires NVIDIA Driver of version 450.80.02 or higher.
Building
Open the $CATBOOST_SRC_ROOT/catboost/python-package directory from the local copy of the CatBoost repository.
Use Python's standard procedures:
Build the wheel distribution
Warning
Note that built Python wheels will be binary compatible only with the same Python X.Y versions.
python setup.py bdist_wheel <options>
Options can be listed by calling python setup.py bdist_wheel --help.
One important option is --prebuilt-extensions-build-root-dir=<path>. It allows to use already built binary _catboost extension shared library. See Build native artifacts. Set this option value to $CMAKE_BINARY_DIR.
The resulting wheel distribution will be created in dist/catboost-<version>-<...>.whl
Build the source distribution (sdist)
Note
Python package build has to be installed
python -m build --sdist
The resulting source distribution will be created in dist/catboost-<version>.tar.gz file.
Other useful commands
-
build_widget. Build CatBoost widget.python setup.py build_widgetUseful if widget code remains unchanged but you want to rebuild other parts. Then run
build_widgetonce and then in subsequent calls tobdist_wheelor other commands use--prebuilt-widgetoption.
Installation
Directly from the source directory
Builds in the process. So build environment setup for CMake is required.
python -m pip install . <options>
You can pass options to setup.py's install stage by using --install-option options like this:
python -m pip install . --install-option=--with-hnsw --install-option=--with-cuda=/usr/local/cuda-11
Create editable install
Builds in the process. So build environment setup for CMake is required.
python -m pip install --editable . <options>
You can pass options to setup.py's install stage by using --install-option options like this:
python -m pip install --install-option=--with-hnsw --install-option=--with-cuda=/usr/local/cuda-11/ --editable .
Install from the built wheel
python -m pip install <path-to-wheel>
Install from the source distribution
Builds in the process. So build environment setup for CMake is required.
python -m pip install <path-to-sdist-tar.gz>
Note
If CUDA_PATH or CUDA_ROOT environment variable is defined and contains a path to a valid CUDA installation, then CatBoost python package will be built with this CUDA version.
Otherwise CUDA support will be disabled in the package.