Getting started
This section gives you a quick introduction to writing and running workflows on Union and Flyte 2.
Prerequisites
Install uv
First,
install the uv
package manager.
You will need to use the
uv
package manager to run the examples in this guide.
In particular, we leverage uv
’s ability to
embed dependencies directly in scripts.
Install Python 3.10 or later
Flyte 2 requires Python 3.10 or later. Install the most recent version of Python (>= 3.10) compatible with your codebase and pin it. For example, to install and pin Python 3.13, do the following:
uv python install 3.13
uv python pin 3.13 --global
Create a Python virtual environment
In your working directory, create a Python virtual environment and activate it:
uv venv
source .venv/bin/activate
Install the flyte
package
Install the latest flyte
package in the virtual environment (we are currently in beta, so you will have to enable prerelease installation):
uv pip install --no-cache --prerelease=allow --upgrade flyte
Create a config.yaml
Next, create a configuration file that points to your Flyte instance.
Use the
flyte create config
command, making the following changes:
- Replace
my-org.my-company.com
with the actual URL of your Flyte backend instance. You can simply copy the domain part of the URL from your browser when logged into your backend instance. - Replace
my-project
with an actual project. The project you specify must already exist on your Flyte backend instance.
flyte create config \
--endpoint my-org.my-company.com \
--builder local \
--domain development \
--project my-project
By default, this will create a ./.flyte/config.yaml
file in your current working directory.
See
Setting up a configuration file for details.
flyte get config
to see the current configuration file being used by the flyte
CLI.Hello world example
Create a file called hello.py
with the following content:
# hello.py
import flyte
# A TaskEnvironment provides a way of grouping the configuration used by tasks.
env = flyte.TaskEnvironment(name="hello_world")
# Use a TaskEnvironment to define tasks, which are regular Python functions.
@env.task
def fn(x: int) -> int: # Type annotations are recommended.
slope, intercept = 2, 5
return slope * x + intercept
# Tasks can call other tasks.
# Each task defined with a given TaskEnvironment will run in its own separate container,
# but the containers will all be configured identically.
@env.task
def main(x_list: list[int] = list(range(10))) -> float:
x_len = len(x_list)
if x_len < 10:
raise ValueError(f"x_list doesn't have a larger enough sample size, found: {x_len}")
# flyte.map is like Python map, but runs in parallel.
y_list = list(flyte.map(fn, x_list))
y_mean = sum(y_list) / len(y_list)
return y_mean
# Running this script locally will perform a flyte.run,
# which will deploy your task code to your remote Union/Flyte instance.
if __name__ == "__main__":
# Establish a remote connection from within your script.
flyte.init_from_config("config.yaml")
# Run your tasks remotely inline and pass parameter data.
run = flyte.run(main, x_list=list(range(10)))
# Print various attributes of the run.
print(run.name)
print(run.url)
# Stream the logs from the remote run to the terminal.
run.wait(run)
Understanding the code
In the code above we do the following:
- Import the
flyte
package. - Define a
TaskEnvironment
to group the configuration used by tasks. - Define two tasks using the
@env.task
decorator.- Tasks are regular Python functions, but each runs in its own container.
- When deployed to your Union/Flyte instance, each task execution will run in its own separate container.
- Both tasks use the same
env
(the sameTaskEnvironment
) so, while each runs in its own container, those containers will be configured identically.
Running the code
Make sure that your config.yaml
file is in the same directory as your hello.py
script.
Now, run the script with:
uv run --prerelease allow hello.py
The main guard section in the script performs a flyte.init_from_config
to set up the connection with your Union/Flyte instance and a flyte.run
to send your task code to that instance and execute it there.
The example scripts in this guide have a main guard that programmatically deploys and runs the tasks defined in the same file.
All you have to do is execute the script itself.
You can also deploy tasks using the flyte
CLI instead. We will cover this in a later section.
Viewing the results
In your terminal, you should see output like this:
cg9s54pksbjsdxlz2gmc
https://my-instance.example.com/v2/runs/project/my-project/domain/development/cg9s54pksbjsdxlz2gmc
Run 'a0' completed successfully.
Click the link to go to your Union instance and see the run in the UI: