0

I am running some python code in a docker container, defined as:

FROM continuumio/anaconda2:5.3.0
USER root
RUN apt-get install git
ENV AWS_DEFAULT_REGION us-east-2

# Copying code in container
RUN mkdir /warburg-investigation

COPY . /warburg-investigation
RUN apt-get update

# Installing necessary packages
RUN pip install panaxea

# Launching
ENV PYTHONPATH "${PYTHONPATH}:/warburg-investigation"
RUN cd warburg-investigation; python Main.py --pysparse

In one of my scripts, importing matplotlib as:

import matplotlib
import matplotlib.pyplot as plt
matplotlib.use("Agg")
plt.switch_backend("agg")

Causes:

File "/opt/conda/lib/python2.7/site-packages/matplotlib/backends/qt_compat.py", line 158, in <module>

    raise ImportError("Failed to import any qt binding")

ImportError: Failed to import any qt binding

Whereas as:

import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
plt.switch_backend("agg")

Any advice why? The latter works but won't pass PEP8 validation as as it raises:

E402 module level import not at top of file
MrD
  • 4,986
  • 11
  • 48
  • 90
  • Check [this](https://stackoverflow.com/questions/45346575/what-does-noqa-mean-in-python-comments) and [this](http://flake8.pycqa.org/en/3.1.1/user/ignoring-errors.html) for the issue of passing pep8 – ImportanceOfBeingErnest Mar 09 '20 at 18:03

1 Answers1

0

It's not possible to achieve what you want easily due to matplotlib API specific. Here is related doc:

If you use the use() function, this must be done before importing matplotlib.pyplot. Calling use() after pyplot has been imported will have no effect. Using use() will require changes in your code if users want to use a different backend. Therefore, you should avoid explicitly calling use() unless absolutely necessary.

So, you could try to suppress PEP8 for this particular case, afaik it's possible.

slesh
  • 1,902
  • 1
  • 18
  • 29