Python

Install

First things first, let’s install the Python library!

(If you already installed the VSCode extension, the extension setup may have already installed the library.)

Run this command to ensure the python library is installed:

$ pip install polyapi-python

Caution

Please be sure to install the correct polyapi pypi package polyapi-python, as there are other registered packages with similar names.

Note

If you see an error like this:

ERROR: Could not find a version that satisfies the requirement polyapi-python (from versions: none)
ERROR: No matching distribution found for polyapi-python

Or like this:

ERROR: Ignored the following versions that require a different python version

It’s probably because you are using an older version of Python!

Use python --version to check your Python version. polyapi-python requires Python 3.10 or higher.

Warning

On MacOS, as of 2024-10-28, Python 3.13 (released on 2024-10-07) does not work very well yet.

We recommend using Python 3.12 for now.

Setup and Generate

Next, we need to make sure the PolyAPI Python client is properly configured:

$ python -m polyapi setup

Enter your server and API Key when prompted.

Next, let’s go ahead and retrieve all the trained Poly functions and generate the Python SDK for them:

$ python -m polyapi generate

Warning

Your api key is stored inside your PolyAPI library wherever it is installed (usually site_packages) in a file name .config.env

Do not commit this file to your git repo.

Develop First Function

Next, let’s develop our first custom function!

Open a new file called hello.py and add the following code:

def hello():
   return "Hello Poly World!"

Next use the PolyAPI Python SDK to deploy this function:

$ python -m polyapi --context mycontext --server function add hello hello.py

This will deploy a new serverless function to Poly. You can also instead do –client to create functions which are packaged into the generated SDK.

Run First Function

Finally, let’s open a new file called “run.py” and add the following code:

from polyapi import poly
print(poly.mycontext.hello())

Now run the python file:

$ python run.py

You should see “Hello Poly World!” printed in the console!

Onward

That’s it! You have now:

  • Setup your Python SDK

  • Trained your first server function

  • Ran your first server function

This is the last step on the guided tour of Poly.

To further explore aspects of Poly and what it can do, head over to Next Steps.