简体   繁体   English

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

[英]How does PyCharm create a virtualenv that works with Matplotlib on macOS?

I'm on macOS 10.14.3 using Python 3 installed via Homebrew, and I have installed virtualenv with pip : 我使用通过Homebrew安装的Python 3在macOS 10.14.3上,我已经用pip安装了virtualenv

$ brew install python
$ pip3 install virtualenv

Now the following issue is 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.

Now I noticed that if I create a virtualenv with PyCharm using "Create New Project" - "Pure Python" with "Project Interpreter" set to "New environment using Virtualenv", I can successfully use Matplotlib with any installed backend. 现在我注意到如果我使用“创建新项目”创建一个带有PyCharm的virtualenv - “纯Python”并将“Project Interpreter”设置为“使用Virtualenv的新环境”,我可以成功地将Matplotlib与任何已安装的后端一起使用。 Say the virtualenv created with PyCharm resides in the folder venv2 : 假设使用PyCharm创建的virtualenv驻留在文件夹venv2

$ source venv2/bin/activate
$ pip install matplotlib ipython
$ ipython
In [1]: %matplotlib
Using matplotlib backend: MacOSX

Why does this work? 为什么这样做? How does PyCharm create the virtualenv so that Matplotlib works? PyCharm如何创建virtualenv以便Matplotlib工作? Their docs state that they are also using the virtualenv package, so I have no clue what they do differently. 他们的文档说他们也在使用virtualenv包,所以我不知道他们做了什么不同的事情。

See https://matplotlib.org/faq/osx_framework.html 请参阅https://matplotlib.org/faq/osx_framework.html

The solution is to not use virtualenv, but instead the stdlib's venv, which provides similar functionality but without exhibiting this issue. 解决方案是不使用virtualenv,而是使用stdlib的venv,它提供类似的功能但没有出现这个问题。

It is a bug with virtual environment created with virtualenv command. 这是使用virtualenv命令创建的虚拟环境的错误。 PyCharm uses venv command as recommended in docs above. PyCharm使用上面文档中推荐的venv命令。

If you create virtual environment in terminal using venv command, it should work without issues. 如果使用venv命令在终端中创建虚拟环境,它应该没有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM