Skip to content

Commit 21a1782

Browse files
committed
Test PyPI API token and virtual environment instructions
1 parent 030a3a5 commit 21a1782

File tree

1 file changed

+61
-6
lines changed

1 file changed

+61
-6
lines changed

content/packaging.rst

Lines changed: 61 additions & 6 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.
197+
198+
Paste that token back into terminal where ``twine upload ...`` is running and press ENTER.
199+
200+
Once this is done, create yet another virtual environment and try to install from test-PyPI (adapt ``myname``).
201+
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
170217
171-
Once this is done, create yet another virtual environment and try to install from test-PyPI (adapt "myname")::
218+
.. tab:: Windows
172219

173-
$ pip install -i https://test.pypi.org/simple/ calculator-myname
220+
.. code-block:: console
221+
:emphasize-lines: 4
174222
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
-------------------------------------

0 commit comments

Comments
 (0)