{"id":8271,"library":"libify","title":"Libify: Import Databricks Notebooks as Libraries/Modules","description":"Libify (version 0.78) is a Python library designed to simplify the process of importing Databricks notebooks as modules. It enables nesting of notebook imports for complex workflows and supports Databricks Runtime Version 5.5 and above. The library was last released in September 2020, indicating an infrequent release cadence and a focus on maintaining existing functionality rather than active development.","status":"maintenance","version":"0.78","language":"en","source_language":"en","source_url":"https://github.com/vagrantism/libify","tags":["databricks","notebooks","import","modules","library","workflow"],"install":[{"cmd":"pip install libify","lang":"bash","label":"Standard PyPI Installation"},{"cmd":"1. Click 'Clusters' icon in sidebar.\n2. Click a running cluster name.\n3. Click 'Libraries' tab.\n4. Click 'Install New'.\n5. Under 'Library Source', choose 'PyPI'.\n6. Under 'Package', write 'libify'.\n7. Click 'Install'.","lang":"text","label":"Databricks Cluster Library Installation"}],"dependencies":[],"imports":[{"symbol":"libify","correct":"import libify"},{"note":"Used in the 'importee' notebook to expose its contents.","symbol":"exporter","correct":"import libify\nlibify.exporter(globals())"},{"note":"Used in the 'importer' notebook to load another notebook as a module.","symbol":"importer","correct":"import libify\nimported_module = libify.importer(globals(), '/path/to/your/notebook')"}],"quickstart":{"code":"# --- In the notebook to be imported (e.g., '/Users/my_user/my_functions') ---\n# This cell must be the LAST cell in the notebook and contain ONLY this code.\ndef greet(name):\n    return f\"Hello, {name} from imported notebook!\"\n\ndef add(a, b):\n    return a + b\n\nimport libify\nlibify.exporter(globals())\n\n# --- In the importing notebook (e.g., '/Users/my_user/main_workflow') ---\nimport libify\n\n# The path should be relative to the Databricks workspace root or absolute DBFS path\nmy_module = libify.importer(globals(), '/Users/my_user/my_functions')\n\n# Now you can access functions/variables defined in 'my_functions' notebook\nmessage = my_module.greet(\"World\")\nprint(message)\n\nresult = my_module.add(5, 3)\nprint(f\"The sum is: {result}\")\n\n# Expected output in the importing notebook:\n# Hello, World from imported notebook!\n# The sum is: 8","lang":"python","description":"To use Libify, you need two types of notebooks: an 'importee' (the one providing functions) and an 'importer' (the one consuming them). The 'importee' notebook must end with a specific `libify.exporter` call to make its global scope available. The 'importer' notebook then uses `libify.importer` to load the target notebook as a module, allowing access to its defined functions and variables using dot notation."},"warnings":[{"fix":"Remove any `dbutils.notebook.exit()` calls from 'importee' notebooks.","message":"The `dbutils.notebook.exit()` function must NOT be used in notebooks that are intended to be imported using `libify`. Its presence will prevent `libify` from properly exporting the notebook's global state.","severity":"gotcha","affected_versions":"All"},{"fix":"Ensure the last cell of the 'importee' notebook contains only `import libify; libify.exporter(globals())`.","message":"The `libify.exporter(globals())` call in the 'importee' notebook must be the absolute last cell and contain ONLY that specific code. Any other code or comments in that final cell will prevent correct module export.","severity":"gotcha","affected_versions":"All"},{"fix":"For new projects or refactoring, evaluate using Databricks 'Files in Repos' for native Python module imports instead of `libify`.","message":"Databricks introduced 'Files in Repos' functionality (available since late 2021 / Feb 2023 update) which allows direct import of Python files as modules. This native feature might offer a more streamlined approach for new projects compared to `libify`, especially for standard Python modules.","severity":"deprecated","affected_versions":"< 0.78 (consider for new projects)"},{"fix":"Include `libify` installation and the importer notebook's initial `import libify` as part of your cluster initialization scripts or rerun manually after restarts.","message":"On Databricks Community Cloud, the installation and setup for `libify` needs to be rerun each time a cluster is created or restarted for imports to function correctly.","severity":"gotcha","affected_versions":"All (specific to Databricks Community Cloud)"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Access specific attributes (functions, variables, classes) from the imported notebook using dot notation (e.g., `my_module.my_function()` or `my_module.my_variable`).","cause":"Attempting to call the imported notebook module directly instead of its functions/variables (e.g., `my_module()` instead of `my_module.function_name()`).","error":"TypeError: module object is not callable"},{"fix":"Verify `libify` is installed on your cluster (see installation steps). Check your workspace for any conflicting files named `libify.py`.","cause":"The `libify` package is not correctly installed on the Databricks cluster, or the `import libify` statement failed. This can also happen if there's a file named `libify.py` in your workspace that's shadowing the actual package.","error":"AttributeError: module 'libify' has no attribute 'importer'"},{"fix":"Ensure the importee notebook does not use `dbutils.notebook.exit()` and that `libify.exporter(globals())` is the *only* code in the *last* cell of the importee notebook.","cause":"The 'importee' notebook either used `dbutils.notebook.exit()` prematurely, or the `libify.exporter(globals())` call was not the last and only statement in its final cell. The global state was not correctly captured.","error":"Notebook functions/variables are not accessible via the imported module (e.g., `AttributeError: 'module' object has no attribute 'my_function'`)"}]}