ImportCollector
It’s simple and requires 0 dependencies outside of the standard library.
This script will recursively traverse the dependencies of a Class
or python-script and find all relevant dependencies from your local project.
It’s useful in multiple types of project, such as (remote) Machine Learning training jobs and serverless deployments, where you don’t want to include irrelevant files.
Why I built this
When I was deploying/building a MLFlow Model
I found that their infer_code_paths
functionality is bugged, as shared in mlflow/issues/14071 and my blog about MLFlow Models, and that I needed something better to really recursively fetch dependencies.
I found that through my nifty little script I could do this better than mlflow
themselves. By updating the load_context
function we could infer the modules
by importing them, assisting mlflow
’s infer_code_paths
function.
def load_context(self, context):
# MLFlow bug where parent class is not added to `infer_code_paths`.
# https://github.com/mlflow/mlflow/issues/14071
= get_dependencies(type(self))
imports for module in imports.modules:
importlib.import_module(module)
This is it for this time,
Hampus Londögård