Skip to main content

Custom Python environments

Each Modelbit deployment gets its own custom environment where you choose which specific versions of Python packages and system packages get installed.

  • Notebook: If you're deploying using a Python notebook, you specify your deployment's environment when calling mb.deploy using the python_version, python_packages and system_packages arguments.
  • Git: If you're deploying using git, then your Python packages are specified in requirements.txt while your Python version and system packages are specified in metadata.yaml.

In addition to configuring the environment's installed dependencies, you can include additional files in your deployment:

Python notebook

In general, the modelbit package will detect the Python packages needed for your deployment when you deploy. The following functions can be used to specify additional packages:

For these examples, suppose you have a deployment function called my_deploy_function:

def my_deploy_function(...):
return ...

Specifying Python packages

To install a specific version of sklearn in your deployment, run:

mb.deploy(my_deploy_function, python_packages=["scikit-learn==1.0.2",])

Likewise, you can specify multiple packages and versions:

mb.deploy(my_deploy_function, python_packages=["scikit-learn==1.0.2", "xgboost==1.5.2"])

Specifying Python version

To specify a specific version of Python:

mb.deploy(my_deploy_function, python_version="3.9")

Specifying system packages

Sometimes your Python packages rely on system packages like git or gcc. To add system packages, use the system_packages parameter in mb.deploy. These packages get installed with apt-get install.

mb.deploy(my_deploy_function, system_packages=["libgomp1"])

You can specify multiple system packages:

mb.deploy(my_deploy_function, system_packages=["build-essential", "cmake"])

Specifying a requirements file

If you have a file like requirements.txt that specifies the desired environment you can use that instead of setting Python packages with python_packages=. Use the requirements_txt_path= parameter to supply the file path of a pip-compatible requirements file.

mb.deploy(my_deploy_function, requirements_txt_path="path/to/requirements.txt")

Git

Within your deployment's directory, in your Modelbit git repository, are two files for specifying environment dependencies:

  • requirements.txt -- Lists the Python packages, and their versions, to install for this deployment.
  • metadata.yaml -- Specifies the Python version and any system packages to install.

Python packages in requirements.txt

Modelbit will execute pip install -r requirements.txt when it builds the environment for your deployment. That means you can add index URLs comments and github-based packages to install. Keep in mind, all the packages listed in the requirements.txt must be publicly accessible or stored as private packages in Modelbit. For example:

requirements.txt
--extra-index-url=https://download.pytorch.org/whl/
git+https://github.com/facebookresearch/segment-anything.git
torch==2.3.0+cu121
torchvision==0.18.0+cu121
matplotlib==3.7.1
numpy==1.25.2
opencv-python==4.8.0.76

System packages in metadata.yaml

Some Python packages require additional system libraries to be installed in order to function properly. You can specify these additional packages in metadata.yaml, and they'll be installed with apt-get install <packages>.

Here's an example configuration to install several system packages (listed under systemPackages), and use the 3.9 version of Python:

metadata.yaml
owner: you@company.com
runtimeInfo:
mainFunction: example_deployment
mainFunctionArgs:
- a:int
- return:int
pythonVersion: "3.9"
systemPackages:
- build-essential
- libgl1
- libgl1-mesa-glx
- libglib2.0-0
schemaVersion: 2

Check out the metadata.yaml documentation for additional parameters.