Development Tip

virtualenv에서 "matplotlib.pyplot을 plt로 가져 오기"할 수 없습니다.

yourdevel 2020. 11. 12. 20:27
반응형

virtualenv에서 "matplotlib.pyplot을 plt로 가져 오기"할 수 없습니다.


저는 가상 환경에서 플라스크로 작업하고 있습니다. pip로 matplotlib를 설치할 수 있었고 import matplotlibPython 세션에서 할 수 있습니다 . 그러나 내가 그것을 가져올 때

matplotlib.pyplot as plt

다음과 같은 오류가 발생합니다.

>>> import matplotlib.pyplot as plt

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "//anaconda/envs/myenv/lib/python2.7/site-packages/matplotlib/pyplot.py", line 109, in <module>
    _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
  File "//anaconda/envs/myenv/lib/python2.7/site-packages/matplotlib/backends/__init__.py", line 32, in pylab_setup
    globals(),locals(),[backend_name],0)
  File "//anaconda/envs/myenv/lib/python2.7/site-packages/matplotlib/backends/backend_macosx.py", line 24, in <module>
    from matplotlib.backends import _macosx
RuntimeError: 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.

왜 파이썬을 프레임 워크로 설치하라고 요청하는지 혼란 스럽습니다. 이미 존재하지 않습니까? "파이썬을 프레임 워크로 설치"한다는 것은 무엇을 의미하며 어떻게 설치합니까?


솔루션 은 저에게 효과적이었습니다. 가상 환경에서 pip를 사용하여 이미 matplotlib를 설치 한 경우 다음을 입력하면됩니다.

$ cd ~/.matplotlib
$ nano matplotlibrc

그리고 backend: TkAgg거기에 적어 주세요. 추가 정보가 필요하면 솔루션 링크로 이동하십시오.


같은 오류가 발생하여 Jonathan의 답변을 시도했습니다 .

백엔드 Agg를 사용하여이 문제를 해결할 수 있습니다.

이동하여 User/yourname/.matplotlib열고 / 만들고 matplotlibrc다음 줄을 추가하면 backend : Agg작동합니다.

나는 프로그램을 실행하고 오류는 없지만 플롯도 없었고 시도했지만 backend: Qt4AggPyQt4가 설치되지 않았다는 것을 출력했습니다.

그런 다음 다른 백엔드를 시도 backend: TkAgg했습니다 . , 작동합니다!

그래서 우리는 다른 백엔드를 시도 할 수 있고 일부는 PyQt4와 같은 requeired 패키지를 작동하거나 설치할 수 있습니다.

다음은 matplotlib를 시도하고 테스트 할 수있는 샘플 Python 스 니펫입니다.

import matplotlib

matplotlib.use('TkAgg')
import matplotlib.pyplot as plt

plt.plot([1, 2, 3], [0, 3, 7])
plt.show()

pip를 사용하여 matplotlib를 설치할 때 비슷한 문제가 발생했습니다. 기본적으로 1.5.0 인 최신 버전을 설치했습니다. 그러나 Python 3.4 및 matplotlib 1.4.3을 사용하는 다른 가상 환경이 있었고 matplotlib.pyplot을 가져올 때이 환경이 제대로 작동했습니다. 따라서 다음을 사용하여 이전 버전의 matplotlib를 설치했습니다.

cd path_to_virtual_environment    # assume directory is called env3
env3/bin/pip install matplotlib==1.4.3

나는 이것이 해결 방법 일뿐이라는 것을 알고 있지만 단기적인 해결책으로 효과적이었습니다.


.matplotib/matplotlibrc구성 파일 을 설정하지 않으려면 'Agg'가져온 직후 matplotlib와 가져 오기 전에 런타임에 백엔드를 설정하여이 문제를 피할 수 있습니다 matplotlib.pyplot.

In [1]: import matplotlib

In [2]: matplotlib.use('Agg')

In [3]: import matplotlib.pyplot as plt

In [4]: fig, ax = plt.subplots(1, 1)

In [5]: import numpy as np

In [6]: x = np.linspace(-1., 1.)

In [7]: y = np.sin(x)

In [8]: ax.plot(x, y)
Out[8]: [<matplotlib.lines.Line2D at 0x1057ecf10>]

In [9=]: fig.savefig('myplot.png')

enter image description here


백엔드를 사용하여이 문제를 해결할 수 있습니다. Agg

이동하여 User/yourname/.matplotlib열고 / 만들고 matplotlibrc다음 줄을 추가하면 backend : Agg작동합니다.


Although most answers seem to point towards patching the activate script to use the system python, I was having trouble getting that to work and an easy solution for me - though a little cringey - was to install matplotlib to the global environment and use that instead of a virtualenv instance. You can do this either by creating your virtualenv with the --system-site-packages flag like virtualenv --system-site-packages foo, or to use the universal flag when pip installing like pip install -U matplotlib.


A clean and easy solution is to create a kernel that sets PYTHONHOME to VIRTUAL_ENV and then uses the system Python executable (instead of the one in the virtualenv).

If you want to automate the creation of such a kernel, you can use the jupyter-virtualenv-osx script.

참고URL : https://stackoverflow.com/questions/29433824/unable-to-import-matplotlib-pyplot-as-plt-in-virtualenv

반응형