Python environment

Set your application's Python environment

In code files, you can write Python code to run alongside your ML models. This code may be business logic, data storage and retrieval, or model processing code, and is attached to your models and made available to your applications via worklets. Code for each application, in both draft and production environments, runs in an isolated and customizable Python environment. Note that this document refers only to the Python code in your code files, not to Python functions in the view builder which use their own limited, browser-compatible Python environment.

The application's Python environment runs Python 3.9 and comes with a number of common data science packages pre-installed such as numpy, pandas, and scipy.

Installing a PyPi package

You might need to use a Python package that is not installed in the environment that runs your Code and Decision blocks. For instance, in the build your first app tutorial, you need the wikipedia package. You can specify packages according to standard pip specifications in your application's requirements.txt file.

After making changes, click "Save & install" and the packages will be added to your application's environment.

When you publish your app, it publishes these environment changes along with any code changes so your code and its dependencies can stay in sync.

Installing a custom Python wheel

And at times you might want to reuse some code that you've already written and would rather not have to copy/paste into Baseten. You can instead package it up as a Python wheel and use the full URL to link to it in your requirements.txt file:

https://files.internal.mycompany.com/myproject-1.0.0-py3-none-any.whl

Depending on how you manage your code, you may instead want to bring in custom code via GitHub import.

System packages

Baseten runs your Python code in a container using a standard Python image for the Debian Operating System.

Occasionally, you may come across Python packages that require additional system packages to function. For example, librosa, a Python package for audio analysis requires the ffmpeg system package for understanding a number of common audio formats such as MP3. Similarly, OpenCV, the popular image-processing package, requires the libjpeg system package to read JPEG files.

Installing system packages

Installing system packages works the same as adding Python packages. In system-packages.txt, add the packages you want to install, then click "Save & install" and they will be installed. After making changes, click "Save & install" and the packages will be added to your application's environment.

Currently, only standard Debian packages are installable but we intend to expand the number of available repositories. Please let us know if you're unable to install a package you need.

Last updated