Skip to content

Python

NOTE:

The source code for the examples below can be found on GitHub.

Create the following Python script:

if __name__ == '__main__':
    print("Hello, world!")

Then, create a workflow YAML file:

workflows:
  - name: hello-py
    provider: bash
    commands:
      - python python/hello.py

Now, run it locally using the dstack run command:

$ dstack run hello-py

RUN           WORKFLOW  SUBMITTED  STATUS     TAG  BACKEND 
shady-1       hello-py  now        Submitted       local

Provisioning... It may take up to a minute. ✓

To interrupt, press Ctrl+C.

Hello, world

Python packages

You can use pip within workflows install Python packages.

Let's create the following Python script:

import pandas as pd

if __name__ == '__main__':
    df = pd.DataFrame(
        {
            "Name": [
                "Braund, Mr. Owen Harris",
                "Allen, Mr. William Henry",
                "Bonnell, Miss. Elizabeth",
            ],
            "Age": [22, 35, 58],
            "Sex": ["male", "male", "female"],
        }
    )

    print(df)

Now, create the following workflow YAML file:

workflows:
  - name: hello-pandas
    provider: bash
    commands:
      - pip install pandas
      - python python/hello_pandas.py

Run it locally using the dstack run command:

$ dstack run hello-pandas

Python version

By default, the workflow uses the same Python version that you use locally. You can override the major Python version using the python property:

workflows:
  - name: python-version
    provider: bash
    python: 3.7
    commands:
      - python --version

Run it locally using the dstack run command:

$ dstack run python-version