Skip to content

[docs] update Python-package installation guide - #6767

Merged
StrikerRUS merged 121 commits into
masterfrom
docs/install-py
Apr 17, 2025
Merged

StrikerRUS merged 121 commits into
masterfrom
docs/install-py

Conversation

@StrikerRUS

@StrikerRUS StrikerRUS commented Dec 26, 2024 •

Copy link
Copy Markdown
Collaborator

Every installation method and some their combinations were tested with the help of our CI (Appveyor and GitHub Actions). Everything is working except --sanitizers flag: package installation is successful but it's import fails:

log:
Fatal Python error: Aborted

Current thread 0x00007ff856c629c0 (most recent call first):
  File "/Users/runner/miniforge/lib/python3.12/ctypes/__init__.py", line 379 in __init__
  File "/Users/runner/miniforge/lib/python3.12/ctypes/__init__.py", line 460 in LoadLibrary
  File "/Users/runner/miniforge/lib/python3.12/site-packages/lightgbm/libpath.py", line 49 in <module>
  File "<frozen importlib._bootstrap>", line 488 in _call_with_frames_removed
  File "<frozen importlib._bootstrap_external>", line 999 in exec_module
  File "<frozen importlib._bootstrap>", line 935 in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1331 in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1360 in _find_and_load
  File "/Users/runner/miniforge/lib/python3.12/site-packages/lightgbm/basic.py", line 9 in <module>
  File "<frozen importlib._bootstrap>", line 488 in _call_with_frames_removed
  File "<frozen importlib._bootstrap_external>", line 999 in exec_module
  File "<frozen importlib._bootstrap>", line 935 in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1331 in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1360 in _find_and_load
  File "/Users/runner/miniforge/lib/python3.12/site-packages/lightgbm/__init__.py", line 11 in <module>
  File "<frozen importlib._bootstrap>", line 488 in _call_with_frames_removed
  File "<frozen importlib._bootstrap_external>", line 999 in exec_module
  File "<frozen importlib._bootstrap>", line 935 in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1331 in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1360 in _find_and_load
  File "/Users/runner/work/LightGBM/LightGBM/tests/python_package_test/test_basic.py", line 15 in <module>
  File "/Users/runner/miniforge/lib/python3.12/site-packages/_pytest/assertion/rewrite.py", line 184 in exec_module
  File "<frozen importlib._bootstrap>", line 935 in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1331 in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1360 in _find_and_load
  File "<frozen importlib._bootstrap>", line 1387 in _gcd_import
  File "/Users/runner/miniforge/lib/python3.12/importlib/__init__.py", line 90 in import_module
  File "/Users/runner/miniforge/lib/python3.12/site-packages/_pytest/pathlib.py", line 587 in import_path
  File "/Users/runner/miniforge/lib/python3.12/site-packages/_pytest/python.py", line 493 in importtestmodule
  File "/Users/runner/miniforge/lib/python3.12/site-packages/_pytest/python.py", line 546 in _getobj
  File "/Users/runner/miniforge/lib/python3.12/site-packages/_pytest/python.py", line 284 in obj
  File "/Users/runner/miniforge/lib/python3.12/site-packages/_pytest/python.py", line 562 in _register_setup_module_fixture
  File "/Users/runner/miniforge/lib/python3.12/site-packages/_pytest/python.py", line 549 in collect
  File "/Users/runner/miniforge/lib/python3.12/site-packages/_pytest/runner.py", line 389 in collect
  File "/Users/runner/miniforge/lib/python3.12/site-packages/_pytest/runner.py", line 341 in from_call
  File "/Users/runner/miniforge/lib/python3.12/site-packages/_pytest/runner.py", line 391 in pytest_make_collect_report
  File "/Users/runner/miniforge/lib/python3.12/site-packages/pluggy/_callers.py", line 103 in _multicall
  File "/Users/runner/miniforge/lib/python3.12/site-packages/pluggy/_manager.py", line 120 in _hookexec
  File "/Users/runner/miniforge/lib/python3.12/site-packages/pluggy/_hooks.py", line [513](https://github.com/microsoft/LightGBM/actions/runs/12499103876/job/34873729965#step:3:514) in __call__
  File "/Users/runner/miniforge/lib/python3.12/site-packages/_pytest/runner.py", line 567 in collect_one_node
  File "/Users/runner/miniforge/lib/python3.12/site-packages/_pytest/main.py", line 835 in _collect_one_node
  File "/Users/runner/miniforge/lib/python3.12/site-packages/_pytest/main.py", line 970 in genitems
  File "/Users/runner/miniforge/lib/python3.12/site-packages/_pytest/main.py", line 975 in genitems
  File "/Users/runner/miniforge/lib/python3.12/site-packages/_pytest/main.py", line 809 in perform_collect
  File "/Users/runner/miniforge/lib/python3.12/site-packages/_pytest/main.py", line 347 in pytest_collection
  File "/Users/runner/miniforge/lib/python3.12/site-packages/pluggy/_callers.py", line 103 in _multicall
  File "/Users/runner/miniforge/lib/python3.12/site-packages/pluggy/_manager.py", line 120 in _hookexec
  File "/Users/runner/miniforge/lib/python3.12/site-packages/pluggy/_hooks.py", line 513 in __call__
  File "/Users/runner/miniforge/lib/python3.12/site-packages/_pytest/main.py", line 336 in _main
  File "/Users/runner/miniforge/lib/python3.12/site-packages/_pytest/main.py", line 283 in wrap_session
  File "/Users/runner/miniforge/lib/python3.12/site-packages/_pytest/main.py", line 330 in pytest_cmdline_main
  File "/Users/runner/miniforge/lib/python3.12/site-packages/pluggy/_callers.py", line 103 in _multicall
  File "/Users/runner/miniforge/lib/python3.12/site-packages/pluggy/_manager.py", line 120 in _hookexec
  File "/Users/runner/miniforge/lib/python3.12/site-packages/pluggy/_hooks.py", line 513 in __call__
  File "/Users/runner/miniforge/lib/python3.12/site-packages/_pytest/config/__init__.py", line 175 in main
  File "/Users/runner/miniforge/lib/python3.12/site-packages/_pytest/config/__init__.py", line 201 in console_main
  File "/Users/runner/miniforge/bin/pytest", line 8 in <module>

Extension modules: numpy._core._multiarray_umath, numpy.linalg._umath_linalg, scipy._lib._ccallback_c, numpy.random._common, numpy.random.bit_generator, numpy.random._bounded_integers, numpy.random._mt19937, numpy.random.mtrand, numpy.random._philox, numpy.random._pcg64, numpy.random._sfc64, numpy.random._generator, scipy.sparse._sparsetools, _csparsetools, scipy.sparse._csparsetools, scipy.linalg._fblas, scipy.linalg._flapack, scipy.linalg.cython_lapack, scipy.linalg._cythonized_array_utils, scipy.linalg._solve_toeplitz, scipy.linalg._decomp_lu_cython, scipy.linalg._matfuncs_sqrtm_triu, scipy.linalg.cython_blas, scipy.linalg._matfuncs_expm, scipy.linalg._decomp_update, scipy.sparse.linalg._dsolve._superlu, scipy.sparse.linalg._eigen.arpack._arpack, scipy.sparse.linalg._propack._spropack, scipy.sparse.linalg._propack._dpropack, scipy.sparse.linalg._propack._cpropack, scipy.sparse.linalg._propack._zpropack, scipy.sparse.csgraph._tools, scipy.sparse.csgraph._shortest_path, scipy.sparse.csgraph._traversal, scipy.sparse.csgraph._min_spanning_tree, scipy.sparse.csgraph._flow, scipy.sparse.csgraph._matching, scipy.sparse.csgraph._reordering, sklearn.__check_build._check_build, psutil._psutil_osx, psutil._psutil_posix, scipy.special._ufuncs_cxx, scipy.special._ufuncs, scipy.special._specfun, scipy.special._comb, scipy.special._ellip_harm_2, scipy.spatial._ckdtree, scipy._lib.messagestream, scipy.spatial._qhull, scipy.spatial._voronoi, scipy.spatial._distance_wrap, scipy.spatial._hausdorff, scipy.spatial.transform._rotation, scipy.optimize._group_columns, scipy.optimize._trlib._trlib, scipy.optimize._lbfgsb, _moduleTNC, scipy.optimize._moduleTNC, scipy.optimize._cobyla, scipy.optimize._slsqp, scipy.optimize._minpack, scipy.optimize._lsq.givens_elimination, scipy.optimize._zeros, scipy.optimize._highs.cython.src._highs_wrapper, scipy.optimize._highs._highs_wrapper, scipy.optimize._highs.cython.src._highs_constants, scipy.optimize._highs._highs_constants, scipy.linalg._interpolative, scipy.optimize._bglu_dense, scipy.optimize._lsap, scipy.optimize._direct, scipy.integrate._odepack, scipy.integrate._quadpack, scipy.integrate._vode, scipy.integrate._dop, scipy.integrate._lsoda, scipy.interpolate._fitpack, scipy.interpolate._dfitpack, scipy.interpolate._bspl, scipy.interpolate._ppoly, scipy.interpolate.interpnd, scipy.interpolate._rbfinterp_pythran, scipy.interpolate._rgi_cython, scipy.special.cython_special, scipy.stats._stats, scipy.stats._biasedurn, scipy.stats._levy_stable.levyst, scipy.stats._stats_pythran, scipy._lib._uarray._uarray, scipy.stats._ansari_swilk_statistics, scipy.stats._sobol, scipy.stats._qmc_cy, scipy.stats._mvn, scipy.stats._rcont.rcont, scipy.stats._unuran.unuran_wrapper, scipy.ndimage._nd_image, _ni_label, scipy.ndimage._ni_label, pandas._libs.tslibs.ccalendar, pandas._libs.tslibs.np_datetime, pandas._libs.tslibs.dtypes, pandas._libs.tslibs.base, pandas._libs.tslibs.nattype, pandas._libs.tslibs.timezones, pandas._libs.tslibs.fields, pandas._libs.tslibs.timedeltas, pandas._libs.tslibs.tzconversion, pandas._libs.tslibs.timestamps, pandas._libs.properties, pandas._libs.tslibs.offsets, pandas._libs.tslibs.strptime, pandas._libs.tslibs.parsing, pandas._libs.tslibs.conversion, pandas._libs.tslibs.period, pandas._libs.tslibs.vectorized, pandas._libs.ops_dispatch, pandas._libs.missing, pandas._libs.hashtable, pandas._libs.algos, pandas._libs.interval, pandas._libs.lib, pandas._libs.ops, pandas._libs.hashing, pandas._libs.arrays, pandas._libs.tslib, pandas._libs.sparse, pandas._libs.internals, pandas._libs.indexing, pandas._libs.index, pandas._libs.writers, pandas._libs.join, pandas._libs.window.aggregations, pandas._libs.window.indexers, pandas._libs.reshape, pandas._libs.groupby, pandas._libs.json, pandas._libs.parsers, pandas._libs.testing, sklearn.utils._isfinite, sklearn.utils.sparsefuncs_fast, sklearn.utils.murmurhash, sklearn.utils._openmp_helpers, sklearn.preprocessing._csr_polynomial_expansion, sklearn.preprocessing._target_encoder_fast, scipy.io.matlab._mio_utils, scipy.io.matlab._streams, scipy.io.matlab._mio5_utils, sklearn.datasets._svmlight_format_fast, sklearn.utils._random, sklearn.utils._vector_sentinel, sklearn.feature_extraction._hashing_fast, sklearn.metrics.cluster._expected_mutual_info_fast, sklearn.metrics._dist_metrics, sklearn.metrics._pairwise_distances_reduction._datasets_pair, sklearn.utils._cython_blas, sklearn.metrics._pairwise_distances_reduction._base, sklearn.metrics._pairwise_distances_reduction._middle_term_computer, sklearn.utils._heap, sklearn.utils._sorting, sklearn.metrics._pairwise_distances_reduction._argkmin, sklearn.metrics._pairwise_distances_reduction._argkmin_classmode, sklearn.metrics._pairwise_distances_reduction._radius_neighbors, sklearn.metrics._pairwise_distances_reduction._radius_neighbors_classmode, sklearn.metrics._pairwise_fast (total: 164)
/Users/runner/work/LightGBM/LightGBM/.ci/setup.sh: line 41:  8194 Abort trap: 6           pytest $GITHUB_WORKSPACE/tests/python_package_test

I'm not sure we ever checked even building LightGBM (not testlightgbm) with sanitizers.

Comment thread python-package/README.rst
--config-settings=cmake.define.USE_GPU=ON \
--config-settings=cmake.define.OpenCL_INCLUDE_DIR="/usr/local/cuda/include/" \
--config-settings=cmake.define.OpenCL_LIBRARY="/usr/local/cuda/lib64/libOpenCL.so"
pip install lightgbm --no-binary lightgbm --config-settings=cmake.define.USE_GPU=ON --config-settings=cmake.define.OpenCL_INCLUDE_DIR="/usr/local/cuda/include/" --config-settings=cmake.define.OpenCL_LIBRARY="/usr/local/cuda/lib64/libOpenCL.so"

@StrikerRUS StrikerRUS Jan 19, 2025 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We cannot predict which shell user is using. For example, to split long line in PowerShell, ` char should be used. So for better UX I think it better to provide universal copy-pastable commands.

@StrikerRUS
StrikerRUS marked this pull request as ready for review January 19, 2025 19:04
@StrikerRUS StrikerRUS changed the title [WIP][docs] update Python-package installation guide [docs] update Python-package installation guide Jan 19, 2025

@jameslamb jameslamb left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this! I left some suggestions for your consideration.

Comment thread python-package/README.rst Outdated

|

Use LightGBM with Matplotlib and Graphviz

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Use LightGBM with Matplotlib and Graphviz
Use LightGBM Plotting Capabilities

For the other blocks, "Use LightGBM with {library}" is a helpful title, because that's how users are likely to be thinking about it. e.g. "I have a Dask cluster, how do I use LightGBM with Dask".

For plotting, I don't think that's true. I suspect that for most users, they are not thinking "how do I use LightGBM with graphviz?" and instead are just interested in "how do I visualize the structure of my LightGBM models?".

In other words... the fact that these functions happen to use matplotlib and graphviz isn't central to their value.

Would you consider renaming this?

Comment thread python-package/README.rst

.. code:: sh

# in sh.exe, git bash, or other Unix-like shell

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh nice!

Comment thread python-package/README.rst
conda install -c conda-forge lightgbm

These are precompiled packages that are fast to install.
Use them instead of ``pip install`` if any of the following are true:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do think that the conda-forge packages offer a lot of benefits. But it's not as clear as one approach being "recommended" and the other being "not recommended".

Situations where conda-forge's lightgbm should be preferred:

Situations where pip install lightgbm should be preferred

  • you want to customize the build (e.g. use a different Boost version, build a variant like the MPI package, compile with debug symbols, etc.)
  • you want to (or are required to!) use pip for non-LightGBM reasons

All that said... I'm indifferent about the order. If you want to put conda before pip, that's fine. But at this point, I don't think we should say that one is "preferred" or "recommended".

Comment thread python-package/README.rst Resolved
Comment thread python-package/pyproject.toml Resolved
Comment thread build-python.sh Outdated
# OpenCL include directory.
# --opencl-library=FILEPATH
# Path to OpenCL library.
# --sanitizers=LIST_OF_SANITIZERS

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not excited about adding --sanitizers, --debug, and --nohomebrew to this script when no one has asked for them.

This build-python.sh script is already more complex than I'm comfortable with and is duplicating functionality that'd be better-handled by CMake.

Rather than trying to have a flag here for every option() in this project's CMake, I think we should just rely on documentation describing how to use --precompile to use a shared library built with whatever customizations you want.

I'd love to put our energy into instead working towards eliminating this script eventually with heavier use of CMake + all of the improvements that have gone into scikit-build-core over the last year (some of which I sort of describe in #6774).

What do you think?

Comment thread python-package/README.rst Outdated
Comment on lines +216 to +226
Build without Searching in Homebrew Folders for Dependencies on macOS
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. code:: sh

pip install lightgbm --no-binary lightgbm --config-settings=cmake.define.USE_HOMEBREW_FALLBACK=OFF

Use this option to stop looking into Homebrew standard folders for finding dependencies (e.g. OpenMP) during the build on macOS.

|

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Build without Searching in Homebrew Folders for Dependencies on macOS
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. code:: sh
pip install lightgbm --no-binary lightgbm --config-settings=cmake.define.USE_HOMEBREW_FALLBACK=OFF
Use this option to stop looking into Homebrew standard folders for finding dependencies (e.g. OpenMP) during the build on macOS.
|

I think we should omit this section. If you're using pip install (not pip wheel or python -m build) like this, then you're not building a package for redistribution... you're just installing one for runtime use on the same system.

The OpenMP found at build time will become the first RPATH entry in lib_lightgbm.dylib, and so it'll be found when you run import lightgbm.

As long as that library is still there later, it doesn't matter whether you later brew install libomp on the same system.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just do not think we need 1 paragraph-size section like this matching every option() in CMakeLists.txt. It creates duplication across the project and and adds to the size of the doc, which I think is intimidating for some users.

I think it is fine to only have specific sections for:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel the same way about other new sections below... do not think we need to have a separate section in this doc describing how to build the Python package with sanitizers or with USE_DEBUG enabled.

plotting = [
"graphviz",
"matplotlib"
]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree! Every optional dependency should have an extra. Totally support this.

Comment on lines +83 to +89
[tool.scikit-build.cmake]
version = "CMakeLists.txt"
build-type = "Release"

[tool.scikit-build.cmake.define]
__BUILD_FOR_PYTHON = "ON"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the benefit of moving these things down here? Are the equivalent fields in [tool.scikit-build] deprecated?

@StrikerRUS StrikerRUS Jan 26, 2025 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, these nested fields are identical, I simply don't know how to specify nested mapping tool.scikit-build.cmake.define in existing setup 😬

And I think that

[tool.scikit-build.cmake.define]
__BUILD_FOR_PYTHON = "ON"
some_new_option = "value"

is more readable than

[tool.scikit-build]
cmake.define.__BUILD_FOR_PYTHON = "ON"
cmake.define.some_new_option = "value"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with you about that, I'd expected that this form would work:

[tool.scikit-build]
cmake.define = [
  "__BUILD_FOR_PYTHON=ON",
  "some_new_option=value"
]

But when I search around on GitHub, I don't see any examples like that: https://github.com/search?q=%22cmake.define%22+language%3ATOML&type=code&p=1

Let's leave this as you have it in the PR, I like it.

@StrikerRUS StrikerRUS changed the title [docs] update Python-package installation guide [WIP][docs] update Python-package installation guide Feb 10, 2025
@StrikerRUS

Copy link
Copy Markdown
Collaborator Author

@jameslamb I think I addressed all your comments. Please give another look at this PR.

@StrikerRUS StrikerRUS changed the title [WIP][docs] update Python-package installation guide [docs] update Python-package installation guide Mar 2, 2025

@jameslamb jameslamb left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great to me! I think all of my initial comments have been addressed. Thank you very much for all of your hard work, especially for testing all of these different installation methods.

@StrikerRUS
StrikerRUS merged commit d18fff1 into master Apr 17, 2025
@StrikerRUS
StrikerRUS deleted the docs/install-py branch April 17, 2025 09:39
@github-actions

Copy link
Copy Markdown
Contributor

This pull request has been automatically locked since there has not been any recent activity since it was closed.
To start a new related discussion, open a new issue at https://github.com/lightgbm-org/LightGBM/issues including a reference to this.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Apr 23, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants