Skip to content

Commit c74db0a

Browse files
committed
Fix: cleanup edits from review
1 parent b3e6a42 commit c74db0a

File tree

1 file changed

+56
-47
lines changed

1 file changed

+56
-47
lines changed

tutorials/publish-pypi.md

Lines changed: 56 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
:::{todo}
44
* 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?
66
* 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
77
* Should we install hatch with pipx?
88

@@ -32,8 +32,8 @@ In this lesson you will learn how to:
3232

3333
You will do all of your development work in this lesson using [Hatch](https://hatch.pypa.io/latest/).
3434

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/).
3737

3838
You will learn how to publish to conda-forge in a future lesson.
3939

@@ -64,25 +64,19 @@ The steps for publishing on test PyPI vs. real PyPI are the same with the
6464
exception of a different url. Thus, in this lesson you will use test PyPI
6565
to practice and learn.
6666

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-
7567
## 4 Steps for publishing a Python package on PyPI
7668

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:
7872
to PyPI. You need to:
7973

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.
8276
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+
8479

85-
In this lesson you will learn how to publish your package to PyPI using [Hatch](https://hatch.pypa.io/latest/).
8680
In a future lesson, you will learn how to create an automated
8781
GitHub action workflow that publishes an updated
8882
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
129123
source "/Path/to/env/here/hatch/env/virtual/pyosPackage/Mk7F5Y0T/sphinx-2i2c-theme/bin/activate"
130124
```
131125

132-
View what's in the environment:
126+
View what's in the environment using `pip list`:
133127

134128
```bash
135129
➜ pip list
@@ -148,28 +142,40 @@ tzdata 2023.4
148142

149143
At any time you can exit the environment using `exit`.
150144

145+
```bash
146+
➜ hatch shell
147+
source "/Users/leahawasser/Library/Application Support/hatch/env/virtual/pyospackage/twO2iQR3/pyospackage/bin/activate"
148+
149+
# 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+
151157

152158
### Hatch and environments
153159

154160
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.
156162

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.
159165
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.
160166

161167
## Step 2: Build your package's sdist and wheel distributions
162168

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:
164170

165171
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`.
167173

168174
You will use Hatch as a **Front end** tool that builds
169175
your package's sdist and wheel using the [hatchling](https://hatch.pypa.io/latest/) build back-end.
170176
The hatchling build back-end is used because you declared it in your pyproject.toml file in the [previous lesson](1-installable-code).
171177

172-
To build your package run:
178+
To build your package run `hatch build`:
173179

174180
```bash
175181
➜ hatch build
@@ -202,23 +208,23 @@ dist/pyospackage-0.1.0-py3-none-any.whl
202208
### <i class="fa-solid fa-wand-magic-sparkles"></i> Congratulations - you've created your Python package distribution files <i class="fa-solid fa-wand-magic-sparkles"></i>
203209

204210
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.
206212

207213
## Step 3. Setup your test PyPI account
208214

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
211217
safely learn how to publish a package without stressing the
212218
real PyPI's servers.
213219

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.
216222
:::
217223

218224
1. [Open up a web browser and go to the test PyPI website](https://test.pypi.org/).
219225
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!
220226
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.
222228

223229
Example: `pyosPackage_yourNameHere`.
224230

@@ -244,8 +250,8 @@ use a backup device that only you can access to validate that the person logging
244250

245251
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.
246252

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.
249255
:::
250256

251257
## 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
269275
* When you create your token, be sure to copy the token value and store it in a secure place before closing that browser.
270276

271277

278+
Your token should look something like this:
272279

280+
`pypi-abunchofrandomcharactershere...`
281+
282+
It should start with `pypi` followed by a dash and a bunch of characters.
273283

274284
### Upload to PyPI using Hatch
275285

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
277287
PyPI.
278288

289+
* Run `hatch publish -r test`
279290

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.
287292

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:
290295

291296
```bash
292297
❯ hatch publish -r test
@@ -297,9 +302,12 @@ dist/pyospackage-0.1.0.tar.gz ... already exists
297302

298303
```
299304

300-
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.
301308

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.
303311

304312
## Install your package from test PyPI
305313

@@ -308,13 +316,15 @@ test PYPI. You can find the installation instructions on the test PyPI
308316
landing page for your newly uploaded package.
309317

310318
:::{figure-md} testpypi-landing-page
311-
<img src="../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+
<img src="../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">
312320

313321
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.
314322
:::
315323

316324

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
327+
installation instructions as follows:
318328

319329
`pip install -i https://test.pypi.org/simple/ pyosPackage`
320330

@@ -379,8 +389,7 @@ To do this:
379389
2. Click on the manage button for the project that you wish to add a token for
380390
3. Go to settings
381391
4. Click on "Create a token for your-package-name-here"
382-
5. Create the token and follow the steps above to open up the `.pypirc` file
383-
6. Replace the old account wide token with your new package token.
392+
5. Create the token and follow the steps above publish your package using the repository specific token.
384393

385394
And you're all done!
386395

0 commit comments

Comments
 (0)