Quickstart
Convert a Python script into your first Prefect workflow
Prefect makes it easy to deploy Python scripts, run them on a schedule, make them robust to failure, and observe them in a UI.
To do this, you need to perform the following tasks:
- Install Prefect
- Connect to a Prefect API (self-hosted or Prefect Cloud)
- Add decorators to the functions in the script
Let’s start with a Python script that fetches the number of stars for a list of GitHub repositories.
Install Prefect
To install Prefect with pip, run:
See Install Prefect for more details on installation.
Connect to a Prefect API
-
Head to https://app.prefect.cloud/ and sign in or create a free Prefect Cloud account.
-
Log in to Prefect Cloud from your development environment:
-
Choose Log in with a web browser and click the Authorize button in the browser window that opens.
Your CLI is now authenticated with your Prefect Cloud account through a locally stored API key that expires in 30 days.
If you have any issues with browser-based authentication, you can authenticate with a manually created API key instead.
Convert your script to a Prefect workflow
Decorators are the easiest way to convert a Python script into a workflow.
- Add a
@flow
decorator to the script’s entrypoint. - Add
@task
decorators to any functions called by the flow.
This will create a flow and corresponding tasks. Tasks receive metadata about upstream dependencies and the state of those dependencies before they run. Prefect records these dependencies and states as it orchestrates tasks.
The log_prints=True
argument provided to the @flow
decorator automatically converts any print
statements within the function to INFO
level logs.
Run your flow
You can run your Prefect flow just as you would a Python script:
The output in your terminal should look similar to this:
Prefect automatically tracks the state of the flow run and logs the output, which can be viewed directly in the terminal or in the UI.
Next steps
In this tutorial, you successfully converted a Python script to a deployable workflow tracked by Prefect.
Next, get this workflow off of your laptop and run it automatically on a schedule.
Need help? Book a meeting with a Prefect Product Advocate to get your questions answered.