我可以在一个file.py中导入matplotlib.pyplot,但无法在Django app的view.py中导入

2024-04-26 12:37:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我有plot_file.py,它有:

import matplotlib.pyplot as plt

def plot_graph():
    squares = [1, 4, 9, 16, 25]
    plt.plot(squares)
    plt.savefig('static/img/sqaure_plot.png', bbox_inches='tight')

当我运行这个,它的工作和保存文件正确。我正在Django的同一个app文件夹中的views.py中导入此函数

当我将import语句放入views.py

from myapp.plot_file import plot_graph as pg

保存并尝试从终端启动runserver,我得到:

ModuleNotFoundError: No module named 'matplotlib'

两个文件都在同一个文件夹中

myapp/plot_file.py
myapp/views.py

我已经使用pip3 install matplotlib安装了matplotlib。我正在使用MacOS和后来安装的python3。我使用的是在Python3上编译的崇高文本

我不明白如何在plot\u file.py而不是views.py文件中导入matplotlib

我是初学者。先谢谢你


Tags: 文件pyimport文件夹plotmatplotlibdefas