Skip to content

Instantly share code, notes, and snippets.

@peterotool
Last active July 17, 2023 16:34
Show Gist options
  • Save peterotool/0aa8d3b05cdbc18baeba84a1de8d5067 to your computer and use it in GitHub Desktop.
Save peterotool/0aa8d3b05cdbc18baeba84a1de8d5067 to your computer and use it in GitHub Desktop.
Poetry

Poetry

Why Poetry?

  • Poetry lets you easily define and install dependencies
  • And resolve dependency issues automatically
  • Bonus: Poetry makes it super easy to pusblish libraries to PyPI

Other tools to create virtual environments

Install poetry

pip install poetry

Update Poetry

# update poetry to [latest]
poetry self update
# update poetry to [latest-dev]
poetry self update --preview

Set up Pyenv (Pre-requierement)

pyenv install <python_version>
pyenv local <python_version> # go into your project directory and set the local python version

Initialising Poetry in a pre-existing project

Instead of creating a new project, Poetry can be used to ‘initialise’ a pre-populated directory. To interactively create a pyproject.toml file in directory pre-existing-project:

cd pre-existing-project
poetry init

Creating a virtual environment with poetry

poetry config virtualenvs.in-project true
pyenv local <python_version>
poetry install
poetry env info # display info where the virtual env is located
poetry env -p   # get the path where the virtual env is located

Activating Virtual Env

poetry shell

Installing Dependencies

poetry add  <package> # install package
poetry remove  <package> # remove package

Add and install dev dependency

poetry add --group dev black
poetry add --group dev pytest@latest

Deactivating Virtual Env

deactive 
exit

Removing it

rm -fr $pwd/.env

Poetry in VSCode

poetry env list --full-path # path that belongs to poetry’s virtual environment (venv).
poetry env info --executable
  • Within the project, make a new folder called .vscode.
  • Within the folder, make a file called settings.json
  • In the file, add the following configuration:
{
    "python.pythonPath": "<copied path>"
}

Packaging and publishing using poetry

Run Python Script

poetry run <script.py>

Advance Poetry

Enable completion for bash or zsh

print -l $fpath | grep '.oh-my-zsh/completions'

poetry completions zsh  > $HOME/.antigen/bundles/zsh-users/zsh-completions/_poetry

For oh-my-zsh you must then enable poetry in your ~/.zshrc plugin

antigen bundle poetry

Dockerfile: python poetry image

FROM python:3.8

# Metadata
LABEL name="PBG Poetry Example"
LABEL maintainer="PBG"
LABEL version="0.1"

ARG YOUR_ENV="virtualenv"

ENV YOUR_ENV=${YOUR_ENV} \
    PYTHONFAULTHANDLER=1 \
    PYTHONUNBUFFERED=1 \
    PYTHONHASHSEED=random \
    PIP_NO_CACHE_DIR=off \
    PIP_DISABLE_PIP_VERSION_CHECK=on \
    PIP_DEFAULT_TIMEOUT=100 \
    POETRY_VERSION=1.1.6 \
    LC_ALL=C.UTF-8 \
    LANG=C.UTF-8

# System deps:
RUN DEBIAN_FRONTEND=noninteractive apt update && apt install -y libpq-dev gcc

# Install poetry
RUN pip install "poetry==$POETRY_VERSION"

# Copy only requirements to cache them in docker layer
WORKDIR /app

#Copy all the project files
COPY . .
# Install libraries 
RUN poetry config virtualenvs.create false \
    && poetry install $(test "$YOUR_ENV" = production) --no-dev --no-interaction --no-ansi

# Set the launching script exec
RUN chmod +x launch.sh

# Launch the script for cron
CMD ["bash", "launch.sh"]

# Launch main python script
# CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:8000", "core.app:app"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment