[英]How does PyCharm create a virtualenv that works with Matplotlib on macOS?
我使用通过Homebrew安装的Python 3在macOS 10.14.3上,我已经用pip
安装了virtualenv
:
$ brew install python
$ pip3 install virtualenv
$ 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.
现在我注意到如果我使用“创建新项目”创建一个带有PyCharm的virtualenv - “纯Python”并将“Project Interpreter”设置为“使用Virtualenv的新环境”,我可以成功地将Matplotlib与任何已安装的后端一起使用。 假设使用PyCharm创建的virtualenv驻留在文件夹venv2
:
$ source venv2/bin/activate
$ pip install matplotlib ipython
$ ipython
In [1]: %matplotlib
Using matplotlib backend: MacOSX
为什么这样做? PyCharm如何创建virtualenv以便Matplotlib工作? 他们的文档说他们也在使用virtualenv
包,所以我不知道他们做了什么不同的事情。
请参阅https://matplotlib.org/faq/osx_framework.html
解决方案是不使用virtualenv,而是使用stdlib的venv,它提供类似的功能但没有出现这个问题。
这是使用virtualenv命令创建的虚拟环境的错误。 PyCharm使用上面文档中推荐的venv命令。
如果使用venv命令在终端中创建虚拟环境,它应该没有问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.