---
# System prepended metadata

title: Python Packaging and Deployment

---

# Python Packaging and Deployment

This is HackMD - it's a collaborative note taking tool

Access the note here: https://hackmd.io/@energy/Syt1Ima49/edit

Rough agenda

1. Definitions
2. Anatomy of a Python package
3. Doing packaging
4. Tools to help with packaging
5. Distribution/Deployment

## What is packaging and deployment?

- Packaging - getting your code ready in a usable state, adding metadata etc.
- Deployment - getting the code into production (e.g. a web server)
- Distribution - publishing the code so others can install it
- Releasing - tagging a specific version of the code (use semantic versioning)

### It's confusing

- distutils is bundled in the Python core
- setuptools replaces distutils, but you have to opt-in
- there are alternatives...

A useful starting point is the [Python Packaging User Guide](https://packaging.python.org/en/latest/)

### Related concepts I will refer to, but will not cover today

- Version control - releases, tags, branches
- Testing
- Continuous integration and deployment
- Conda environments

## Anatomy of a Python Package

Take a look at the simple [LCOE](https://github.com/willu47/lcoe) project.

**EXERCISE** - Write a brief description next to each line (after the `#`)

```
docs  # Documentation (read the docs) files from Sphinx or similar
src/lcoe  # the actual source code, organised into sub folders
tests  # Contains code to test the functionality of every function (what is expected)
.gitignore  # contains info on which files and folder to ignore
AUTHORS.rst  # Contributors to the package
CHANGELOG.rst  # Keeping track of versions
LICENSE.txt  # The type of license for the package
README.rst  # The content for the landing page/Readme
requirements.txt  # Contains the list of dependencies required to install the package with their specific versions
setup.cfg  #
setup.py  # 
```

## Packaging a Python project

A nice [tutorial](https://packaging.python.org/en/latest/tutorials/packaging-projects/)

## Tools to help you

- [pyscaffold](https://pyscaffold.org/en/stable/)
- [cookiecutter](https://cookiecutter.readthedocs.io/en/1.7.2/)
- [poetry](https://python-poetry.org/)
- ...

- [tox](https://tox.wiki/en/latest/)

To build your project using Pyscaffold.

1. Create an annotated tag `git tag -a v0.2.2 -m "A description of the release"`
2. Build the project using pyscaffold `tox -e build`
3. You can find the built project in the `dist` folder


## Uploading to PyPi

```
python3 -m pip install --upgrade build
python3 -m build
```

## Continuous Deployment



