Skip to content

Commit

Permalink
Packaging good enough
Browse files Browse the repository at this point in the history
  • Loading branch information
richelbilderbeek committed Nov 22, 2024
1 parent e629406 commit a28a76d
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 6 deletions.
94 changes: 93 additions & 1 deletion docs/package/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ tags:
- Understand how to package a Python project
- Package our Python project locally
- Run a script with out Python project
- Upload our package to PyPI

???- question "For teachers"

Expand Down Expand Up @@ -45,12 +46,16 @@ tags:

## Why?

![bacsim on PyPI](bacsim_on_testpypi.png)

> `bacsim` on (the test version of) PyPI
You want to share or re-use your work
in the regular way.

## Exercises

### Exercise 1: Install and use a Python package
### Exercise 1: install package locally

- Scan the
['Python Packaging User Guide' section on 'Packaging Python Projects'](https://packaging.python.org/en/latest/tutorials/packaging-projects/)
Expand Down Expand Up @@ -142,3 +147,90 @@ in the regular way.
If you get an error such as `Cannot find file 'parameters.csv'`
it means that our package is installed successfully.
Sure, it does not do actual work, but it is installed!


### Exercise 2: upload package to PyPI

#### Exercise 2.1: Build the package distribution files

Follow the steps at [here](https://packaging.python.org/en/latest/tutorials/packaging-projects/):

Install `build`:

```bash
python3 -m pip install --upgrade build --break-system-packages
```

Running `build`:

```bash
python3 -m build
```

???- question "How does that look like?"

```text
richel@richel-N141CU:~/GitHubs/programming_formalisms_project_autumn_2024$ python3 -m build
* Creating isolated environment: venv+pip...
* Installing packages in isolated environment:
- hatchling
* Getting build dependencies for sdist...
* Building sdist...
* Building wheel from sdist
* Creating isolated environment: venv+pip...
* Installing packages in isolated environment:
- hatchling
* Getting build dependencies for wheel...
* Building wheel...
Successfully built bacsim-1.0.1.tar.gz and bacsim-1.0.1-py3-none-any.whl
```

Now there will be files in the `dist` folder:

???- question "How does that look like?"

```bash
richel@richel-N141CU:~/GitHubs/programming_formalisms_project_autumn_2024$ ls dist/
bacsim-1.0.1-py3-none-any.whl bacsim-1.0.1.tar.gz
```

Well done, you've just created the package distribution files!

#### Exercise 2.2: Upload our package distribution

Last step is to upload our package distribution files
to [https://test.pypi.org](https://test.pypi.org):

Install `twine`:

```bash
python3 -m pip install --upgrade twine --break-system-packages
```

After registering to [ https://test.pypi.org]( https://test.pypi.org), you

Check failure on line 210 in docs/package/README.md

View workflow job for this annotation

GitHub Actions / check_markdown

Spaces inside link text [Context: "[ https://test.pypi.org]"]
have the API token be allowed to upload to testPyPI. Upload:

```bash
python3 -m twine upload --repository testpypi dist/*
```

???- question "How does that look like?"

```bash
richel@richel-N141CU:~/GitHubs/programming_formalisms_project_autumn_2024$ python3 -m twine upload --repository testpypi dist/*
Uploading distributions to https://test.pypi.org/legacy/
Enter your API token:
Uploading bacsim-1.0.1-py3-none-any.whl
100% ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 24.8/24.8 kB • 00:00 • 72.3 MB/s
Uploading bacsim-1.0.1.tar.gz
100% ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.6/1.6 MB • 00:00 • 8.5 MB/s

View at:
https://test.pypi.org/project/bacsim/1.0.1/
```

Indeed, the package can now be found at [https://test.pypi.org/project/bacsim/1.0.1/](https://test.pypi.org/project/bacsim/1.0.1/).

???- question "How does that look like?"

![bacsim on PyPI](bacsim_on_testpypi.png)
Binary file added docs/package/bacsim_on_testpypi.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 8 additions & 5 deletions lesson_plans/2024_autumn/20241122_richel.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@ After lunch:

So 4 things before 8:30, let's plan:

6:15-6:45 Implement the project as a package 4/10
6:45-7:15 Optimisation misconceptions 3/10
7:15-7:45 Big O 3/10
7:45-8:15 Runtime speed profiles 3/10
8:15-8:30 check pages
- [x] 6:15-6:45 Implement the project as a package 6/10

I want to add to upload our package to test PyPi, but no time.

- [ ] 6:45-7:15 Optimisation misconceptions 3/10
- [ ] 7:15-7:45 Big O 3/10
- [ ] 7:45-8:15 Runtime speed profiles 3/10
- [ ] 8:15-8:30 check pages



0 comments on commit a28a76d

Please sign in to comment.