Skip to content

Commit 8fe9a52

Browse files
authored
Merge pull request #311 from ashwinvis/fix-packaging-2024
Fix packaging episode
2 parents 2846bc1 + c278949 commit 8fe9a52

File tree

2 files changed

+94
-28
lines changed

2 files changed

+94
-28
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,7 @@ help:
1818
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
1919
%: Makefile
2020
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
21+
22+
# Live reload site documents for local development
23+
livehtml:
24+
sphinx-autobuild "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

content/packaging.rst

Lines changed: 90 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -140,15 +140,17 @@ Sharing packages via PyPI
140140
Once we are able to pip-install the example package locally, we are ready for
141141
upload.
142142

143-
We exercise by uploading to `test-PyPI <https://test.pypi.org/>`__, not the
143+
We exercise by uploading to test-PyPI_, not the
144144
real `PyPI <https://pypi.org/>`__, so that if we mess things up, nothing bad
145145
happens.
146146

147147
We need two more things:
148148

149149
- We will do this using `Twine <https://twine.readthedocs.io/>`__ so you need
150150
to pip install that, too.
151-
- You need an account on `test-PyPI <https://test.pypi.org/>`__.
151+
- You need an account on test-PyPI_
152+
153+
.. _test-PyPI: https://test.pypi.org/
152154

153155
.. highlight:: console
154156

@@ -165,13 +167,64 @@ And use twine to upload the distribution files to test-PyPI::
165167
$ twine upload -r testpypi dist/*
166168

167169
Uploading distributions to https://test.pypi.org/legacy/
168-
Enter your username:
169-
Enter your password:
170+
Enter your API token:
171+
172+
173+
.. _Create API token: https://test.pypi.org/manage/account/token/
174+
175+
.. note::
176+
177+
To generate an API token, proceed to the `Create API token`_ page in test-PyPI.
178+
You will be prompted for your password.
179+
180+
.. solution:: The long-version for finding the *Create API token* page
181+
182+
1. Log on to test-PyPI_ at https://test.pypi.org
183+
2. In the top-right corner, click on the drop-down menu and click **Account settings** or
184+
follow this `link <https://test.pypi.org/manage/account/#api-tokens>`__.
185+
3. Scroll down to the section **API tokens** and click the button **Add API token**,
186+
which opens up the
187+
`Create API token`_ page.
188+
189+
190+
#. Under **Token name** write something memorable.
191+
It should remind you the *purpose* such as
192+
or the *name of the computer* so you can delete it later.
193+
#. Under **Scope** select ``Entire account (all projects)``.
194+
#. Click on **Create token**.
195+
#. Click on **Copy token** once a long string which starts
196+
with ``pypi-`` is generated.
170197

171-
Once this is done, create yet another virtual environment and try to install from test-PyPI (adapt "myname")::
198+
Paste that token back into terminal where ``twine upload ...`` is running and press ENTER.
172199

173-
$ pip install -i https://test.pypi.org/simple/ calculator-myname
200+
Once this is done, create yet another virtual environment and try to install from test-PyPI (adapt ``myname``).
174201

202+
.. tabs::
203+
204+
.. tab:: Linux / macOS
205+
206+
.. code-block:: console
207+
:emphasize-lines: 4-7
208+
209+
$ python3 -m venv venv-calculator
210+
$ source venv-calculator/bin/activate
211+
$ which python
212+
$ python3 -m pip install \
213+
-i https://test.pypi.org/simple/ \
214+
--extra-index-url https://pypi.org/simple/ \
215+
calculator-myname
216+
$ deactivate
217+
218+
.. tab:: Windows
219+
220+
.. code-block:: console
221+
:emphasize-lines: 4
222+
223+
$ python3 -m venv venv-calculator
224+
$ venv-calculator\Scripts\activate
225+
$ where python
226+
$ python3 -m pip install -i https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ calculator-myname
227+
$ deactivate
175228
176229
Tools that simplify sharing via PyPI
177230
------------------------------------
@@ -188,6 +241,8 @@ There are at least two tools which try to make the packaging and PyPI interactio
188241
- `Poetry <https://python-poetry.org/>`__
189242
- `Flit <https://flit.pypa.io/>`__
190243

244+
If you upload packages to PyPI or test PyPI often you can create an API token and
245+
`save it in the .pypirc file <https://packaging.python.org/en/latest/specifications/pypirc/#common-configurations>`__.
191246

192247
Building a conda package and share it
193248
-------------------------------------
@@ -199,42 +254,44 @@ Building a conda package and share it
199254

200255
.. callout:: Prerequisites
201256

202-
To create a conda package, `conda-build` package is required. You may install it with **Anaconda Navigator** or from the command line::
257+
To generate a conda build recipe, the package ``grayskull`` and
258+
to build it, the package ``conda-build`` are required.
259+
You may install these with **Anaconda Navigator** or from the command line::
203260

204-
$ conda install conda-build
261+
$ conda install -n base grayskull conda-build
205262

206263

207264
The simplest way for creating a conda package for your python script is to
208265
first publish it in `PyPI <https://pypi.org/>`__ following the steps explained
209266
above.
210267

211268

212-
Building a python package with conda skeleton pypi
213-
***************************************************
269+
Building a python package with grayskull and conda-build
270+
********************************************************
214271

215272
Once build, the conda package can be installed locally. For this example, we
216273
will use `runtest <https://pypi.org/project/runtest/>`__. `runtest
217274
<https://github.com/bast/runtest>`__ is a numerically tolerant end-to-end test
218275
library for research software.
219276

220-
1. Create pypi skeleton::
277+
1. Generate the *recipe* by executing (``grayskull`` or ``conda grayskull``)::
221278

222-
$ conda skeleton pypi runtest
279+
$ conda grayskull pypi runtest
223280

224-
The command above will create a new folder called `runtest` containing a file `meta.yaml`, the conda recipe for `runtest`.
281+
The command above will create a new folder called `runtest` containing a file `meta.yaml`,
282+
the conda recipe for building the `runtest` package.
225283

226-
2. Edit `meta.yaml` and update requirements:
284+
2. View the contents of `meta.yaml` and ensure requirements :
227285

228286
.. code-block:: yaml
229287
230288
requirements:
231289
host:
232-
- pip
233290
- python
234-
- flit
291+
- flit-core >=2,<4
292+
- pip
235293
run:
236294
- python
237-
- flit
238295
239296
In the requirements above, we specified what is required for the `host <https://docs.conda.io/projects/conda-build/en/latest/resources/define-metadata.html#host>`__ and for `running <https://docs.conda.io/projects/conda-build/en/latest/resources/define-metadata.html#run>`__ the package.
240297

@@ -248,7 +305,7 @@ library for research software.
248305

249306
Your package is now ready to be build with conda::
250307

251-
$ conda-build runtest
308+
$ conda build runtest
252309

253310

254311
.. callout:: Conda package location
@@ -257,11 +314,15 @@ library for research software.
257314

258315
.. code-block:: none
259316
260-
~/anaconda3/conda-bld/win-64/runtest-2.2.1-py38_0.tar.bz2
317+
/home/username/miniforge3/conda-bld/noarch/runtest-2.3.4-py_0.tar.bz2
261318
262-
The prefix `~/anaconda3/` may be different on your machine and depending on your operating system (Linux, Mac-OSX or Windows) the sub-folder `win-64` differs too (for instance `linux-64` on Linux machines).
319+
The prefix ``/home/username/miniforge3/`` may be different on your machine.
320+
depending on your operating system (Linux, Mac-OSX or Windows). The sub-folder is named ``noarch`` since
321+
it is a pure-python package and the recipe indicates the same.
263322

264-
The conda package we have created is specific to your platform (here `win-64`). It can be converted to other platforms using `conda convert <https://docs.conda.io/projects/conda-build/en/latest/user-guide/tutorials/build-pkgs.html#converting-a-package-for-use-on-all-platforms>`__.
323+
If package contained compiled code then the sub-folder would have been named ``win-64`` or ``linux-64``.
324+
It could then be converted to other platforms using
325+
`conda convert <https://docs.conda.io/projects/conda-build/en/latest/user-guide/tutorials/build-pkgs.html#converting-a-package-for-use-on-all-platforms>`__.
265326

266327
4. Check within new environment
267328

@@ -286,7 +347,10 @@ library for research software.
286347
287348
.. callout:: Building a conda package from scratch
288349

289-
It is possible to build a conda package from scratch without using conda skeleton. We recommend you to check the `conda-build documentation <https://docs.conda.io/projects/conda-build/en/latest/user-guide/tutorials/build-pkgs.html>`__ for more information.
350+
It is possible to build a conda package from scratch without using conda grayskull.
351+
We recommend you to check the
352+
`conda-build documentation <https://docs.conda.io/projects/conda-build/en/latest/user-guide/tutorials/build-pkgs.html>`__
353+
for more information.
290354

291355
To be able to share and install your local conda package anywhere (on other platforms), you would need to upload it to a `conda channel <https://docs.conda.io/projects/conda/en/latest/user-guide/concepts/channels.html>`__ (see below).
292356

@@ -295,15 +359,13 @@ To be able to share and install your local conda package anywhere (on other plat
295359
Publishing a python package
296360
***************************
297361

298-
- Upload your package to *Anaconda.org*: see instructions `here
299-
<https://docs.conda.io/projects/conda-build/en/latest/user-guide/tutorials/build-pkgs-skeleton.html#optional-uploading-packages-to-anaconda-org>`__.
300-
Please note that you will have to create an account on Anaconda.
301-
302362
- Upload your package to `conda-forge <https://conda-forge.org/>`__:
303363
conda-forge is a conda channel: it contains community-led collection of
304364
recipes, build infrastructure and distributions for the conda package
305-
manager. Anyone can public conda packages to conda-forge if certain
306-
`guidelines <https://conda-forge.org/docs/>`__ are respected.
365+
manager. Anyone can
366+
`publish conda packages to conda-forge <https://conda-forge.org/docs/maintainer/adding_pkgs/>`__
367+
if certain
368+
`guidelines <https://conda-forge.org/docs/maintainer/guidelines/>`__ are respected.
307369

308370
- Upload your package to `bioconda <https://bioconda.github.io/>`_: bioconda is
309371
a very popular channel for the conda package manager specializing in

0 commit comments

Comments
 (0)