Mage allows you to easily integrate third-party Python packages into your project and pipelines. This guide will walk you through the process of adding and managing external dependencies.
requirements.txt
requirements.txt
.
This file is used to specify all the Python packages that your project depends on.
requirements.txt
. Open this file
and add the Python package you want installed and available throughout your entire project.
For example, if your project is named demo_project
then the file will be located at
demo_project/requirements.txt
.
requirements.txt
file and list each package on a new line. You can also specify version requirements if needed.
requirements.txt
:requirements.txt
file will install:
USER_CODE_PATH
environment variable, which defines your project path (e.g. /home/src/project_name
),
to locate your requirements.txt
file and install the packages listed there.
Using Command Line Flags
USER_CODE_PATH
can be set using command line flags. You will need to add these command line flags at every run when running a new Docker container or installing a new version of Mage.USER_CODE_PATH
in DockerUSER_CODE_PATH
environment variable tells Mage where your project code is located within the Docker container. This is essential for features like automatic package installation from requirements.txt
.USER_CODE_PATH
when running a Mage Docker container, use the following command structure:it
: Runs the container interactively with a pseudo-TTY.p 6789:6789
: Maps port 6789 from the container to your host machine.v $(pwd):/home/src
: Mounts your current directory to /home/src
in the container.e USER_CODE_PATH=/home/src/project_name
: Sets the USER_CODE_PATH
environment variable.mageai/mageai
: Specifies the Mage Docker image to use./app/run_app.sh mage start [project_name]
: The command to run inside the container.USER_CODE_PATH
setting is not persistent across container restarts or when installing new versions of Mage. You must include these flags each time you run a new Docker container.[project_name]
with your actual Mage project name in both the USER_CODE_PATH
and the start command.USER_CODE_PATH
matches the mount point specified in the v
flag. In this example, we mount to /home/src
, so the USER_CODE_PATH
starts with /home/src
.$(pwd)
in the v
flag assumes you’re running the Docker command from your project’s parent directory. Adjust this path if you’re running from a different location.Using Dockerfile
USER_CODE_PATH
, which tells Mage where your project code is located.
While this can be done using command-line flags when running Docker containers, it’s also possible to set the USER_CODE_PATH
directly in your Dockerfile. This approach can be more convenient for consistent deployments and when building custom images.FROM mageai/mageai:latest
PROJECT_NAME
: Should be replaced with your actual project name.MAGE_CODE_PATH
: Sets the base path for Mage code.USER_CODE_PATH
: Constructs the full path to your project, effectively setting the USER_CODE_PATH
for Mage.WORKDIR ${MAGE_CODE_PATH}
MAGE_CODE_PATH
.COPY ${PROJECT_NAME} ${PROJECT_NAME}
CMD ["/bin/sh", "-c", "/app/run_app.sh"]
[project_name]
with your actual project name.USER_CODE_PATH
using build arguments. By defining it this way, you ensure that Mage will
consistently know where to find your project code within the container, without needing to specify it each time you run the container.Follow the Repository setup documentation to deploy this
Dockerfile to fully automate the installation of third-party Python pacakges.USER_CODE_PATH
is set to your project path, using
the command line flags or the Dockerfile methods.
Example: /home/src/project_name
run_app.sh
to manage the application startup process.
run_app.sh
script checks the USER_CODE_PATH
environment variable.requirements.txt
file within the specified project directory.requirements.txt
are automatically installed before the server starts.