from dhanhq import DhanContext, dhanhq
ImportError: cannot import name ‘DhanContext’ from ‘dhanhq’ … getting this error
Hi @saghar_sakpal ,
We request you to please try passing ‘‘from dhanhq import dhanhq’’ and check if you are still receiving the same error response while importing the library.
Hi Shubham, i am also getting same error for market feed code.
This part is working ‘‘from dhanhq import dhanhq’’ but from dhanhq import DhanContext, dhanhq
ImportError: cannot import name ‘DhanContext’ from ‘dhanhq’ … getting this error
@Rajan_Arora , @saghar_sakpal ,
On the commandline type : pip show dhanhq
It should show the below:
Name: dhanhq
Version: 2.1.0
If it shows version v2.0.2, install the latest dhanhq version using this command:
pip install --no-cache-dir --force-reinstall
git+https://github.com/dhan-oss/DhanHQ-py.git@main
The package will be updated to v2.1.0 and shall resolve the ‘DhanContext’ import error.
Hi Amit
I am still getting the same error after doing what you suggested. Please help.
pip show dhanhq
Name: dhanhq
Version: 2.1.0
Summary: The official Python client for communicating with the DhanHQ API
Home-page: https://dhanhq.co/
Author: Dhan
Author-email: dhan-oss@dhan.co
Thanks
Sai
Hey @saisaigraph ,
We request you to please ensure that the latest version of pip is installed on your system. You may do this by running the following command:
pip install dhanhq==2.1.0
After installation, you can import the DhanHQ Libraries as shown below:
from dhanhq import DhanContext, dhanhq
dhan_context = DhanContext("client_id", "access_token")
dhan = dhanhq(dhan_context)
Please replace "client_id" and "access_token" with your actual credentials. You can then start making API calls seamlessly.
For reference, you may also visit our official GitHub repository here:
Trishul Thanks for your response.
In cursor it is using python 3.9… version and i get this error but
in vscode it is using python 3.13… version and it does not give the error has been my observation.
now i get this error in vscode:
no close frame received or sent
at this code:
data = MarketFeed(dhan_context, instruments_v2, version)
while True:
data.run_forever()
response = data.get_data()
instruments = [(1, ‘2963’)] #SAIL symbol
Please suggest/help.
Hii @saisaigraph ,
Please ensure that your instruments field is correctly structured. Below is the complete sample code to fetch live data using WebSocket via the DhanHQ API:
from dhanhq import DhanContext, MarketFeed
# Define and use your dhan_context if you haven't already done so like below:
dhan_context = DhanContext("client_id", "access_token")
# Structure for subscribing is (exchange_segment, "security_id", subscription_type)
instruments = [
(MarketFeed.NSE, "1333", MarketFeed.Ticker)]
version = "v2" # Mention version and set to the latest 'v2'
try:
data = MarketFeed(dhan_context, instruments, version)
while True:
data.run_forever()
response = data.get_data()
print(response)
except Exception as e:
print(e)
Using the exact code given by you get the same error:
symbol = SAIL, sec_id = 2963
instruments = [(1, ‘2963’)]
subscription_code = 15
(1, ‘2963’, 15)
no close frame received or sent
Please let me know if you would like to connect via zoom please so i can show you.
Hello @saisaigraph ,
Please drop us an email at help@dhan.co from your registered email address and we will connect with you over Gmeet to help you receive data via websocket.
Hi @saisaigraph ,
As discussed over the video meet, we are glad that we could resolve your websocket error and that you are now able to fetch the data correctly using the correct client id and payload.
