ValueRaider / yfinance-cache

Caching wrapper for yfinance module. Intelligent caching, not dumb caching of web requests.
MIT License
24 stars 9 forks source link

macOS: Unable to run yfinance-cache: python multiprocessing errors #67

Open sonique6784 opened 2 weeks ago

sonique6784 commented 2 weeks ago

I'm not able to run yfinance-cache. I get the following python multiprocessing errors:

I tried with Python 3.12 and 3.9, macOS Ventura

What Python version do you recommend?

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/local/Cellar/python@3.9/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/spawn.py", line 116, in spawn_main
    exitcode = _main(fd, parent_sentinel)
  File "/usr/local/Cellar/python@3.9/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/spawn.py", line 125, in _main
    prepare(preparation_data)
  File "/usr/local/Cellar/python@3.9/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/spawn.py", line 236, in prepare
    _fixup_main_from_path(data['init_main_from_path'])
  File "/usr/local/Cellar/python@3.9/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/spawn.py", line 287, in _fixup_main_from_path
    main_content = runpy.run_path(main_path,
  File "/usr/local/Cellar/python@3.9/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/runpy.py", line 288, in run_path
    return _run_module_code(code, init_globals, run_name,
  File "/usr/local/Cellar/python@3.9/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/runpy.py", line 97, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "/usr/local/Cellar/python@3.9/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/path/to/venv/script.py", line 376, in <module>
    best_performing_asset(snp500)
  File "/path/to/venv/script.py", line 250, in best_performing_asset
    stock_data = get_asset_data(asset_list)
  File "/path/to/venv/script.py", line 219, in get_asset_data
    stock_data = yf.download(asset_list_joined, start=start, end=end, interval="1mo")["Close"]  # Download closing monthly.

  File "/path/to/venv/lib/python3.9/site-packages/yfinance_cache/yfc_multi.py", line 48, in download
    queue = multiprocessing.Manager().Queue()
  File "/usr/local/Cellar/python@3.9/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/context.py", line 57, in Manager
    m.start()
  File "/usr/local/Cellar/python@3.9/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/managers.py", line 554, in start
    self._process.start()
  File "/usr/local/Cellar/python@3.9/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/process.py", line 121, in start
    self._popen = self._Popen(self)
  File "/usr/local/Cellar/python@3.9/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/context.py", line 284, in _Popen
    return Popen(process_obj)
  File "/usr/local/Cellar/python@3.9/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/popen_spawn_posix.py", line 32, in __init__
    super().__init__(process_obj)
  File "/usr/local/Cellar/python@3.9/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/popen_fork.py", line 19, in __init__
    self._launch(process_obj)
  File "/usr/local/Cellar/python@3.9/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/popen_spawn_posix.py", line 42, in _launch
    prep_data = spawn.get_preparation_data(process_obj._name)
  File "/usr/local/Cellar/python@3.9/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/spawn.py", line 154, in get_preparation_data
    _check_not_importing_main()
  File "/usr/local/Cellar/python@3.9/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/spawn.py", line 134, in _check_not_importing_main
    raise RuntimeError('''
RuntimeError: 
        An attempt has been made to start a new process before the
        current process has finished its bootstrapping phase.

        This probably means that you are not using fork to start your
        child processes and you have forgotten to use the proper idiom
        in the main module:

            if __name__ == '__main__':
                freeze_support()
                ...

        The "freeze_support()" line can be omitted if the program
        is not going to be frozen to produce an executable.
Traceback (most recent call last):
  File "/path/to/venv/script.py", line 5, in <module>
    import yfinance_cache as yf
  File "/path/to/venv/lib/python3.9/site-packages/yfinance_cache/__init__.py", line 3, in <module>
    from .yfc_dat import Period, Interval, AmbiguousComparisonException
  File "/path/to/venv/lib/python3.9/site-packages/yfinance_cache/yfc_dat.py", line 266, in <module>
    manager = Manager()
  File "/usr/local/Cellar/python@3.9/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/context.py", line 57, in Manager
    m.start()
  File "/usr/local/Cellar/python@3.9/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/managers.py", line 558, in start
    self._address = reader.recv()
  File "/usr/local/Cellar/python@3.9/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/connection.py", line 250, in recv
    buf = self._recv_bytes()
  File "/usr/local/Cellar/python@3.9/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/connection.py", line 414, in _recv_bytes
    buf = self._recv(4)
  File "/usr/local/Cellar/python@3.9/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/connection.py", line 383, in _recv
    raise EOFError
EOFError
ValueRaider commented 1 week ago

What version YFC? Latest release has big changes to download, so I'm curious if upgrading to latest, or downgrading to previous, helps.

sonique6784 commented 1 week ago

@ValueRaider thanks I tried to downgrade as suggested, but I still get the same error. I tried with yfinance 0.2.40 (latest), 0.2.38, 0.2.36 (found in yfinance-cache dependencies), but no luck.

Perhaps you could share your configuration and module versions with pip list?

$ python --version    
Python 3.9.19

$ pip list                    
Package               Version
--------------------- -----------
appdirs               1.4.4
beautifulsoup4        4.12.3
certifi               2024.7.4
charset-normalizer    3.3.2
click                 8.1.7
exchange_calendars    4.5.5
frozendict            2.4.4
html5lib              1.1
idna                  3.7
korean-lunar-calendar 0.3.1
lxml                  5.2.2
multitasking          0.0.11
numpy                 1.26.4
pandas                2.0.3
peewee                3.17.5
pip                   24.0
platformdirs          4.2.2
pyluach               2.2.0
python-dateutil       2.9.0.post0
pytz                  2024.1
requests              2.32.3
scipy                 1.13.1
setuptools            69.2.0
six                   1.16.0
soupsieve             2.5
toolz                 0.12.1
tzdata                2024.1
urllib3               2.2.2
webencodings          0.5.1
yfinance              0.2.36
yfinance-cache        0.6.4
ValueRaider commented 1 week ago

Python 3.12. It's more likely to be how multiprocessing works on Macs than package versions - I dev on Linux. You aren't first to encounter issues on Mac

sonique6784 commented 1 week ago

I tried with Python 3.12, but the issue is the same. Sounds like it is specific to Mac. I will run my program in a Docker for now.

FYI

$ python --version
Python 3.12.3

$ pip list
Package               Version
--------------------- -----------
appdirs               1.4.4
beautifulsoup4        4.12.3
certifi               2024.7.4
charset-normalizer    3.3.2
click                 8.1.7
exchange_calendars    4.5.5
frozendict            2.4.4
html5lib              1.1
idna                  3.7
korean-lunar-calendar 0.3.1
lxml                  5.2.2
multitasking          0.0.11
numpy                 1.26.4
pandas                2.0.3
peewee                3.17.5
pip                   24.0
platformdirs          4.2.2
pyluach               2.2.0
python-dateutil       2.9.0.post0
pytz                  2024.1
requests              2.32.3
scipy                 1.14.0
six                   1.16.0
soupsieve             2.5
toolz                 0.12.1
tzdata                2024.1
urllib3               2.2.2
webencodings          0.5.1
yfinance              0.2.40
yfinance-cache        0.6.4