PyCharm 如何在 macOS 上创建一个与 Matplotlib 一起工作的 virtualenv?
How does PyCharm create a virtualenv that works with Matplotlib on macOS?
我在 macOS 10.14.3 上使用通过 Homebrew 安装的 Python 3,我已经安装 virtualenv
和 pip
:
$ brew install python
$ pip3 install virtualenv
现在下面的问题是well documented and 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.
现在我注意到,如果我使用 "Create New Project" 创建一个带有 PyCharm 的 virtualenv - "Pure Python" 并将 "Project Interpreter" 设置为 "New environment using 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 命令在终端中创建虚拟环境,它应该可以正常工作。
我在 macOS 10.14.3 上使用通过 Homebrew 安装的 Python 3,我已经安装 virtualenv
和 pip
:
$ brew install python
$ pip3 install virtualenv
现在下面的问题是well documented and 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.
现在我注意到,如果我使用 "Create New Project" 创建一个带有 PyCharm 的 virtualenv - "Pure Python" 并将 "Project Interpreter" 设置为 "New environment using 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 命令在终端中创建虚拟环境,它应该可以正常工作。