http://groups.google.com/group/python-virtualenv/browse_thread/thread/83fa4a12d22a30c8/744e19c194f1618a?#744e19c194f1618a详细说明了如何在 OSX 上以 virtualenvs 的方式为 python 启用 wxpython,目前它与 wincy3 类比安装模式。
伊恩之前在这些方面的讨论在这里:
http://groups.google.com/group/python-virtualenv/browse_thread/thread/b119a3c26aa49238
此处列出了目前执行此操作的代码:
http://github.com/gldnspud/virtualenv-pythonw-osx/
我们可以将其融入核心发行版吗? 这种方法需要改变什么?
+1 对此。
想要链接的“.Python”目录是什么
http://github.com/gldnspud/virtualenv-pythonw-osx/ ? 我没有看到任何.Python
我的 venv。
相反,我看到了这个:
(py27) $ laf /Users/glind/venvs/py27//include/python2.7
/Users/glind/venvs/py27//include/python2.7@ -> /usr/local/Cellar/python/2.7.2/include/python2.7
(py27) $ which python
/Users/glind/venvs/py27/bin/python
此错误以多种方式表现出来:
http://stackoverflow.com/questions/3692928/why-doesnt-the-save-button-work-on-a-matplotlib-plot
http://code.google.com/p/iterm2/issues/detail?id=1680
没有.Python
原因是因为在我最初我是从brew install python
venving 的。 只有brew install python --framework
和其他框架安装才能获得.Python
dynlib。
有办法解决这个问题吗? 我仍然有害羞的 Matplotlib 窗口的问题
这个问题已经有 4.5 年的历史了,需要丑陋的黑客才能让 matplotlib 在 Mac OSX 上运行。 我们能解决这个问题吗?
我支持这个。 我安装了 python 2.7,然后使用以下命令创建了 virtualenv:
brew install python --framework
virtualenv env
source env/bin/activate
pip install matplotlib
但是当我尝试导入matplotlib.pyplot
时,我收到以下消息。
Python 2.7.10 (default, Aug 22 2015, 20:33:39)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.1)] on darwin
>>> import matplotlib.pyplot as plt
...
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. If you are Working with Matplotlib in a virtual enviroment see 'Working with Matplotlib in Virtual environments' in the Matplotlib FAQ
@nils-werner 提到的解决方法有效。 但对于想要在 virtualenv 的 jupyter notebook 中使用 matplotlib(不仅)的初学者来说,它是一个巨大的障碍。
我写了如何解决这个问题的详细指南, http://blog.rousek.name/2015/11/29/adventure-with-matplotlib-virtualenv-and-macosx/
呃.......我也只是在升级到 matplotlib 1.5(现在无法导入后端,如上面@stlk 所记录的)之后点击了这个。 我正在使用 brew 安装的 python 3.5。
正在使用 python 章节进行可视化介绍,我什至无法开始在 jupyter 中编写第一个代码示例,因为我一直在 mac 上遇到这个问题......我正在使用 mac + virtualenv 附带的系统 python .
+1
+1
天啊....
2011 年的问题.. 仍未修复。
与此同时,在这个问题(和常见问题解答)中链接的实用程序仍然对我有用:
$ # install the utility
$ pip install git+https://github.com/gldnspud/virtualenv-pythonw-osx.git
$ # enter the virtualenv with virtualenvwrapper (or manually)
$ workon my-venv
$ # double-check that this is your venv Python binary
$ which python
/Users/macbook/.virtualenvs/my-venv/bin/python
$ # fix it, using magic
$ fix-osx-virtualenv `which python`/../..
是的,这很简单,但我花了一段时间才弄明白。 我不知道这是做什么的,但结果 Matplotlib 只需使用 venv Python 二进制文件调用它就可以工作——不需要包装壳函数。
我知道这个解决方案,但是:
感觉这不是一个理想的解决方案,但一个简单的解决方法是使用virtualenvwrapper ,然后使用钩子进行配置。 安装 virtualenv-pythonw-osx 和 virtualevnwrapper,获取 virtualenvwrapper.sh 文件以创建钩子脚本,然后将以下内容添加到postactivate
脚本中:
if [ ! -d $VIRTUAL_ENV/Python.app ]; then
echo Fixing OSX Python display issues...
fix-osx-virtualenv $VIRTUAL_ENV
fi
当您通过workon
命令激活它们时,这将自动修复任何虚拟环境
+1
+1
+1
也刚碰到这个bug。 建议的解决方法并不完全优雅。 能修好就好了!
尝试更改 matplotlib 后端以使 matplotlib 在虚拟环境中工作:
我使用了(至少)两种解决方案,下面还包含一些其他方法。 这里概述的两者都应该以略有不同的方法来处理同一件事:
如果您已经在虚拟环境中使用 pip 安装了 matplotlib,则可以...
_1. 为您的虚拟环境添加一个新的 matplotlibrc 文件_
例如,在您的虚拟环境中运行:
$ cd ~/.matplotlib
$ nano matplotlibrc #to create file using nano
在文件中写入backend: TkAgg
并在退出时保存。 你应该很高兴去。
_OR _
_2. 编辑你的虚拟环境引用的 matplotlibrc 文件_
在您的虚拟环境中,打开一个 python shell 并运行:
import matplotlib
matplotlib.matplotlib_fname()
'/home/foo/.config/matplotlib/matplotlibrc'
注意/更新:如果您在虚拟环境中遇到语法错误,您可能只需要运行:
import matplotlib
matplotlib.matplotlib_fname()
这将显示您的虚拟环境中的 matplotlib 包引用的 matplotlibrc 文件。 按照路径打开 matplotlibrc 文件。 编辑文件的backend
标签以读取backend: TkAgg
和save
[另外- _第三种方法_]
以前没有尝试过,但根据文档,如果您不想更改脚本本身的后端,您应该能够在导入 matplotlib 时定义后端:
import matplotlib
matplotlib.use('TkAgg')
(有关进一步的解释和其他方法,请参阅下面的来源)
资料来源:
[1] http://matplotlib.org/faq/usage_faq.html#what -is-a-backend
[2] http://matplotlib.org/users/customizing.html#customizing -matplotlib
[3] http://stackoverflow.com/questions/4130355/python-matplotlib-framework-under-macosx
[4] http://stackoverflow.com/questions/29433824/unable-to-import-matplotlib-pyplot-as-plt-in-virtualenv
@wwp3写得很好,谢谢! 我对这个选项很幸运:
import matplotlib
matplotlib.use('TkAgg')
正在为此问题改进 Matplotlib 文档@stlk任何反对使用您的指南的一部分来修复 Jupyter 笔记本问题(我们已经涵盖了终端 python/ipython)
@jenshnielsen随意使用它,我很高兴它有帮助。
+1
这解决了吗? 如果是这样,有人可以关闭问题吗?
它不是
绝对不。 它比以往任何时候都更糟,因为它在import
上崩溃
virtualenv test
source test/bin/activate
pip install matplotlib
python -c "import matplotlib.pyplot; print 'import succeeded'"
有解决这个问题的 PR 吗? 如果是这样,遇到问题的人是否对其进行了测试并确认它解决了问题? 如果没有,有人在研究吗? 对于无法重现该问题的任何人(我猜这至少需要安装 OSX 才能完成),开发修复程序或多或少是不可能的,因此我们在这里依靠贡献。
@nils-werner 感谢您对如何复制的清晰描述。 如果您可以添加环境详细信息(我在线程中看到的所有内容都是“OSX”,但没有具体说明是否是所有版本,virtualenv 和 python 的版本以及它们的安装方式,是否需要安装某些其他东西,如何安装安装它们等)也会有帮助。
然后希望可以访问合适环境的人可以制作 PR 并在此处引用。 在那个阶段,我们可以审查 PR 并从那里获取它。
@pfmoore我一直在使用我之前评论中的解决方法,它仍然对我有用。 – 虽然我不确定_how_是否有效。 另请参阅@nils-werner 的以下评论。
(记录:OS X 10.11.6、Python 3.5.2、matplotlib-1.5.3。)
@nils-werner 更糟糕的唯一原因是我们在 Matplotlib 中运行 OSX 后端之前明确检查框架构建https://github.com/matplotlib/matplotlib/blob/master/src/_macosx.m#L3071
另一种选择是软故障,其中 gui 仅部分功能。
WXPython 有一个类似的检查构建,它可以防止您从非框架构建中使用 WXPython。
如果您使用的是 python3,最好的解决方法是使用标准库中的 venv
与正常工作的 virtualenv 不同。
http://matplotlib.org/devdocs/faq/osx_framework.html#introduction
好的,所以我想如果没有人在做 PR,那么这里的解决方案是:
venv
凉爽的。 我不确定在这种情况下是否值得让这个问题悬而未决 - 有一个明确的解决方法,无论如何,任何代码修复都将是一个新的 PR。 我建议我们关闭问题 - 评论?
有任何更新吗? 解决这个问题而不是使用变通方法会很好。
@wwp3太好了,谢谢! 我用这个选项成功修复了它:
$ cd ~/.matplotlib
$ nano matplotlibrc #使用nano创建文件
在文件中写入后端:TkAgg 并在退出时保存。 你应该很高兴去。
@stlk很有帮助谢谢
此问题已自动标记为陈旧,因为它最近没有活动。 如果没有进一步的活动发生,它将被关闭。 如果您想让它保持打开状态,只需添加评论。 感谢你的贡献。
尝试更改 matplotlib 后端以使 matplotlib 在虚拟环境中工作:
我使用了(至少)两种解决方案,下面还包含一些其他方法。 这里概述的两者都应该以略有不同的方法来处理同一件事:
如果您已经在虚拟环境中使用 pip 安装了 matplotlib,则可以...
_1. 为您的虚拟环境添加一个新的 matplotlibrc 文件_
例如,在您的虚拟环境中运行:$ cd ~/.matplotlib $ nano matplotlibrc #to create file using nano
在文件中写入
backend: TkAgg
并在退出时保存。 你应该很高兴去。_ _或__
_2. 编辑你的虚拟环境引用的 matplotlibrc 文件_
在您的虚拟环境中,打开一个 python shell 并运行:import matplotlib matplotlib.matplotlib_fname() '/home/foo/.config/matplotlib/matplotlibrc'
注意/更新:如果您在虚拟环境中遇到语法错误,您可能只需要运行:
import matplotlib matplotlib.matplotlib_fname()
这将显示您的虚拟环境中的 matplotlib 包引用的 matplotlibrc 文件。 按照路径打开 matplotlibrc 文件。 编辑文件的
backend
标签以读取backend: TkAgg
和save
[另外- _第三种方法_]
以前没有尝试过,但根据文档,如果您不想更改脚本本身的后端,您应该能够在导入 matplotlib 时定义后端:import matplotlib matplotlib.use('TkAgg')
(有关进一步的解释和其他方法,请参阅下面的来源)
资料来源:
[1] http://matplotlib.org/faq/usage_faq.html#what -is-a-backend
[2] http://matplotlib.org/users/customizing.html#customizing -matplotlib
[3] http://stackoverflow.com/questions/4130355/python-matplotlib-framework-under-macosx
[4] http://stackoverflow.com/questions/29433824/unable-to-import-matplotlib-pyplot-as-plt-in-virtualenv
你的第一个解决方案太棒了,解决了我的问题。 谢谢!
最有用的评论
尝试更改 matplotlib 后端以使 matplotlib 在虚拟环境中工作:
我使用了(至少)两种解决方案,下面还包含一些其他方法。 这里概述的两者都应该以略有不同的方法来处理同一件事:
如果您已经在虚拟环境中使用 pip 安装了 matplotlib,则可以...
_1. 为您的虚拟环境添加一个新的 matplotlibrc 文件_
例如,在您的虚拟环境中运行:
在文件中写入
backend: TkAgg
并在退出时保存。 你应该很高兴去。_OR _
_2. 编辑你的虚拟环境引用的 matplotlibrc 文件_
在您的虚拟环境中,打开一个 python shell 并运行:
注意/更新:如果您在虚拟环境中遇到语法错误,您可能只需要运行:
这将显示您的虚拟环境中的 matplotlib 包引用的 matplotlibrc 文件。 按照路径打开 matplotlibrc 文件。 编辑文件的
backend
标签以读取backend: TkAgg
和save
[另外- _第三种方法_]
以前没有尝试过,但根据文档,如果您不想更改脚本本身的后端,您应该能够在导入 matplotlib 时定义后端:
(有关进一步的解释和其他方法,请参阅下面的来源)
资料来源:
[1] http://matplotlib.org/faq/usage_faq.html#what -is-a-backend
[2] http://matplotlib.org/users/customizing.html#customizing -matplotlib
[3] http://stackoverflow.com/questions/4130355/python-matplotlib-framework-under-macosx
[4] http://stackoverflow.com/questions/29433824/unable-to-import-matplotlib-pyplot-as-plt-in-virtualenv