4

I am using a plotting function that utilizes matplotlib and all of a sudden, it has stopped working and is returning the following error.

from matplotlib.dates import (HOURS_PER_DAY, MIN_PER_HOUR, SEC_PER_MIN,
ImportError: cannot import name 'warnings' from 'matplotlib.dates'

Is there a known fix for this?

Thanks

Ryan Reid
  • 189
  • 1
  • 3
  • 9
  • 2
    You are going to have a difficult time finding help if you tell us the error happened "all of a sudden" instead of showing the code. – M-Wi Jul 20 '20 at 16:08

2 Answers2

5

Let me guess ... You are using backtrader and had this issue while using it. The error you have is :

File "/xxx/backtrader/plot/locator.py", line 35, in <module>
from matplotlib.dates import (HOURS_PER_DAY, MIN_PER_HOUR, SEC_PER_MIN,
ImportError: cannot import name 'warnings' from 'matplotlib.dates'    

The root cause of the error is that at line 35 of above mentionned file (locator.py) there is an attempt to import "warnings" from matplotlib.dates :

from matplotlib.dates import (HOURS_PER_DAY, MIN_PER_HOUR, SEC_PER_MIN,
                          MONTHS_PER_YEAR, DAYS_PER_WEEK,
                          SEC_PER_HOUR, SEC_PER_DAY,
                          num2date, rrulewrapper, YearLocator,
                          MicrosecondLocator, warnings)    

But if you look at the doc or the code, you will find that there is no warnings in matplotlib.dates. Digging in this file history on github, one can find that a line :

import warnings   

have been removed on 7 sept 2019, this is probably the reason of the issue you noticed. In Backtrader, there is a pull request awaiting for this issue : https://github.com/mementum/backtrader/pull/418

You can either apply the fix yourself to the code if you forked it, or wait for the PR to be merged

FFF
  • 66
  • 3
  • Exactly. Thanks for the info – Ryan Reid Jul 21 '20 at 20:32
  • any idea how long it usually takes for something like this to be merged? Thanks – Ryan Reid Jul 21 '20 at 20:35
  • Well, no. It's complicated : the only maintainer of backtrader apply only fix, and on an very irregular basis. Some users are thinking of forking & making a new version (backtrader2) You can find discussion about this here : https://community.backtrader.com/topic/2553/backtrader-s-future You can find discussion to the issue you experience here : https://community.backtrader.com/topic/2784/cannot-import-name-warnings-from-matplotlib-dates – FFF Jul 23 '20 at 11:26
  • Seems that if you want to fix this issue somehow quickly, you would have to do it yourself, on your computer, in file "backtrader/plot/locator.py" - line 39 : remove ", warnings". - line 30 : under "import datetime" add a new line with "import warnings" – FFF Jul 23 '20 at 11:27
  • Is there a way to revert to a previous version of backtrader/matplotlib that would also allow this to work? @FFF – Ryan Reid Jul 23 '20 at 15:34
  • Reverting to a previous version of backtrader would not solve this issue. According to the matplotlib version history : https://pypi.org/project/matplotlib/#history the version prior to 7 sept. 2019 still have the "import warnings" line. So reverting to matplotlib version 3.1.1 should do the trick (at price of possible matplotlib bugs linked to the previous version) If you try it, please post if you are successful or not so that other facing the same issue in the future and willing to follow the same workaround procedure would have the answer. – FFF Jul 23 '20 at 18:15
  • I made the recommended changes to the backtrader/plot/locator.py but now I am getting another error. ModuleNotFoundError: No module named '_tkinter'. Is there any additional code that has to be changed? – Ryan Reid Jul 27 '20 at 18:19
  • No. Looks like your error is not linked to backtrader. but to your python install or to matplotlib. Maybe you should provide more details, or even better, search on stackoverflow for this error. It seems there is a lot of answers about this issue. – FFF Jul 28 '20 at 22:00
  • 2
    Hi guys, Just for posterity, I had this issue as well and reverted to matplotlib 3.1.1 (by doing `pip uninstall matplotlib` and `pip install matplotlib=3.1.1` and it worked!! As for the _tkinter issue, that is separate from this specific matplotlib issue - you need to install the tkinter package - see https://stackoverflow.com/questions/48504746/importerror-libtk8-6-so-cannot-open-shared-object-file-no-such-file-or-direct – squeegene Aug 19 '20 at 13:30
1

I had the same issue... Am using python-3.6.13, conda-4.9.2, backtrader-1.9.76.123.dist-info.

It seems matplotlib version I was using (matplotlib-3.3.4) were the issue, even importing warnings (import warnings) did not resolve.

The solution for me was:
pip uninstall matplotlib
pip install matplotlib==3.2.2

Hope to help, thank you!

Erni Souza
  • 260
  • 1
  • 6