我在 macOS 10.14.3 上使用通过 Homebrew 安装的 Python 3,并且我已经使用 pip
安装了 virtualenv
:
$ brew install python
$ pip3 install virtualenv
现在下面的问题是well documented和 known not to work :
$ virtualenv venv
$ source venv/bin/activate
$ pip install matplotlib ipython
$ ipython
In [1]: %matplotlib
ImportError: 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 using (Ana)Conda please install
python.app and replace the use of 'python' with 'pythonw'. See 'Working with
Matplotlib on OSX' in the Matplotlib FAQ for more information.
现在我注意到,如果我使用“创建新项目”-“纯 Python”和“项目解释器”设置为“使用 Virtualenv 的新环境”使用 PyCharm 创建一个 virtualenv,我可以成功地将 Matplotlib 与任何已安装的后端一起使用。假设使用 PyCharm 创建的 virtualenv 位于文件夹 venv2
中:
$ source venv2/bin/activate
$ pip install matplotlib ipython
$ ipython
In [1]: %matplotlib
Using matplotlib backend: MacOSX
为什么会这样? PyCharm 如何创建 virtualenv 以便 Matplotlib 工作? Their docs声明他们也在使用 virtualenv
包,所以我不知道他们有什么不同。
最佳答案
参见 https://matplotlib.org/faq/osx_framework.html
解决方案是不使用 virtualenv,而是使用 stdlib 的 venv,它提供类似的功能但没有出现此问题。
这是使用virtualenv 命令创建的虚拟环境的错误。 PyCharm 使用上面文档中推荐的 venv 命令。
如果您使用 venv 命令在终端中创建虚拟环境,它应该可以正常工作。
关于python - PyCharm 如何在 macOS 上创建一个与 Matplotlib 一起工作的 virtualenv?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54998038/