cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
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

8 REPLIES 8

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)

ameet9257
New Contributor III

if you ever received this kind of error after installing the correct Python package then try running the below command. 

dbutils.library.restartPython()
Amit Prajapati

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you won’t want to miss the chance to attend and share knowledge.

If there isn’t a group near you, start one and help create a community that brings people together.

Request a New Group