ImportError Cannot import name 'warnings' from 'matplotlib.dates - matplotlib

Alpaca backtrader plot issue: I ran into this import issue and found this article, so I applied the code, but same issue not resolved. any one can help please?
My installed matplotlib version is 3.3.1
backtrader 1.9.76.123
python 3.8.5
the entire code posted below:
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)
import alpaca_backtrader_api
import backtrader as bt
from datetime import datetime
#import matplotlib
ALPACA_API_KEY = "XXXXX"
ALPACA_SECRET_KEY = "XXXX"
ALPACA_PAPER = True
class SmaCross(bt.SignalStrategy):
def init(self):
sma1, sma2 = bt.ind.SMA(period=10), bt.ind.SMA(period=30)
crossover = bt.ind.CrossOver(sma1, sma2)
self.signal_add(bt.SIGNAL_LONG, crossover)
cerebro = bt.Cerebro()
cerebro.addstrategy(SmaCross)
store = alpaca_backtrader_api.AlpacaStore( key_id=ALPACA_API_KEY,secret_key=ALPACA_SECRET_KEY,paper=ALPACA_PAPER)
if not ALPACA_PAPER:
broker = store.getbroker() # or just alpaca_backtrader_api.AlpacaBroker()
cerebro.setbroker(broker)
DataFactory = store.getdata # or use alpaca_backtrader_api.AlpacaData
data0 = DataFactory(dataname='AAPL', historical=True, fromdate=datetime(2015, 1, 1), timeframe=bt.TimeFrame.Days)
cerebro.adddata(data0)
print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
cerebro.run()
print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
cerebro.plot()

Downgrade to matplotlib 3.2.2 until the bug in backtrader is fixed.
Here is the fix pull request: https://github.com/mementum/backtrader/pull/418.
pip uninstall matplotlib # or conda
pip install matplotlib==3.2.2

I suffered the same problem like you did, your link provided has the perfect solution. just get rid of warnings from locator.py
https://community.backtrader.com/topic/981/importerror-cannot-import-name-min_per_hour-when-trying-to-plot/8

I couldn't install matplotlib==3.2.2 nor the patch without uninstalling backtrader first.
So, this worked for me in the end:
Uninstall backtrader:
pip uninstall backtrader
Install the patch provided in the above solution:
pip install git+https://github.com/mementum/backtrader.git#0fa63ef4a35dc53cc7320813f8b15480c8f85517#egg=backtrader
If necessary, install matplotlib again:
pip install matplotlib

As pointed out above, the issue is addressed in this pull request and the patch is the latest commit to master, but there hasn't been a release since 2019-05.
You can install the patched version like so:
pip install git+https://github.com/mementum/backtrader.git#0fa63ef4a35dc53cc7320813f8b15480c8f85517#egg=backtrader
You could alternatively specify the required commit in requirements.txt like so:
-e git+https://github.com/mementum/backtrader.git#0fa63ef4a35dc53cc7320813f8b15480c8f85517#egg=backtrader
…then pip install -r requirements.txt
After installing with either method, you can confirm the versions installed with pip freeze:
...
backtrader==1.9.76.123
...
How to install from git

Mac Big Sur
for me it only worked if:
Downgrade python3.9 to python 3.8
then I downgraded matplotlib==3.2.2

For both python 3.8.x and 3.9.x, I solved the problem by using specific version of matplotlib==3.2.2
pip install matplotlib==3.2.2
By default, I used matplotlib==3.4.x version and the problem occured.

All of the above answers are fine. The problem is not with Matplotlib though. The Backtrader library hasn't kept up with the Matplotlib updates. You can do the off-label Backtrader update suggested by Joel Brigate above...or you can make a simple mod to locator.py file (backtrader.plot):
Just change:
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)
to:
from matplotlib import warnings
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)
You'll note that the warnings import now comes directly out of matplotlib rather than matplotlib.dates. This is the offending issue within locator.py.

Here is my solution:
python -m pip uninstall matplotlib
python -m pip uninstall backtrader
python -m pip install backtrader
python -m pip install matplotlib==3.2.2
Enjoy!

I could not install matplotlib==3.2.2 with python 3.9 .
Here is how did I fix this issue:
$ pip uninstall backtrader
$ pip install git+https://github.com/mementum/backtrader.git#0fa63ef4a35dc53cc7320813f8b15480c8f85517#egg=backtrader
Reference:
Github: Fix ImportError from matplotlib.dates
#418

Mac Big Sur. I did the same: python 3.8.5, uninstall matplotlib, install matplotlib==3.2.2
I'm new at this so I first tried the easy way, through anaconda.org, but could not find version 3.2.2. Then tried it from the Jupyter notebook with conda install... didn't work. I finally did it straight through terminal, which worked fine.

#laffuste solution of downgrading to version 3.2.2 of matplotlib solved the issue for me. PR to fix the issue is still open, you can also follow this forum for more info on the problem:

Related

Can't import pytorch_lightning on Google colab

I'm using !pip install git+https://github.com/PyTorchLightning/pytorch-lightning
but when I'm importing pytorch_lightning I get the following error:
ImportError: cannot import name '_RequirementAvailable' from 'pytorch_lightning.utilities.imports' (/usr/local/lib/python3.7/dist-packages/pytorch_lightning/utilities/imports.py)
It all worked fine a week ago...
When you install Lightning as
pip install git+https://github.com/PyTorchLightning/pytorch-lightning
you get the latest version in development. It is not an official release. I recommend installing
pip install pytorch-lightning
to get the latest stable release. I suspect this will solve your import error.

Install TensorFlow addons

I have a venv with the following details:
python 3.6
TensorFlow 2.0.0
I tried to install tensorflow-addons using the following:
pip install -q --no-deps tensorflow-addons~=0.6
But then I keep receiving the following error:
Could not find a version that satisfies the requirement tensorflow-addons~=0.6 (from versions: )
No matching distribution found for tensorflow-addons~=0.6
You are using pip version 18.0, however version 19.3.1 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.
I also tried other versions of tensorflow-addons, e. g., 0.4.0, 0.5.0, ..., but it did not work out.
I came across this problem two times and each time I had to solve the problem with a different solution.
1. Solution:
Upgrade pip/pip3 by using the following command.
python3 -m pip install --upgrade pip
Select appropriate version of the tensorflow-addons using the
following link
https://github.com/tensorflow/addons#python-op-compatibility-matrix
Install using the following command
pip install tensorflow-addons==version
2. Solution:
go to https://pypi.org/project/tensorflow-addons/#history
click on appropriate version
click on "Download files" on menu to the left
click on a .whl file that matches your system
requirements/specifications
go to the directory where you download the .whl file and run the
following
pip install tensorflow_addons-name.whl
The problem appears to have been related to installing on Windows platforms in the earlier versions of tensorflow-addons. As of time of updating this comment this issue should disappear completely.
In fact the developers state it has been solved, as it is shown here:
FYI stable release for windows is out. pip install tensorflow-addons
https://github.com/tensorflow/addons/issues/173#issuecomment-573106184
At your command prompt, simply specify the version you want to install.
For me, my python version is 3.7.4 and Tensorflow version is 2.2.0
Therefore, the tensorflow-addons version that matches my python and tensorflow version is 0.10.0
pip install tensorflow-addons==0.10.0

Error installing pandas with pip: Could not find a version that satisfies the requirement numpy==1.9.3

I'm trying to install pandas. When I run: pip install pandas in cmd, I get the following error message: Could not find a version that satisfies the requirement numpy==1.9.3. Not sure how to fix this.
It's likely you have a different version of numpy installed, try upgrade numpy first with:
pip install numpy==1.9.3 --upgrade
then run pip install pandas. Also check this github issue. Maybe your python version is not supported.

Issue while installing Tensorflow

While installing tensorflow(CPU only) I am facing below error :
>>> import tensorflow as tf
Traceback (most recent call last): File "", line 1, in
ImportError: No module named 'tensorflow'
I have python version 3.5
Did you first try:
pip3 install --upgrade tensorflow
It's maybe due to some missing .py files. Download the package from Github link and paste it on python35/Lib/site-packages
import tensorflow as tf
invokes tensorflow not install it there is great article how to install on tensorflow website.
You have not mentioned platform/OS you working on also steps taken if any before trying this command.
best thing for beginners I feel is Anaconda which is suit of packages required for data science.
Give it a try.
python3 -m pip install --upgrade https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-0.12.0-py3-none-any.whl
You can search for the appropriate version you wish to install and update in the above mentioned url.
Try to install an old version of Tensorflow as:
pip install tensorflow==1.5.0
After installing the old version retry with the newest version, for example:
pip install tensorflow==1.9.0

installing numpy for python 3.1.2 on Ubuntu 10.04

I've searched everywhere I could and I couldn't find appropriate answer. I don't know how to install numpy so I could use it in Geany with python 3.1.2. It only works for python 2.6.5. I'm new to ubuntu.
edit: I get ImportError: No module named numpy
If python 3.1.2 is installed via python3 package then you could try:
$ sudo apt-get install python3-numpy
Tried the apt-get installation solution above but it didn't work, however, pip did work:
$ sudo pip3 install numpy
Also, here are the install instructions for pip if needed.