【问题标题】:How does PyCharm create a virtualenv that works with Matplotlib on macOS?PyCharm 如何在 macOS 上创建与 Matplotlib 一起使用的 virtualenv?
【发布时间】:2019-03-13 00:17:16
【问题描述】:

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

$ 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,使用“创建新项目”-“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


【解决方案1】:

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

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

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

如果您使用 venv 命令在终端中创建虚拟环境,它应该可以正常工作。

【讨论】:

    猜你喜欢
    • 2018-05-29
    • 1970-01-01
    • 2020-06-10
    • 2019-11-27
    • 2017-03-20
    • 2023-01-14
    • 2018-05-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多