pnkflydgr
pnkflydgr

Reputation: 715

Python package not found - Alpaca

I followed the guide from the official Alpaca-py Documentation - https://alpaca.markets/docs/python-sdk/getting_started.html#introduction

I ran: pip install alpaca-py

Then I created this file called alpaca.py:

from alpaca.data.historical import StockHistoricalDataClient
from alpaca.data.requests import StockBarsRequest
from alpaca.data.timeframe import TimeFrame

client = StockHistoricalDataClient('api_key', 'secret_key')

request_params = StockBarsRequest(
    symbol_or_symbols=["AAPL", "MSFT"],
    timeframe=TimeFrame.Day,
    start="2022-11-01"
)

bars = client.get_stock_bars(request_params)

print(bars)

Everything autocompleted just fine like it was finding the package. For example, when I typed 'from alpaca.', I see: enter image description here

But when I run it I see the error at the bottom of this block:

PS E:\code\projects\kcInvestments> py --version
Python 3.11.0
PS E:\code\projects\kcInvestments> py -m pip --version
pip 22.3 from C:\Python311\Lib\site-packages\pip (python 3.11)
PS E:\code\projects\kcInvestments> pip list
Package            Version
------------------ ---------
aiohttp            3.8.1
aiosignal          1.2.0
alpaca-py          0.6.1
async-timeout      4.0.2
attrs              22.1.0
autopep8           2.0.0
certifi            2022.9.24
charset-normalizer 2.1.1
deprecation        2.1.0
frozenlist         1.3.1
idna               3.4
msgpack            1.0.3
multidict          6.0.2
numpy              1.23.4
packaging          21.3
pandas             1.5.1
pip                22.3
pycodestyle        2.9.1
pydantic           1.10.2
pyparsing          3.0.9
python-dateutil    2.8.2
pytz               2022.6
PyYAML             6.0
requests           2.28.1
setuptools         63.2.0
six                1.16.0
sseclient-py       1.7.2
TA-Lib             0.4.24
tomli              2.0.1
typing_extensions  4.4.0
urllib3            1.26.12
websocket-client   1.4.1
websockets         10.4
yarl               1.8.1
PS E:\code\projects\kcInvestments> py alpaca.py
Traceback (most recent call last):
  File "E:\code\projects\kcInvestments\alpaca.py", line 1, in <module>
    from alpaca.data.historical import StockHistoricalDataClient
  File "E:\code\projects\kcInvestments\alpaca.py", line 1, in <module>
    from alpaca.data.historical import StockHistoricalDataClient
ModuleNotFoundError: No module named 'alpaca.data'; 'alpaca' is not a package
PS E:\code\projects\kcInvestments> py test.py
[1 2 3]

You can see that I'm using python 3.11.0, I'm using the corresponding pip and you can see alpaca-py in the pip list.

Python is not my main language so I'm not sure if I'm doing something wrong here or if there is something wrong with the package.

I create this file called test.py:

import numpy as np

arr = np.array([1, 2, 3])

print(arr)

You can see that it prints: [1 2 3] from the block above.

This makes me believe that it's not something wrong with my setup but something wrong with the package itself.

Upvotes: 0

Views: 1178

Answers (2)

UserName _
UserName _

Reputation: 3

first of all make sure alpaca-py is installed correctly if its on env or main environment folder.

pip install alpaca-py

then make sure the file you are coding in is NOT name alpaca.py

change the file name to something else and it will work wonderfully.

Upvotes: 0

pnkflydgr
pnkflydgr

Reputation: 715

It looks like it was a naming conflict with my file name being alpaca.py.

This post helped me: Python 'No module named' error; 'package' is not a package

Upvotes: 4

Related Questions