Python packages

PyPI packages

You can install your required PyPI packages and import them in your Python files. Cortex looks for a requirements.txt file in the top level Cortex project directory (i.e. the directory which contains cortex.yaml):

├── cortex.yaml
├── ...
└── requirements.txt

Note that some packages are pre-installed by default (see "pre-installed packages" for your Predictor type in the Predictor documentation).

It is also possible to reference Python libraries that are packaged using

Here is an example directory structure:

├── cortex.yaml
├── ...
├── mypkg
│ └──
├── requirements.txt

In this case, requirements.txt can include a single .:

# requirements.txt

If this is the contents

from distutils.core import setup

And looks like this:

# mypkg/
def hello():

You can reference your package in

import mypkg
class PythonPredictor:
def predict(self, payload):

Private packages on GitHub

You can also install private packages hosed on GitHub by adding them to requirements.txt using this syntax:

# requirements.txt
git+https://<personal access token><username>/<repo name>.git@<tag or branch name>#egg=<package name>

You can generate a personal access token by following these steps.

Conda packages

You can install Conda packages by creating a custom Docker image that first installs Conda and then installs your Conda packages.

Customize the template Dockerfile below with your desired Conda packages and follow these instructions to build and push your image to a container registry and configure Cortex to use your custom image.

# Dockerfile
# remove system-wide packages from the base image
RUN pip freeze > req && for pkg in "$(cat req)"; do pip uninstall $pkg -y || true; done && rm req
# add conda to path
ENV PATH /opt/conda/bin:$PATH
# install conda, it also includes py3.6.9
RUN curl --output ~/ && \
/bin/bash ~/ -b -p /opt/conda && \
rm ~/ && \
/opt/conda/bin/conda update conda && \
/opt/conda/bin/conda install --force python=3.6.9 && \
/opt/conda/bin/conda clean -tipsy && \
ln -s /opt/conda/etc/profile.d/ /etc/profile.d/ && \
echo ". /opt/conda/etc/profile.d/" >> ~/.bashrc && \
echo "conda activate base" >> ~/.bashrc
# install pip dependencies
RUN pip install --upgrade pip && \
pip install --no-cache-dir -r /src/cortex/lib/requirements.txt && \
pip install --no-cache-dir -r /src/cortex/serve/requirements.txt
# ---------------------------------------------------------- #
# Install your Conda packages here
# RUN conda install --no-update-deps -c conda-forge rdkit
# ---------------------------------------------------------- #
# replace system python with conda's version
RUN sed -i 's/\/usr\/bin\/python3.6/\/opt\/conda\/bin\/python/g' /src/cortex/serve/