PyCharm如何在macOS上创建与Matplotlib一起使用的virtualenv?

问题描述 投票:3回答:1

我使用通过Homebrew安装的Python 3在macOS 10.14.3上,并且我已经使用virtualenv安装了pip

$ brew install python
$ pip3 install virtualenv

现在,以下问题是well documentedknown 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.

现在我注意到如果我使用“创建新项目”创建一个带有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工作? Their docs声称他们也在使用virtualenv包,所以我不知道他们的做法有何不同。

python matplotlib pycharm virtualenv
1个回答
1
投票

https://matplotlib.org/faq/osx_framework.html

解决方案是不使用virtualenv,而是使用stdlib的venv,它提供类似的功能但没有出现这个问题。

这是使用virtualenv命令创建的虚拟环境的错误。 PyCharm使用上面文档中推荐的venv命令。

如果使用venv命令在终端中创建虚拟环境,它应该没有问题。

© www.soinside.com 2019 - 2024. All rights reserved.