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 thepython_version
,python_packages
andsystem_packages
arguments. - Git: If you're deploying using
git
, then your Python packages are specified inrequirements.txt
while your Python version and system packages are specified inmetadata.yaml
.
In addition to configuring the environment's installed dependencies, you can include additional files in your deployment:
- Extra files for one deployment: If you have one or two
.py
files that only one deployment depends on. - Common files for all deployments: If you have Python files that should be available in all deployments.
- Private packages & wheels: If you have a
setup.py
orpyproject.toml
and usepip install
to make your libraries available for import.
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:
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"])
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:
--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:
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.