File: development_setup.rst

package info (click to toggle)
scikit-learn 1.8.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 26,132 kB
  • sloc: python: 224,867; cpp: 5,790; ansic: 846; makefile: 190; javascript: 179
file content (404 lines) | stat: -rw-r--r-- 13,475 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
.. _setup_development_environment:

Set up your development environment
-----------------------------------

.. _git_repo:

Fork the scikit-learn repository
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
First, you need to `create an account <https://github.com/join>`_ on
GitHub (if you do not already have one) and fork the `project repository
<https://github.com/scikit-learn/scikit-learn>`__ by clicking on the 'Fork'
button near the top of the page. This creates a copy of the code under your
account on the GitHub user account. For more details on how to fork a
repository see `this guide <https://help.github.com/articles/fork-a-repo/>`_.

The following steps explain how to set up a local clone of your forked git repository
and how to locally install scikit-learn according to your operating system.

Set up a local clone of your fork
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Clone your fork of the scikit-learn repo from your GitHub account to your
local disk:

.. prompt::

  git clone https://github.com/YourLogin/scikit-learn.git  # add --depth 1 if your connection is slow

and change into that directory:

.. prompt::

  cd scikit-learn

.. _upstream:

Next, add the ``upstream`` remote. This saves a reference to the main
scikit-learn repository, which you can use to keep your repository
synchronized with the latest changes (you'll need this later in the :ref:`development_workflow`):

.. prompt::

  git remote add upstream https://github.com/scikit-learn/scikit-learn.git

Check that the `upstream` and `origin` remote aliases are configured correctly
by running:

.. prompt::

  git remote -v

This should display:

.. code-block:: text

  origin    https://github.com/YourLogin/scikit-learn.git (fetch)
  origin    https://github.com/YourLogin/scikit-learn.git (push)
  upstream  https://github.com/scikit-learn/scikit-learn.git (fetch)
  upstream  https://github.com/scikit-learn/scikit-learn.git (push)


Set up a dedicated environment and install dependencies
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
..
   TODO Add |PythonMinVersion| to min_dependency_substitutions.rst one day.
   Probably would need to change a bit sklearn/_min_dependencies.py since Python is not really a package ...
.. |PythonMinVersion| replace:: 3.11

Using an isolated environment such as venv_ or conda_ makes it possible to
install a specific version of scikit-learn with pip or conda and its dependencies,
independently of any previously installed Python packages, which will avoid potential
conflicts with other packages.

In addition to the required Python dependencies, you need to have a working C/C++
compiler with OpenMP_ support to build scikit-learn `cython <https://cython.org>`__ extensions.
The platform-specific instructions below describe how to set up a suitable compiler and install
the required packages.

.. raw:: html

  <style>
    /* Show caption on large screens */
    @media screen and (min-width: 960px) {
      .install-instructions .sd-tab-set {
        --tab-caption-width: 20%;
      }

      .install-instructions .sd-tab-set.tabs-os::before {
        content: "Operating System";
      }

      .install-instructions .sd-tab-set.tabs-package-manager::before {
        content: "Package Manager";
      }
    }
  </style>

.. div:: install-instructions

  .. tab-set::
    :class: tabs-os

    .. tab-item:: Windows
      :class-label: tab-4

      .. tab-set::
        :class: tabs-package-manager

        .. tab-item:: conda
          :class-label: tab-6
          :sync: package-manager-conda

          First, you need to install a compiler with OpenMP_ support.
          Download the `Build Tools for Visual Studio installer <https://aka.ms/vs/17/release/vs_buildtools.exe>`_
          and run the downloaded `vs_buildtools.exe` file. During the installation you will
          need to make sure you select "Desktop development with C++", similarly to this
          screenshot:

          .. image::
            ../images/visual-studio-build-tools-selection.png

          Next, Download and install `the conda-forge installer`_ (Miniforge)
          for your system. Conda-forge provides a conda-based distribution of
          Python and the most popular scientific libraries.
          Open the downloaded "Miniforge Prompt" and create a new conda environment with
          the required python packages:

          .. prompt::

            conda create -n sklearn-dev -c conda-forge ^
              python numpy scipy cython meson-python ninja ^
              pytest pytest-cov ruff==0.11.2 mypy numpydoc ^
              joblib threadpoolctl pre-commit

          Activate the newly created conda environment:

          .. prompt::

            conda activate sklearn-dev

        .. tab-item:: pip
          :class-label: tab-6
          :sync: package-manager-pip

          First, you need to install a compiler with OpenMP_ support.
          Download the `Build Tools for Visual Studio installer <https://aka.ms/vs/17/release/vs_buildtools.exe>`_
          and run the downloaded `vs_buildtools.exe` file. During the installation you will
          need to make sure you select "Desktop development with C++", similarly to this
          screenshot:

          .. image::
            ../images/visual-studio-build-tools-selection.png

          Next, install the 64-bit version of Python (|PythonMinVersion| or later), for instance from the
          `official website <https://www.python.org/downloads/windows/>`__.

          Now create a virtual environment (venv_) and install the required python packages:

          .. prompt::

            python -m venv sklearn-dev

          .. prompt::

            sklearn-dev\Scripts\activate  # activate

          .. prompt::

            pip install wheel numpy scipy cython meson-python ninja ^
              pytest pytest-cov ruff==0.11.2 mypy numpydoc ^
              joblib threadpoolctl pre-commit


    .. tab-item:: MacOS
      :class-label: tab-4

      .. tab-set::
        :class: tabs-package-manager

        .. tab-item:: conda
          :class-label: tab-6
          :sync: package-manager-conda

          The default C compiler on macOS does not directly support OpenMP. To enable the
          installation of the ``compilers`` meta-package from the conda-forge channel,
          which provides OpenMP-enabled C/C++ compilers based on the LLVM toolchain,
          you first need to install the macOS command line tools:

          .. prompt::

            xcode-select --install

          Next, download and install `the conda-forge installer`_ (Miniforge) for your system.
          Conda-forge provides a conda-based distribution of
          Python and the most popular scientific libraries.
          Create a new conda environment with the required python packages:

          .. prompt::

            conda create -n sklearn-dev -c conda-forge python \
              numpy scipy cython meson-python ninja \
              pytest pytest-cov ruff==0.11.2 mypy numpydoc \
              joblib threadpoolctl compilers llvm-openmp pre-commit

          and activate the newly created conda environment:

          .. prompt::

            conda activate sklearn-dev

        .. tab-item:: pip
          :class-label: tab-6
          :sync: package-manager-pip

          The default C compiler on macOS does not directly support OpenMP, so you first need
          to enable OpenMP support.

          Install the macOS command line tools:

          .. prompt::

            xcode-select --install

          Next, install the LLVM OpenMP library with Homebrew_:

          .. prompt::

            brew install libomp

          Install a recent version of Python (|PythonMinVersion| or later) using Homebrew_
          (`brew install python`) or by manually installing the package from the
          `official website <https://www.python.org/downloads/macos/>`__.

          Now create a virtual environment (venv_) and install the required python packages:

          .. prompt::

            python -m venv sklearn-dev

          .. prompt::

            source sklearn-dev/bin/activate  # activate

          .. prompt::

            pip install wheel numpy scipy cython meson-python ninja \
              pytest pytest-cov ruff==0.11.2 mypy numpydoc \
              joblib threadpoolctl pre-commit

    .. tab-item:: Linux
      :class-label: tab-4

      .. tab-set::
        :class: tabs-package-manager

        .. tab-item:: conda
          :class-label: tab-6
          :sync: package-manager-conda

          Download and install `the conda-forge installer`_ (Miniforge) for your system.
          Conda-forge provides a conda-based distribution of Python and the most
          popular scientific libraries.
          Create a new conda environment with the required python packages
          (including `compilers` for a working C/C++ compiler with OpenMP support):

          .. prompt::

            conda create -n sklearn-dev -c conda-forge python \
              numpy scipy cython meson-python ninja \
              pytest pytest-cov ruff==0.11.2 mypy numpydoc \
              joblib threadpoolctl compilers pre-commit

          and activate the newly created environment:

          .. prompt::

            conda activate sklearn-dev

        .. tab-item:: pip
          :class-label: tab-6
          :sync: package-manager-pip

          To check your installed Python version, run:

          .. prompt::

            python3 --version

          If you don't have Python |PythonMinVersion| or later, please install `python3`
          from your distribution's package manager.

          Next, you need to install the build dependencies, specifically a C/C++
          compiler with OpenMP support for your system. Here you find the commands for
          the most widely used distributions:

          * On debian-based distributions (e.g., Ubuntu), the compiler is included in
            the `build-essential` package, and you also need the Python header files:

            .. prompt::

              sudo apt-get install build-essential python3-dev

          * On redhat-based distributions (e.g. CentOS), install `gcc`` for C and C++,
            as well as the Python header files:

            .. prompt::

              sudo yum -y install gcc gcc-c++ python3-devel

          * On Arche Linux, the Python header files are already included in the python
            installation, and `gcc`` includes the required compilers for C and C++:

            .. prompt::

              sudo pacman -S gcc

          Now create a virtual environment (venv_) and install the required python packages:

          .. prompt::

            python -m venv sklearn-dev

          .. prompt::

            source sklearn-dev/bin/activate  # activate

          .. prompt::

            pip install wheel numpy scipy cython meson-python ninja \
              pytest pytest-cov ruff==0.11.2 mypy numpydoc \
              joblib threadpoolctl pre-commit


.. _install_from_source:

Install editable version of scikit-learn
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Make sure you are in the `scikit-learn` directory
and your venv or conda `sklearn-dev` environment is activated.
You can now install an editable version of scikit-learn with `pip`:

.. prompt::

  pip install --editable . --verbose --no-build-isolation --config-settings editable-verbose=true

.. dropdown:: Note on `--config-settings`

  `--config-settings editable-verbose=true` is optional but recommended
  to avoid surprises when you import `sklearn`. `meson-python` implements
  editable installs by rebuilding `sklearn` when executing `import sklearn`.
  With the recommended setting you will see a message when this happens,
  rather than potentially waiting without feedback and wondering
  what is taking so long. Bonus: this means you only have to run the `pip
  install` command once, `sklearn` will automatically be rebuilt when
  importing `sklearn`.

  Note that `--config-settings` is only supported in `pip` version 23.1 or
  later. To upgrade `pip` to a compatible version, run `pip install -U pip`.

To check your installation, make sure that the installed scikit-learn has a
version number ending with `.dev0`:

.. prompt::

  python -c "import sklearn; sklearn.show_versions()"

You should now have a working installation of scikit-learn and your git repository
properly configured.

It can be useful to run the tests now (even though it will take some time)
to verify your installation and to be aware of warnings and errors that are not
related to you contribution:

.. prompt::

  pytest

For more information on testing, see also the :ref:`pr_checklist`
and :ref:`pytest_tips`.

.. _pre_commit:

Set up pre-commit
^^^^^^^^^^^^^^^^^

Additionally, install the `pre-commit hooks <https://pre-commit.com>`__, which will
automatically check your code for linting problems before each commit in the
:ref:`development_workflow`:

.. prompt::

  pre-commit install

.. _OpenMP: https://en.wikipedia.org/wiki/OpenMP
.. _meson-python: https://mesonbuild.com/meson-python
.. _Ninja: https://ninja-build.org/
.. _NumPy: https://numpy.org
.. _SciPy: https://www.scipy.org
.. _Homebrew: https://brew.sh
.. _venv: https://docs.python.org/3/tutorial/venv.html
.. _conda: https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html
.. _the conda-forge installer: https://conda-forge.org/download/

.. END Set up your development environment