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

1
  • 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. Commented Jul 20, 2020 at 16:08

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

9 Comments

Exactly. Thanks for the info
any idea how long it usually takes for something like this to be merged? Thanks
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 : community.backtrader.com/topic/2553/backtrader-s-future You can find discussion to the issue you experience here : community.backtrader.com/topic/2784/…
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"
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 stackoverflow.com/questions/48504746/…
|
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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.