yfinance-cache icon indicating copy to clipboard operation
yfinance-cache copied to clipboard

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

Open sonique6784 opened this issue 1 year ago • 6 comments

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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/connection.py", line 383, in _recv
    raise EOFError
EOFError

sonique6784 avatar Jul 06 '24 04:07 sonique6784

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

ValueRaider avatar Jul 06 '24 10:07 ValueRaider

@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

sonique6784 avatar Jul 07 '24 02:07 sonique6784

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

ValueRaider avatar Jul 07 '24 10:07 ValueRaider

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

sonique6784 avatar Jul 07 '24 10:07 sonique6784

I have the same issue on a windows machine.

louiskenyon98 avatar Sep 25 '24 06:09 louiskenyon98

I don't have access to Windows nor Mac. Most changes in #46 were from ChatGPT/Claude, so try getting them to fix. There isn't much parallel code to feed the AI:

  • process launch https://github.com/ValueRaider/yfinance-cache/blob/8ce6b3f098c0d8bdd7700bd85997cc175ae9f005/yfinance_cache/yfc_multi.py#L47-L83
  • safety locks https://github.com/ValueRaider/yfinance-cache/blob/8ce6b3f098c0d8bdd7700bd85997cc175ae9f005/yfinance_cache/yfc_dat.py#L263-L269

ValueRaider avatar Sep 26 '24 17:09 ValueRaider

I landed on this thread from a google search about yfinance-cache and python multiprocessing errors on my mac using python 3.12.9.

So I decided to run the same code in a docker container, and it worked fine. It seemed like a possible mac vs linux issue.

When stepping through with the python debugger though, it was looking for some metadata in ~/Library/Caches/py-yfinance-cache. I removed that folder and it started working without error (and the cache folder was recreated).

Anyway, might be worth trying, @sonique6784 and @louiskenyon98.

keithbentrup avatar Mar 05 '25 03:03 keithbentrup

Does latest main commit help? #85

ValueRaider avatar Mar 05 '25 09:03 ValueRaider