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
+41-27Lines changed: 41 additions & 27 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -66,15 +66,17 @@ to practice and learn.
66
66
67
67
## 4 Steps for publishing a Python package on PyPI
68
68
69
-
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:
70
72
to PyPI. You need to:
71
73
72
-
1.**Create a package development environment.** You will do this using Hatch.
73
-
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 2 types of distribution files: sdist and wheel. The wheel distribution file is particularly important for users who will `pip install` your package.
74
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.
75
-
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
+
76
79
77
-
In this lesson you will learn how to publish your package to PyPI using [Hatch](https://hatch.pypa.io/latest/).
78
80
In a future lesson, you will learn how to create an automated
79
81
GitHub action workflow that publishes an updated
80
82
version of your package to PyPI every time you create a GitHub release.
@@ -121,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
+
143
157
144
158
### Hatch and environments
145
159
146
160
Behind the scenes when hatch creates a new virtual environment,
147
-
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.
148
162
149
-
hatch will:
150
-
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.
151
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.
152
166
153
167
## Step 2: Build your package's sdist and wheel distributions
154
168
155
-
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:
156
170
157
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`.
158
172
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`.
@@ -161,7 +175,7 @@ You will use Hatch as a **Front end** tool that builds
161
175
your package's sdist and wheel using the [hatchling](https://hatch.pypa.io/latest/) build back-end.
162
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>
195
209
196
210
You've now built your Python package and created your package distribution files. The next step is to setup
197
-
your account on PyPI so you can publish your package.
211
+
your account on testPyPI so you can publish your package.
198
212
199
213
## Step 3. Setup your test PyPI account
200
214
201
-
Next, you'll setup an account on test PyPI. Remember that you
202
-
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
203
217
safely learn how to publish a package without stressing the
204
218
real PyPI's servers.
205
219
206
-
:::{admonition} Test vs. real PyPI
207
-
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.
208
222
:::
209
223
210
224
1.[Open up a web browser and go to the test PyPI website](https://test.pypi.org/).
@@ -236,7 +250,8 @@ use a backup device that only you can access to validate that the person logging
236
250
237
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.
238
252
239
-
2-factor authentication is now required for authentication on PyPI.
253
+
2-factor authentication is required for PyPI authentication
254
+
as of 1 January 2024.
240
255
:::
241
256
242
257
## Step 4. Create a package upload token
@@ -260,24 +275,23 @@ It's ideal to create a package-specific token. When you create an account wide t
260
275
* When you create your token, be sure to copy the token value and store it in a secure place before closing that browser.
261
276
262
277
278
+
Your token should look something like this:
279
+
280
+
`pypi-abunchofrandomcharactershere...`
263
281
282
+
It should start with `pypi` followed by a dash and a bunch of characters.
264
283
265
284
### Upload to PyPI using Hatch
266
285
267
-
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
268
287
PyPI.
269
288
289
+
* Run `hatch publish -r test`
270
290
271
-
Your token should look something like this:
272
-
273
-
`pypi-abunchofrandomcharactershere...`
274
-
275
-
1. Finally run `hatch publish -r test`
276
-
277
-
-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.
278
292
279
-
* Add the word `__token__` for your username.
280
-
* 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:
0 commit comments