【问题标题】:Importing python modules works in command line but not in the python 3.8 shell导入 python 模块在命令行中有效,但在 python 3.8 shell 中无效
【发布时间】:2020-04-18 06:39:28
【问题描述】:

我在 python 中导入模块时遇到问题。 当我在命令行中运行我的程序时,它工作得很好。 但是,当我尝试在 python shell 中运行相同的程序时,我会收到以下错误提示:

ModuleNotFoundError: 没有名为“matplotlib”的模块

我已经使用“python -m pip install matplotlib”成功安装了 matplotlib。 我已经读过,当您安装了两个不同版本的 python 时,可能会发生这种情况;但是,我没有。 我已经卸载并重新安装了python,但我仍然遇到同样的问题。我还使用 pip 卸载并重新安装了 matplotlib。

我相信我的问题是python用来搜索导入模块的模块路径在两者之间是不同的。

当我在 python shell 和命令行中使用“print(sys.path)”命令时,我得到两个不同的输出。

任何帮助将不胜感激!!!

The file different system paths between the python shell and the command line

【问题讨论】:

标签: python command-line import-module


【解决方案1】:

我想,您在同一台计算机上安装了两个 Python 版本。

如果是这样,那么我的答案是进入两个 Python 脚本文件夹并在它们两个上安装 matplotlib。

我也遇到过这个问题。我的路径包括 Python 3.7.1 的 pip 以及当我尝试在 Python 3.4 上导入模块时。它会引发错误!

也许,您可以将两个 Python 添加到路径中。

【讨论】:

    【解决方案2】:

    你有两个版本的 python。我建议你删除所有的蟒蛇并选择 anaconda https://www.anaconda.com/distribution/。它将解决您的路径问题,并允许您使用不同版本的 python 创建环境。这也是未来最不痛苦的方式:)祝你好运。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-07
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      • 2012-01-31
      • 1970-01-01
      相关资源
      最近更新 更多