Python 无法在 virtualenv 中“将 matplotlib.pyplot 作为 plt 导入”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29433824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unable to "import matplotlib.pyplot as plt" in virtualenv
提问by Rohit
I am working with flask in a virtual environment. I was able to install matplotlib with pip, and I can import matplotlib
in a Python session. However, when I import it as
我正在虚拟环境中使用烧瓶。我能够使用 pip 安装 matplotlib,并且可以import matplotlib
在 Python 会话中安装。但是,当我将其导入为
matplotlib.pyplot as plt
I get the following error:
我收到以下错误:
>>> import matplotlib.pyplot as plt
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "//anaconda/envs/myenv/lib/python2.7/site-packages/matplotlib/pyplot.py", line 109, in <module>
_backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
File "//anaconda/envs/myenv/lib/python2.7/site-packages/matplotlib/backends/__init__.py", line 32, in pylab_setup
globals(),locals(),[backend_name],0)
File "//anaconda/envs/myenv/lib/python2.7/site-packages/matplotlib/backends/backend_macosx.py", line 24, in <module>
from matplotlib.backends import _macosx
RuntimeError: Python is not installed as a framework. The Mac OS X backend will not be able to function correctly if Python is not installed as a framework. See the Python documentation for more information on installing Python as a framework on Mac OS X. Please either reinstall Python as a framework, or try one of the other backends.
I am confused about why it asks me to install Python as framework. Doesn't it already exists? What does it mean to "install Python as framework", and how do I install it?
我很困惑为什么它要求我安装 Python 作为框架。不是已经存在了吗?“将 Python 安装为框架”是什么意思,我该如何安装?
采纳答案by Hercules
This solutionworked for me. If you already installed matplotlib using pip on your virtual environment, you can just type the following:
这个解决方案对我有用。如果您已经在虚拟环境中使用 pip 安装了 matplotlib,则只需键入以下内容:
$ cd ~/.matplotlib
$ nano matplotlibrc
And then, write backend: TkAgg
in there.
If you need more information, just go to the solution link.
然后,backend: TkAgg
在那里写。如果您需要更多信息,只需转到解决方案链接。
回答by user1718097
I had similar problem when I used pip to install matplotlib. By default, it installed the latest version which was 1.5.0. However, I had another virtual environment with Python 3.4 and matplotlib 1.4.3 and this environment worked fine when I imported matplotlib.pyplot. Therefore, I installed the earlier version of matplotlib using the following:
当我使用 pip 安装 matplotlib 时,我遇到了类似的问题。默认情况下,它安装了最新版本 1.5.0。但是,我有另一个使用 Python 3.4 和 matplotlib 1.4.3 的虚拟环境,当我导入 matplotlib.pyplot 时,这个环境运行良好。因此,我使用以下命令安装了早期版本的 matplotlib:
cd path_to_virtual_environment # assume directory is called env3
env3/bin/pip install matplotlib==1.4.3
I know this is only a work-around, but it worked for me as a short-term fix.
我知道这只是一种解决方法,但它对我来说是一种短期解决方案。
回答by Bobby
Although most answers seem to point towards patching the activate
script to use the system python, I was having trouble getting that to work and an easy solution for me - though a little cringey - was to install matplotlib to the global environment and use that instead of a virtualenv instance. You can do this either by creating your virtualenv with the --system-site-packages flag like virtualenv --system-site-packages foo
, or to use the universal flag when pip installing like pip install -U matplotlib
.
尽管大多数答案似乎都指向修补activate
脚本以使用系统 python,但我在让它工作时遇到了麻烦,对我来说一个简单的解决方案 - 尽管有点尴尬 - 是将 matplotlib 安装到全局环境并使用它而不是一个虚拟环境实例。您可以通过使用 --system-site-packages 标志创建您的 virtualenv 来做到这一点,例如virtualenv --system-site-packages foo
,或者在 pip 安装时使用通用标志pip install -U matplotlib
。
回答by Jonathan
You can fix this issue by using the backend Agg
您可以使用后端解决此问题 Agg
Go to User/yourname/.matplotlib
and open/create matplotlibrc
and add the following line backend : Agg
and it should work for you.
转到User/yourname/.matplotlib
并打开/创建matplotlibrc
并添加以下行backend : Agg
,它应该适合您。
回答by JasonWayne
I got the same error, and tried Jonathan
's answer:
我遇到了同样的错误,并尝试了Jonathan
答案:
You can fix this issue by using the backend Agg
Go to
User/yourname/.matplotlib
and open/creatematplotlibrc
and add the following linebackend : Agg
and it should work for you.
您可以使用后端 Agg 解决此问题
转到
User/yourname/.matplotlib
并打开/创建matplotlibrc
并添加以下行backend : Agg
,它应该适合您。
I run the program, no error, but also no plots, and I tried backend: Qt4Agg
,
it prints out that I haven't got PyQt4 installed.
我运行程序,没有错误,但也没有绘图,我试过了backend: Qt4Agg
,它打印出我没有安装 PyQt4。
Then I tried another backend: backend: TkAgg
, it works!
然后我尝试了另一个后端:backend: TkAgg
,它有效!
So maybe we can try difference backends and some may work or install the requeired packages like PyQt4.
所以也许我们可以尝试不同的后端,有些可以工作或安装像 PyQt4 这样的需要的包。
Here is a sample python snippet that you can try and test matplotlib.
这是一个示例 python 片段,您可以尝试测试 matplotlib。
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [0, 3, 7])
plt.show()
回答by Pedro M Duarte
If you do not want to set a .matplotib/matplotlibrc
configuration file, you can circumvent this issue by setting the 'Agg'
backend at runtime right after importing matplotlib
and before importing matplotlib.pyplot
:
如果您不想设置.matplotib/matplotlibrc
配置文件,您可以通过'Agg'
在导入之后matplotlib
和导入之前在运行时设置后端来规避此问题matplotlib.pyplot
:
In [1]: import matplotlib
In [2]: matplotlib.use('Agg')
In [3]: import matplotlib.pyplot as plt
In [4]: fig, ax = plt.subplots(1, 1)
In [5]: import numpy as np
In [6]: x = np.linspace(-1., 1.)
In [7]: y = np.sin(x)
In [8]: ax.plot(x, y)
Out[8]: [<matplotlib.lines.Line2D at 0x1057ecf10>]
In [9=]: fig.savefig('myplot.png')
回答by Mapio
A clean and easy solution is to create a kernel that sets PYTHONHOME
to VIRTUAL_ENV
and then uses the system Python executable (instead of the one in the virtualenv).
一个干净而简单的解决方案是创建一个内核,该内核设置PYTHONHOME
为VIRTUAL_ENV
然后使用系统 Python 可执行文件(而不是 virtualenv 中的那个)。
If you want to automate the creation of such a kernel, you can use the jupyter-virtualenv-osxscript.
如果要自动创建这样的内核,可以使用jupyter-virtualenv-osx脚本。