Poetry is a tool for dependency management and packaging in Python. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you. While we generally recommend using requirements.txt or adding packages directly to your environment, you may have scenarios where you prefer to use Poetry, or it may just be a tool you're already comfortable using, so here's a quick overview to getting started with Poetry in Domino!
Contents:
1. Setting up your Environment
2. Creating or Initializing a Poetry Project
1. Environment Setup
You can add Poetry to your environment with Dockerfile instructions. The following instructions will install poetry and add the tool as an additional path in your environment (more information on PATHs in Domino):
ENV \
POETRY_HOME=/etc/poetry \
PATH=/etc/poetry/bin:$PATH
ADD https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py ./
RUN python get-poetry.py && chmod +x /etc/poetry/bin/poetry \
&& echo "export PATH=/etc/poetry/bin/:${PATH}" >> /home/ubuntu/.domino-defaults
*If you get a build error with python: can't open file 'get-poetry.py': [Errno 13] Permission denied, you may need to switch to the user root before this code in your Dockerfile:
USER root
ENV \
POETRY_HOME=/etc/poetry \
PATH=/etc/poetry/bin:$PATH
ADD https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py ./
RUN python get-poetry.py && chmod +x /etc/poetry/bin/poetry \
&& echo "export PATH=/etc/poetry/bin/:${PATH}" >> /home/ubuntu/.domino-defaults
USER ubuntu
2. Creating or Initializing a Poetry Project
Creating a New Poetry Project
We can use Poetry in a Workspace to start a new project and automatically generate the basic Poetry project files. To begin, start a new Workspace using the Poetry environment and your preferred IDE. Once your Workspace launches, go to the Terminal and use the command poetry new <project-name>
.
This will create a new subfolder <project_name> in your Project files with all of the files necessary to get started with Poetry!
Initializing a Poetry project in an existing folder
If you've already created a subfolder for your project and you simply want to add the Poetry components, you can instead cd into that folder in the Terminal, and run poetry init
.
You will be prompted to set up some options, or hit 'Enter' to accept defaults. Once completed, this will create a pyproject.toml for Poetry in your existing folder.
3. Sample Job using Poetry
We won't re-hash all of the Poetry commands as there's an overview of the Basic usage of Poetry on their site here: Basic Usage, but here's a quick example of a Job you could run using Poetry:
For this job, we're simply going to use the Pendulum package (installed through Poetry) to tell you what time it is in the US Pacific timezone. Our simple example python file, which we'll put in our Poetry project folder from Step 2:
#poetry-test.py
import pendulum
myTime = pendulum.now('US/Pacific')
print(f'Hello World, it\'s {myTime} at my house!')
To run this using Poetry:
a) Add any packages you need to pyproject.toml that lives in the Poetry project folder we set up in step 2. This can be done by manually editing the file, for example:
[tool.poetry.dependencies]
pendulum = "^1.4"
or in a Workspace terminal using Poetry: $ poetry add pendulum
. This adds the package to the pyproject.toml automatically without you having to edit it yourself and worry about syntax. (Be sure to sync your Workspace back to your Project!)
b) Once you have Poetry set up with the packages you need, we'll need an .sh
file to point to our Poetry project folder and pyproject.toml file. This file only contains a few steps and can be placed in your Poetry project or in your main project Files:
#!/bin/sh
# Change to our Poetry project folder
cd my-poetry-proj
# Install packages specified in pyproject.toml
poetry install
# Run our python script
poetry run python poetry-test.py
c) Run the Job using the .sh file we created! Once the job completes, you can in the Results that Poetry installed our packages and ran our python script:
This is just a basic example of how to use Poetry in Domino. Be sure to check out their site for more information!
Comments
0 comments
Please sign in to leave a comment.