【发布时间】:2019-03-13 00:17:16
【问题描述】:
我在 macOS 10.14.3 上使用通过 Homebrew 安装的 Python 3,并且我已经安装了 virtualenv 和 pip:
$ 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.
现在我注意到,如果我使用 PyCharm 创建一个 virtualenv,使用“创建新项目”-“Pure Python”并将“项目解释器”设置为“使用 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 包,所以我不知道他们有什么不同。
【问题讨论】:
-
我尝试了复制,但我也遇到了 PyCharm 创建的 venv 的导入错误 - 但这可能是因为我使用的是 Python 版本管理器而不是
brew install python
标签: python matplotlib pycharm virtualenv