# Cannot import StockDataStream

**URL:** <https://forum.alpaca.markets/t/cannot-import-stockdatastream/12944>\
**Category:** Random\
**Created:** [October 2, 2023, 6:46pm UTC](https://forum.alpaca.markets/t/cannot-import-stockdatastream/12944 "2023-10-02T18:46:42Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jackw](https://avatars.discourse-cdn.com/v4/letter/j/67e7ee/32.png) [@jackw](https://forum.alpaca.markets/u/jackw)\
**Post date:** [October 2, 2023, 6:46pm UTC](https://forum.alpaca.markets/t/cannot-import-stockdatastream/12944/1 "2023-10-02T18:46:42Z")

</div>

Getting the error “ImportError: cannot import name ‘StockDataStream’ from ‘alpaca.data’” for the following code.

alpaca-py is installed.

```auto
from alpaca.data import StockDataStream

from api import *

# keys are required for live data

#crypto_stream = CryptoDataStream("api-key", "secret-key")

# keys required

stock_stream = StockDataStream(ALPACA_API_KEY_ID,ALPACA_API_SECRET_KEY)

print(stock_stream)

```

---

<div class="post-metadata">

**Author:** ![dana](https://avatars.discourse-cdn.com/v4/letter/d/9de053/32.png) [@dana](https://forum.alpaca.markets/u/dana)\
**Post date:** [June 22, 2024, 3:19am UTC](https://forum.alpaca.markets/t/cannot-import-stockdatastream/12944/2 "2024-06-22T03:19:16Z")

</div>

Hi, did you fix it? I’m getting the same error- alpaca-py is installed

---

<div class="post-metadata">

**Author:** ![Dan\_Whitnable\_Alpaca](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/dan_whitnable_alpaca/32/1658_2.png) [@Dan\_Whitnable\_Alpaca](https://forum.alpaca.markets/u/Dan_Whitnable_Alpaca)\
**Post date:** [June 22, 2024, 1:42pm UTC](https://forum.alpaca.markets/t/cannot-import-stockdatastream/12944/3 "2024-06-22T13:42:27Z")

</div>

@dana The imports from from the alpaca-py SDK can be challenging. In this specific case try the following

`from alpaca.data.live.stock import StockDataStream`

A general approach to finding where to import things from is to check out the reference section of the documentation. For example, the StockDataStream class is documented [here](https://alpaca.markets/sdks/python/api_reference/data/stock/live.html#stockdatastream) and shown below.

 ![image](https://us1.discourse-cdn.com/flex020/uploads/alp/original/2X/b/b0365011bc8fc834f1a739ef5bdb298c4ffe153d.jpeg)

Notice the full path to the class is provided. This is true of all the classes. Simply include that full path in your import statement. There often can be shortcuts but that will always work.

---

<div class="post-metadata">

**Author:** ![dana](https://avatars.discourse-cdn.com/v4/letter/d/9de053/32.png) [@dana](https://forum.alpaca.markets/u/dana)\
**Post date:** [June 23, 2024, 7:34am UTC](https://forum.alpaca.markets/t/cannot-import-stockdatastream/12944/4 "2024-06-23T07:34:59Z")

</div>

Hi @Dan_Whitnable_Alpaca  
Thanks for your help and now I know how to find the right import.  
I was trying to do CryptoDataStream, following the example on the bottom of your documentation guide here: [Market Data - Alpaca-py](https://alpaca.markets/sdks/python/market_data.html)  
The example is incorrect- it says ‘from alpaca.data import CryptoDataStream, StockDataStream’  
This doesn’t work. It should probably be updated.

---

<div class="post-metadata">

**Author:** ![dana](https://avatars.discourse-cdn.com/v4/letter/d/9de053/32.png) [@dana](https://forum.alpaca.markets/u/dana)\
**Post date:** [July 21, 2024, 3:11am UTC](https://forum.alpaca.markets/t/cannot-import-stockdatastream/12944/5 "2024-07-21T03:11:38Z")

</div>

Hey @Dan_Whitnable_Alpaca  
Is there any example code of this that works?  
I had it working a month ago but now I keep getting an error about  
RuntimeError: asyncio.run() cannot be called from a running event loop

---

<div class="post-metadata">

**Author:** ![dana](https://avatars.discourse-cdn.com/v4/letter/d/9de053/32.png) [@dana](https://forum.alpaca.markets/u/dana)\
**Post date:** [July 21, 2024, 9:18pm UTC](https://forum.alpaca.markets/t/cannot-import-stockdatastream/12944/6 "2024-07-21T21:18:25Z")

</div>

I was probably just tired last night. Anyway, code works today and thought I’d post it in case anyone else is having trouble in the future.

```auto
from alpaca.trading.client import TradingClient
import os
from alpaca.data.live.crypto import CryptoDataStream
import logging

# Set up logging
logging.basicConfig(level=logging.INFO,
                    format='%(asctime)s - %(levelname)s - %(message)s')

ALPACA_API_KEY = os.getenv('ALPACA_API_KEY')
ALPACA_SECRET_KEY = os.getenv('ALPACA_SECRET_KEY')
trading_client = TradingClient(ALPACA_API_KEY, ALPACA_SECRET_KEY)

# Get our account information.
account = trading_client.get_account()

# Check if our account is restricted from trading.
if account.trading_blocked:
    print('Account is currently restricted from trading.')

# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger( __name__ )

# Initialize the CryptoDataStream
crypto_stream = CryptoDataStream(
    api_key=ALPACA_API_KEY, secret_key=ALPACA_SECRET_KEY)

# Define an asynchronous handler function to process the incoming data

async def quote_data_handler(data):
    logger.info(f"Received quote data: {data}")

# Subscribe to trade data for BTC/USD
crypto_stream.subscribe_quotes(quote_data_handler, "BTC/USD")

# Run the websocket connection
crypto_stream.run()

```

---

<div class="post-metadata">

**Author:** ![Dan\_Whitnable\_Alpaca](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/dan_whitnable_alpaca/32/1658_2.png) [@Dan\_Whitnable\_Alpaca](https://forum.alpaca.markets/u/Dan_Whitnable_Alpaca)\
**Post date:** [July 24, 2024, 12:51am UTC](https://forum.alpaca.markets/t/cannot-import-stockdatastream/12944/7 "2024-07-24T00:51:51Z")

</div>

@dana Great job. Thank you for posting! I’m sure others will find that helpful.
