You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: tutorials/publish-pypi.md
+56-47Lines changed: 56 additions & 47 deletions
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
3
3
:::{todo}
4
4
* emphasize that we recommended the trusted publisher GitHub action for most maintainers
5
-
* Make sure they add /dist to their .gitignore file. We have not discussed github workflows anywhere yet. Where does that fit?
5
+
* Make sure they add /dist to their .gitignore file. We have not discussed GitHub workflows anywhere yet. Where does that fit?
6
6
*https://hatch.pypa.io/latest/intro/#existing-project <- hatch will migrate from setup.py for you - if we go with hatch then we may want to add this to the installable code lesson
7
7
* Should we install hatch with pipx?
8
8
@@ -32,8 +32,8 @@ In this lesson you will learn how to:
32
32
33
33
You will do all of your development work in this lesson using [Hatch](https://hatch.pypa.io/latest/).
34
34
35
-
Once your package is on PyPI you can publish it to the conda-forge channel of conda
36
-
using [grayskull](https://conda.github.io/grayskull/).
35
+
Once your package is on PyPI you can publish it to conda-forge (which is a channel on conda)
36
+
using [Grayskull](https://conda.github.io/grayskull/).
37
37
38
38
You will learn how to publish to conda-forge in a future lesson.
39
39
@@ -64,25 +64,19 @@ The steps for publishing on test PyPI vs. real PyPI are the same with the
64
64
exception of a different url. Thus, in this lesson you will use test PyPI
65
65
to practice and learn.
66
66
67
-
:::{todo}
68
-
When this lesson is published -
69
-
in xx lesson, you will learn how to setup an automated release workflow on GitHub
70
-
using GitHub actions that will automate the PyPI publication process whenever
71
-
you create a new software release.
72
-
:::
73
-
74
-
75
67
## 4 Steps for publishing a Python package on PyPI
76
68
77
-
There are 4 things that you need to do to publish your Python package
69
+
In this lesson you will learn how to publish your package to PyPI
70
+
using [Hatch](https://hatch.pypa.io/latest/). There are 4 things that
71
+
you need to do to publish your Python package:
78
72
to PyPI. You need to:
79
73
80
-
1.**Create a package development environment.** You will do this using Hatch.
81
-
1.[**Build your package**](../package-structure-code/python-package-distribution-files-sdist-wheel). Building a package is the process of turning your code into 2 types of distribution files: sdist and wheel. The wheel distribution file is particularly important for users who will `pip install` your package.
74
+
1.**Create a package development environment**
75
+
1.[**Build your package using `hatch build`**](../package-structure-code/python-package-distribution-files-sdist-wheel). Building a package is the process of turning your code into two types of distribution files: sdist and wheel. The wheel distribution file is particularly important for users who will `pip install` your package.
82
76
1.**Create an account on (test) PyPI**: You will need to create a PyPI account and associated token which provides permissions for you to upload your package.
83
-
1.**Publish to PyPI using `hatch publish`**: Once you have completed the above two steps, you are ready to use `hatch` to publish your package!
77
+
1.**Publish to PyPI using `hatch publish`**
78
+
84
79
85
-
In this lesson you will learn how to publish your package to PyPI using [Hatch](https://hatch.pypa.io/latest/).
86
80
In a future lesson, you will learn how to create an automated
87
81
GitHub action workflow that publishes an updated
88
82
version of your package to PyPI every time you create a GitHub release.
@@ -129,7 +123,7 @@ Hatch environment, it will automatically install your package into the environme
# Notice here you're in the (pyospackage) environment which is the default
150
+
pyosPackage (☊ main) [✎ ×1 ] is 📦 v0.1.4 via 🐍 pyenv (pyospackage)
151
+
➜ exit
152
+
153
+
pyosPackage (☊ main) [✎ ×1 ] is 📦 v0.1.4 via 🐍 pyenv took 43s
154
+
➜
155
+
```
156
+
151
157
152
158
### Hatch and environments
153
159
154
160
Behind the scenes when hatch creates a new virtual environment,
155
-
by default it uses venv[^venv].
161
+
by default it uses venv[^venv] which is the default environment management tool that comes with Python installations.
156
162
157
-
hatch will:
158
-
1. Create a new virtualenv (venv) that is located on your computer. You can customize the location of this environment if you wish ....<where??>
163
+
Hatch will:
164
+
1. Create a new virtualenv (venv) that is located on your computer.
159
165
2. Install your package into the environment in editable mode (similar to `pip install -e`). This means it installs both your project and your project's dependencies as declared in your pyproject.toml file.
160
166
161
167
## Step 2: Build your package's sdist and wheel distributions
162
168
163
-
Once you have your development environment setup, you are ready to build your package using Hatch. Remember that building is the process of turning your Python package files into two distribution files:
169
+
Once you have your development environment setup, you are ready to build your package using Hatch. Remember that building is the process of turning your Python package file structure into two distribution files:
164
170
165
171
1. The [wheel distribution](#python-wheel) is a pre-built version of your package. It useful for users as it can be directly installed using a tool such as `pip`. This file has the extension `.whl`.
166
-
2. The [source distribution](#python-source-distribution)is the files that make up your package in an unbuilt format. This file will have the extension `.tar.gz`.
172
+
2. The [source distribution](#python-source-distribution)contains the files that make up your package in an unbuilt format. This file will have the extension `.tar.gz`.
167
173
168
174
You will use Hatch as a **Front end** tool that builds
169
175
your package's sdist and wheel using the [hatchling](https://hatch.pypa.io/latest/) build back-end.
170
176
The hatchling build back-end is used because you declared it in your pyproject.toml file in the [previous lesson](1-installable-code).
### <iclass="fa-solid fa-wand-magic-sparkles"></i> Congratulations - you've created your Python package distribution files <iclass="fa-solid fa-wand-magic-sparkles"></i>
203
209
204
210
You've now built your Python package and created your package distribution files. The next step is to setup
205
-
your account on PyPI so you can publish your package.
211
+
your account on testPyPI so you can publish your package.
206
212
207
213
## Step 3. Setup your test PyPI account
208
214
209
-
Next, you'll setup an account on test PyPI. Remember that you
210
-
are using test PyPI here instead of the real PyPI as a way to
215
+
Next, you'll setup an account on Test PyPI. Remember that you
216
+
are using test PyPI here instead of the PyPI as a way to
211
217
safely learn how to publish a package without stressing the
212
218
real PyPI's servers.
213
219
214
-
:::{admonition} Test vs. real PyPI
215
-
If you have a package that you are confident belongs on the real PyPI, all of the steps below will also work for you if you replace test.pypi.org with pypi.org wherever it appears.
220
+
:::{admonition} Test PyPI vs. PyPI
221
+
If you have a package that you are confident belongs on PyPI, all of the steps below will also work for you. When you publish using Hatch, you will call `hatch publish` to publish directly to PyPI instead of `hatch publish -r test` which publishes to Test PyPI.
216
222
:::
217
223
218
224
1.[Open up a web browser and go to the test PyPI website](https://test.pypi.org/).
219
225
2.[Create an account](https://test.pypi.org/account/register/) if you don't already have one. Be sure to store your password in a safe place!
220
226
3. Once you have an account setup, login to it.
221
-
4. Search on [https://test.pypi.org/](https://test.pypi.org/)(or pypi.org) to ensure that the package name that you have selected doesn't already exist. If you are using our test pyosPackage, then we suggest that you add your name or GitHub username to the end of the package name to ensure it's unique.
227
+
4. Search on [https://test.pypi.org/](https://test.pypi.org/) to ensure that the package name that you have selected doesn't already exist. If you are using our test pyosPackage, then we suggest that you add your name or GitHub username to the end of the package name to ensure it's unique.
222
228
223
229
Example: `pyosPackage_yourNameHere`.
224
230
@@ -244,8 +250,8 @@ use a backup device that only you can access to validate that the person logging
244
250
245
251
This matters on PyPI because someone could login to your account and upload a version of your package that has security issues. These issues will then impact all of your users when they download and install that version of the package.
246
252
247
-
While you don't have to setup 2-factor authentication, we strongly
248
-
suggest that you do so.
253
+
2-factor authentication is required for PyPI authentication
254
+
as of 1 January 2024.
249
255
:::
250
256
251
257
## Step 4. Create a package upload token
@@ -269,24 +275,23 @@ It's ideal to create a package-specific token. When you create an account wide t
269
275
* When you create your token, be sure to copy the token value and store it in a secure place before closing that browser.
270
276
271
277
278
+
Your token should look something like this:
272
279
280
+
`pypi-abunchofrandomcharactershere...`
281
+
282
+
It should start with `pypi` followed by a dash and a bunch of characters.
273
283
274
284
### Upload to PyPI using Hatch
275
285
276
-
Once you have the token in a safe place, you are ready to publish to
286
+
Once you have your token, you are ready to publish to
277
287
PyPI.
278
288
289
+
* Run `hatch publish -r test`
279
290
280
-
Your token should look something like this:
281
-
282
-
`pypi-abunchofrandomcharactershere...`
283
-
284
-
1. Finally run `hatch publish -r test`
285
-
286
-
-r stands for repository. In this case because you are publishing to test-PyPI you will use `-r test`. Hatch will then ask for a username and credentials.
291
+
`-r` stands for repository. In this case because you are publishing to test-PyPI you will use `-r test`. Hatch will then ask for a username and credentials.
287
292
288
-
* Add the word `__token__` for your username.
289
-
* Paste your PyPI token value in for the credential values.
293
+
* Add the word `__token__` for your username. This tells Test PyPI that you are using a token value rather than a username.
294
+
* Paste your PyPI token value in at the `Enter your credentials` prompt:
If your credentials are valid, Hatch will publish your package to test PyPI.
305
+
If your credentials are valid, and you have already run `hatch build`
306
+
and thus have your 2 distribution files in a `dist/` directory then
307
+
Hatch will publish your package to test PyPI.
301
308
302
-
Hatch also has a caching system so once you enter your credentials it will remember them.
309
+
Hatch also has a caching system so once you enter your credentials it
310
+
will remember them.
303
311
304
312
## Install your package from test PyPI
305
313
@@ -308,13 +316,15 @@ test PYPI. You can find the installation instructions on the test PyPI
308
316
landing page for your newly uploaded package.
309
317
310
318
:::{figure-md} testpypi-landing-page
311
-
<imgsrc="../images/tutorials/test-pypi-package.png"alt="A screenshot of the test PyPI page for pyosPackage. It says pyosPackage 0.1.0 at the top with the pip install instructions below. The landing page of the package has information from the package's readme file. "width="700px">
319
+
<imgsrc="../images/tutorials/test-pypi-package.png"alt="A screenshot of the test PyPI page for pyosPackage. It says pyosPackage 0.1.0 at the top with the pip install instructions below. The landing page of the package has information from the package's README file. "width="700px">
312
320
313
321
This is an example landing page for the pyosPackage that was just uploaded. Notice at the top of the page there is instruction for how to install the package from test PyPI. You can simply copy that code and use it to install your package from testPyPi locally.
314
322
:::
315
323
316
324
317
-
As an example, [check out our pyOpenSci pyosPackage landing page on test PyPI](https://test.pypi.org/project/pyosPackage/). Notice the page has information about the current package version and also installation instructions as follows:
325
+
As an example, [check out our pyOpenSci pyosPackage landing page on test PyPI](https://test.pypi.org/project/pyosPackage/). Notice that
326
+
the page has information about the current package version and also
0 commit comments