yfinance icon indicating copy to clipboard operation
yfinance copied to clipboard

Exception near "without": syntax error - ticker.fast_info['market_cap']

Open zacra opened this issue 2 years ago • 14 comments

I want to import 'market_cap' information, but I am getting an error.

Exception near "without": syntax error

check please

zacra avatar Jan 28 '23 23:01 zacra

Post exact code that reproduces error.

ValueRaider avatar Jan 28 '23 23:01 ValueRaider

ticker = yf.Ticker('AAPL') print(ticker.fast_info['market_cap'])

zacra avatar Jan 28 '23 23:01 zacra

Actually, there's more info you need to provide. Read this properly

ValueRaider avatar Jan 28 '23 23:01 ValueRaider

yf version is 0.2.9

OS: AWS amazon Linux 2.0

zacra avatar Jan 29 '23 00:01 zacra

  • Error message, with traceback if shown

All exceptions generate a traceback. What is confusing about this request?

ValueRaider avatar Jan 29 '23 00:01 ValueRaider

Full Code 👍

import yfinance as yf import pprint

try:

stock_code = 'AAPL'

print(stock_code, " ..Start.. ")
ticker = yf.Ticker(stock_code)


stock_info2 = ticker.fast_info
print("-------------------------\n")
pprint.pprint(stock_info2)
print("-------------------------\n")
print(stock_info2['last_price'])
print("-------------------------\n")
print(stock_info2['market_cap'])
print("-------------------------\n")

except Exception as e: print("Exception ", e)

Full Log 👍

AAPL ..Start..

lazy-loading dict with keys = ['currency', 'exchange', 'timezone', 'shares', 'market_cap', 'last_price', 'previous_close', 'open', 'day_high', 'day_low', 'regular_market_previous_close', 'last_volume', 'fifty_day_average', 'two_hundred_day_average', 'ten_day_average_volume', 'three_month_average_volume', 'year_high', 'year_low', 'year_change']

145.92999267578125

Exception near "without": syntax error

zacra avatar Jan 29 '23 00:01 zacra

try:
...
except Exception as e:
print("Exception ", e)

You are deliberately discarding the traceback by doing this. Stop making this difficult.

ValueRaider avatar Jan 29 '23 00:01 ValueRaider

Oh Sorry.. Error code is

Traceback (most recent call last): File "/var/autobot/test_i.py", line 20, in print(stock_info2['market_cap']) File "/home/ec2-user/.local/lib/python3.7/site-packages/yfinance/base.py", line 110, in getitem return getattr(self, k) File "/home/ec2-user/.local/lib/python3.7/site-packages/yfinance/base.py", line 402, in market_cap shares = self.shares File "/home/ec2-user/.local/lib/python3.7/site-packages/yfinance/base.py", line 213, in shares shares = self._tkr.get_shares_full(start=pd.Timestamp.utcnow().date()-pd.Timedelta(days=548)) File "/home/ec2-user/.local/lib/python3.7/site-packages/yfinance/base.py", line 1506, in get_shares_full tz = self._get_ticker_tz(debug_mode=False, proxy=None, timeout=10) File "/home/ec2-user/.local/lib/python3.7/site-packages/yfinance/base.py", line 1176, in _get_ticker_tz cache = utils.get_tz_cache() File "/home/ec2-user/.local/lib/python3.7/site-packages/yfinance/utils.py", line 839, in get_tz_cache _tz_cache = _TzCache() File "/home/ec2-user/.local/lib/python3.7/site-packages/yfinance/utils.py", line 764, in init self._tz_db = _KVStore(_os.path.join(self._db_dir, "tkr-tz.db")) File "/home/ec2-user/.local/lib/python3.7/site-packages/yfinance/utils.py", line 721, in init self.conn.execute('create table if not exists "kv" (key TEXT primary key, value TEXT) without rowid') sqlite3.OperationalError: near "without": syntax error

zacra avatar Jan 29 '23 00:01 zacra

Run this, print output:

import sqlite3
print(sqlite3.sqlite_version)

And what is your Python version?

ValueRaider avatar Jan 29 '23 00:01 ValueRaider

Python version is Python 3.7.16 sqlite3 version is 3.7.17

zacra avatar Jan 29 '23 00:01 zacra

So that error message generated by old sqlite bundled in old Python. Can update Python to 3.8?

ValueRaider avatar Jan 29 '23 00:01 ValueRaider

Alternatively, this branch should fix error: fix/old-sqlite-error. Instructions #1080

ValueRaider avatar Jan 29 '23 00:01 ValueRaider

Thanks.

I will try both solutions!

zacra avatar Jan 29 '23 00:01 zacra

I solved it by upgrading the sqlite3 version. thank you!

zacra avatar Jan 29 '23 02:01 zacra

Hey I am able to run the yfinance API locally, but when I tried running it on Amazon MWAA Airflow I got the same error

My local machine has this configuration python version - 3.10.8 Sqlite3 - 3.37.2

and my Amazon MWAA Airflow configuration is python version - 3.10.8 (main, Jan 17 2023, 22:57:31) [GCC 7.3.1 20180712 (Red Hat 7.3.1-15)] SQLite - 3.7.17

@zacra how did you upgrade the sqlite version, can you please share the steps?

rpandit-info avatar Feb 02 '23 11:02 rpandit-info

Hey I am able to run the yfinance API locally, but when I tried running it on Amazon MWAA Airflow I got the same error

My local machine has this configuration python version - 3.10.8 Sqlite3 - 3.37.2

and my Amazon MWAA Airflow configuration is python version - 3.10.8 (main, Jan 17 2023, 22:57:31) [GCC 7.3.1 20180712 (Red Hat 7.3.1-15)] SQLite - 3.7.17

@zacra how did you upgrade the sqlite version, can you please share the steps?

Even I am facing the same problem. Not able to use yfinance in AWS Airflow 2.4.3

jasmohan-narula avatar Feb 02 '23 12:02 jasmohan-narula

Hey I am able to run the yfinance API locally, but when I tried running it on Amazon MWAA Airflow I got the same error

My local machine has this configuration python version - 3.10.8 Sqlite3 - 3.37.2

and my Amazon MWAA Airflow configuration is python version - 3.10.8 (main, Jan 17 2023, 22:57:31) [GCC 7.3.1 20180712 (Red Hat 7.3.1-15)] SQLite - 3.7.17

@zacra how did you upgrade the sqlite version, can you please share the steps?

try below command!

pip3 install yfinance --upgrade --no-cache-dir

wget https://kojipkgs.fedoraproject.org//packages/sqlite/3.8.11/1.fc21/x86_64/sqlite-devel-3.8.11-1.fc21.x86_64.rpm wget https://kojipkgs.fedoraproject.org//packages/sqlite/3.8.11/1.fc21/x86_64/sqlite-3.8.11-1.fc21.x86_64.rpm sudo yum install sqlite-3.8.11-1.fc21.x86_64.rpm sqlite-devel-3.8.11-1.fc21.x86_64.rpm

zacra avatar Feb 02 '23 12:02 zacra

@zacra Locally I'm able to update sqlite. Not sure how I will be able to update on AWS Airflow.

jasmohan-narula avatar Feb 02 '23 12:02 jasmohan-narula

@zacra Locally I'm able to update sqlite. Not sure how I will be able to update on AWS Airflow.

Oh I use AWS EC2. I'm not sure how solves this problem on AWS Airflow

zacra avatar Feb 02 '23 13:02 zacra

Not sure how I will be able to update on AWS Airflow.

You don't need to. PIP pre-release 0.2.10b1 has a fix for older sqlite verison (and dev branch), can anyone confirm fixed?

ValueRaider avatar Feb 02 '23 13:02 ValueRaider

@ValueRaider

A very simple test

import yfinance as yf
import sqlite3

def test_crawler():
  print(yf.version.version)
  print(sqlite3.sqlite_version)

  y_dict = yf.download(tickers=['7203.T', ], start='2023-01-01', end='2023-02-01',)
  print(len(y_dict))
# def

The output

0.2.10b2
3.7.17
[*********************100%***********************]  1 of 1 completed
19

OS is

$ cat /etc/system-release
Amazon Linux release 2 (Karoo)

And, thank you all for yfinance !

clxy avatar Feb 04 '23 06:02 clxy

import yfinance as yf import sqlite3

def test_crawler(): print(yf.version.version) print(sqlite3.sqlite_version)

y_dict = yf.download(tickers=['7203.T', ], start='2023-01-01', end='2023-02-01',) print(len(y_dict))

def

Hi @clxy,

I ran the following code in Amazon MWAA Airflow:-

    import yfinance as yf
    import sqlite3

    print(yf.version.version)
    print(sqlite3.sqlite_version)
    y_dict = yf.download(tickers=['7203.T', ], start='2023-01-01', end='2023-02-01',)
    print(len(y_dict))
    print("Code ran on Amazon Airflow")

This is the output that I got:

[2023-02-07 05:45:41,762] {{logging_mixin.py:104}} INFO - 0.2.9
[2023-02-07 05:45:41,762] {{logging_mixin.py:104}} INFO - 3.7.17
[2023-02-07 05:45:41,773] {{logging_mixin.py:104}} INFO - 
[*********************100%***********************]  1 of 1 completed
[2023-02-07 05:45:41,776] {{logging_mixin.py:104}} WARNING - /usr/local/airflow/.local/lib/python3.7/site-packages/watchtower/__init__.py:349 WatchtowerWarning: Received empty message. Empty messages cannot be sent to CloudWatch Logs
[2023-02-07 05:45:41,776] {{logging_mixin.py:104}} WARNING - Traceback (most recent call last):
[2023-02-07 05:45:41,777] {{logging_mixin.py:104}} WARNING -   File "/usr/local/airflow/config/cloudwatch_logging.py", line 162, in emit
    self.sniff_errors(record)
[2023-02-07 05:45:41,777] {{logging_mixin.py:104}} WARNING -   File "/usr/local/airflow/config/cloudwatch_logging.py", line 211, in sniff_errors
    if pattern.search(record.message):
[2023-02-07 05:45:41,777] {{logging_mixin.py:104}} WARNING - AttributeError: 'LogRecord' object has no attribute 'message'
[2023-02-07 05:45:41,777] {{logging_mixin.py:104}} INFO - 
1 Failed download:
[2023-02-07 05:45:41,777] {{logging_mixin.py:104}} INFO - - 7203.T: OperationalError('near "without": syntax error')
[2023-02-07 05:45:41,777] {{logging_mixin.py:104}} INFO - 0
[2023-02-07 05:45:41,777] {{logging_mixin.py:104}} INFO - Code ran on Amazon Airflow

jasmohan-narula avatar Feb 07 '23 05:02 jasmohan-narula

@clxy If I ran similar code on my local machine

import yfinance as yf
import sqlite3

print(yf.version.version)
print(sqlite3.sqlite_version)
y_dict = yf.download(tickers=['7203.T', ], start='2023-01-01', end='2023-02-01',)
print(len(y_dict))
print("Code ran on local python")

I get the following output:-

0.2.9
3.39.4
[*********************100%***********************]  1 of 1 completed
19
Code ran on local python

jasmohan-narula avatar Feb 07 '23 05:02 jasmohan-narula

@clxy @ValueRaider the issue is that there is no way for me to upgrade the version of the sqlite3 library on Amazon MWAA Airflow.

So effectively, yfinance is broken for me if I use it on Amazon MWAA Airflow.

jasmohan-narula avatar Feb 07 '23 05:02 jasmohan-narula

@jasmohan-narula

I have no idea about Amazon MWAA Airflow. But, as ValueRaider said above

You don't need to. PIP pre-release 0.2.10b1 has a fix for older sqlite verison (and dev branch), can anyone confirm fixed?

And, they just released the 0.2.10 today. So, just re-install yfinance, and try again.

clxy avatar Feb 08 '23 06:02 clxy