cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
cancel
Showing results for 
Search instead for 
Did you mean: 

cannot import name 'sql' from 'databricks'

mickniz
Contributor

I am working on Databricks version 10.4 premium cluster and while importing sql from databricks module I am getting below error.

cannot import name 'sql' from 'databricks' (/databricks/python/lib/python3.8/site-packages/databricks/__init__.py).Trying to execute below sample code

from databricks import sql

import os

with sql.connect(server_hostname = os.getenv("DATABRICKS_SERVER_HOSTNAME"),

         http_path    = os.getenv("DATABRICKS_HTTP_PATH"),

         access_token  = os.getenv("DATABRICKS_TOKEN")) as connection:

 with connection.cursor() as cursor:

  cursor.execute("SELECT * FROM default.x LIMIT 2")

  result = cursor.fetchall()

  for row in result:

   print(row)

ANy suggestion here

7 REPLIES 7

Matt101122
Contributor

@Ritu Kumari​ ignore my last response lol Looks like you need to install this lib:

https://docs.databricks.com/dev-tools/python-sql-connector.html#package

Package

databricks-sql-connector

Usage: 

pip install databricks-sql-connector

See also databricks-sql-connector in the Python Package Index (PyPI).

Hubert-Dudek
Esteemed Contributor III

Just use natively spark.sql this way code is much faster and simpler you use the power of RDD. All you need is

spark.sql("SELECT * FROM default.x LIMIT 2")

Anonymous
Not applicable

Hi @Ritu Kumari​ 

Hope all is well!

Just wanted to check in if you were able to resolve your issue and would you be happy to share the solution or mark an answer as best? Else please let us know if you need more help. 

We'd love to hear from you.

Thanks!

Anonymous
Not applicable

Just currios, why did you use spark.sql for your case I think it easy than import sql from databricks.

mickniz
Contributor

I basically used pyodbc

Spark sql is also way.

Sumit_Kumar
New Contributor III

There are 2 instances I got this error -

  1. Installing correct python package. Make sure you install "databricks.sql" module, not "databricks" module. Run below code in jupyter -

%pip install databricks.sql

2. The hostname, os.getenv("DATABRICKS_SERVER_HOSTNAME") returns, should NOT contain "https://".

Please upvote the answer if it solves your issue.

wallystart
New Contributor II

I resolve the same error installing library from cluster interface (UI)

Welcome to Databricks Community: Lets learn, network and celebrate together

Join our fast-growing data practitioner and expert community of 80K+ members, ready to discover, help and collaborate together while making meaningful connections. 

Click here to register and join today! 

Engage in exciting technical discussions, join a group with your peers and meet our Featured Members.