Can't import dlt into Python notebook

Boris Akselrud 16 Reputation points
2022-09-15T06:42:06.867+00:00

I am trying to use Databricks Delta Live Tables from Python notebook. The very first thing is to "import dlt" which fails and complains that the module is not found.
The Databricks workspace is premium and the new DLT Pipelines feature is avilable, I can create and define a pipeline to run the notebook
I'm trying to figure out if the "import dlt" will work in the notebook without running the notebook as part of a pipeline (if this is the case, it would be very hard to deal with)

Azure Databricks
Azure Databricks
An Apache Spark-based analytics platform optimized for Azure.
2,514 questions
{count} vote

2 answers

Sort by: Most helpful
  1. PRADEEPCHEEKATLA 90,641 Reputation points Moderator
    2022-09-16T10:55:21.51+00:00

    Hello @Boris Akselrud ,

    Thanks for the additional details.

    This is an excepted when you are running interactive notebooks.

    Make to sure to install dlt using the command pip install dlt:

    241846-image.png

    For different methods to install packages in Azure Databricks, refer to How to install a library on a databricks cluster using some command in the notebook?

    Hope this will help. Please let us know if any further queries.

    ------------------------------

    • Please don't forget to click on 130616-image.png or upvote 130671-image.png button whenever the information provided helps you. Original posters help the community find answers faster by identifying the correct answer. Here is how
    • Want a reminder to come back and check responses? Here is how to subscribe to a junotification
    • If you are interested in joining the VM program and help shape the future of Q&A: Here is jhow you can be part of Q&A Volunteer Moderators

  2. Bryden, Alain 1 Reputation point
    2022-12-01T18:04:44.913+00:00

    As many have noted, the special "dlt" library isn't "available" when running your python code from the databricks notebook editor, only when running it from a pipeline (which means you lose out on being able to easily check your code's syntax before attempting to run it)

    You also can't "%pip install" this library, because it isn't a public package, and the "dlt" package out there has nothing to do with Databricks.

    Here's the solution I came up with...

    You can "catch" the import error and mock out a dlt class sufficiently that the rest of your code can be checked. This slightly improves the developer experience until you get a chance to actually run it in a pipeline.

    So replace import dlt at the top of your first cell with the following:

    try:  
      import dlt # When run in a pipeline, this package will exist (no way to import it here)  
    except ImportError:  
      class dlt: # "Mock" the dlt class so that we can syntax check the rest of our python in the databricks notebook editor  
        def table(comment, **options): # Mock the @dlt.table attribute so that it is seen as syntactically valid below  
          def _(f):  
            pass  
          return _;   
    

    Further mocking may be required depending on how many features from the dlt class you use, but you get the gist.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.