MPyReq is a MATLAB-based Python requirements manager that simplifies installing and configuring Python environments for MATLAB projects. It handles Python version management, package installations, git repository cloning, and weight file downloads with a simple API. MPyReq ensures consistent Python environments across sessions by caching configurations and providing runtime setup for your Python dependencies.
Setup
Call MPyReq.setInstallFolder(largeSSDFolder) to set the install folder.
This only needs to be done once per machine. All subsequent uses of MPyReq share downloaded content from this folder.
Getting Started
Invoke help to see documentation
help MPyReq.m
Note: Click 'Open in MATLAB Online' and run the demoCellpose example in MATLAB Online.
You can set install folder to "/tmp/pydata" when trying it out.
Example
MPyReq.python("3.12");
MPyReq.pipPackage("cellpose");
im = imread("AT3_1m4_01.tif");
model = py.cellpose.models.CellposeModel(gpu=true);
outputs = model.eval(im);
labels = uint16(outputs{1});
imageshow(im,OverlayData=labels)
More Examples
Run the following examples after ensuring to set an installation folder using the call below. Ideally, this folder should be a fast access (e.g SSD) with ~15+ GB free space. These examples download large libraries and model files.
These examples were tested on a glnxa64 machine with a working GPU.
Ensure to run this to clear the python environment between two demo runs:
terminate(pyenv); clear MPyReq
demoCellpose - Run https://www.cellpose.org/ cell segmentation model.
demoDepthPro - Run https://github.com/apple/ml-depth-pro A quick, sharp monocular metric depth model.
demoSAM2 - Run https://github.com/facebookresearch/sam2 Segment Anything Model 2
demoVGGT - Run https://vgg-t.github.io/ Visual Geometry Grounded Transformer
Troubleshooting
Python version or Package Conflicts
MPyReq does not resolve package conflicts. If you tried to install Package A first which used x.xx version of an upstream package U, and then tried to add Package B which used y.yy version of U in the same pyenv session - you will get odd Python errors due to conflicts in required version of Package U.
Best strategy is to work with one project at a time, and clear the environment before switching projects with conflicting requirements.
terminate(pyenv); clear MPyReq
Workflows needing models with conflicting requirements will have to serialize their runs with the above reset between calls. For best performance, run all data through model 1 first, serialize the results into disk (or MATLAB variables if possible), and then run model 2 on the saved/cached data.
win64
MPyReq is expected to work on win64.
However, installing CUDA enabled PyTorch has not been tested. MPyReq uses uv as the install engine, so uv environment variables are honored. You can experiment with setting this environment variable before invoking any installs to guide uv to download from the pytorch index setenv("UV_EXTRA_INDEX_URL","https://download.pytorch.org/whl/cu126")
Alternate repos
setenv("UV_DEFAULT_INDEX", "your internal repo")