External (source) packages
A guide on configuring your truss to use external packages
You might encounter a situation where you have to incorporate your own modules or third-party package(not on PyPi) into your truss. Truss has a few different mechanisms to support this.
- Using the packages directory
- Using the external packages directory
Let’s look at using the packages directory first.
Using the packages directory
Each truss, when initialized, comes with a packages
directory. This directory is at the same level as the model
directory in the hierarchy.
Inside this directory, you can place any additional python packages that your would like to reference inside your truss.
For example, your packages directory might look like this:
You can import these packages inside your model.py
like so:
These packages get bundled with your truss at build time. Because of this, it’s ideal to use this method when your packages are small.
But what if you have multiple trusses that want to reference the same package? This is where the external_package_dirs
comes in handy.
The external_package_dirs
allows you to import packages that are outside your truss and hence allows multiple trusses to reference the same package.
Let’s look at an example.
Using the external packages directory
Let’s say you have the following setup:
In this case the package you want to import, super_cool_awesome_plugin
, is outside the truss. You could move the super_cool_awesome_plugin
directory inside the packages
directory if you wanted to, but there is another option.
Inside the config.yaml
you can specify the path to external packages by using the key external_package_dirs
. Under this key, you can provide a list of external packages that you would like to use in your truss.
Here is what that would look like for the example above:
Configuring the external_package_dirs path
The path of the external packages must be relative to the config.yaml file.
So super_cool_awesome_plugin/
is parallel to stable-diffusion/
, but it’s one directory up from the config.yaml so we use ../super_cool_awesome_plugin
.
Here’s how you can reference your packages inside model.py
:
Depending on the use-case either of these techniques can be used. If you have a one-off package that your truss needs, consider using the packages
directory.
On the other hand, if you have a common package that will get used by multiple trusses, external_package_dirs
is the better option.