Python2-QT绑定

2024-09-29 18:33:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在docker容器中运行一些python代码,定义如下:

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

在我的一个脚本中,将matplotlib导入为:

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

原因:

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

鉴于:

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

有什么建议吗?为什么?后者有效,但不会通过PEP8验证,因为它会引发:

E402 module level import not at top of file

Tags: installruninpyimportenvgetmatplotlib
1条回答
网友
1楼 · 发布于 2024-09-29 18:33:25

由于matplotlib API特定,不可能轻松实现您想要的。以下是相关的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.

所以,你可以试着在这种特殊情况下抑制PEP8,好吧,这是可能的

相关问题 更多 >

    热门问题