首页 > 解决方案 > 不断收到 python ModuleNotFoundError: No module named

问题描述

我正在研究一个树莓派 python 项目,每次导入一个包时,我都会遇到同样的错误ModuleNotFoundError: No module named ''。例如我正在尝试添加ambient_api包

我按照他们的设置步骤:

pip install ambient_api

在我的文件顶部添加了导入:

from ambient_api.ambientapi import AmbientAPI
api = AmbientAPI()

但我收到错误:

ModuleNotFoundError: No module named 'ambient_api'

我尝试添加的所有导入都会发生这种情况,我无法弄清楚我缺少什么。

当我在谷歌上查找时,我发现__init__.py可能是一个解决方案,但我不确定它是如何工作的?

编辑:

here is what was output when I installed:
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Requirement already satisfied: ambient_api in /home/pi/.local/lib/python2.7/site-packages (1.5.2)
Requirement already satisfied: requests in /usr/lib/python2.7/dist-packages (from ambient_api) (2.21.0)
Requirement already satisfied: urllib3 in /usr/lib/python2.7/dist-packages (from ambient_api) (1.24.1)

我的终端中的版本:pi@raspberrypi:~/Raspberry-Pi-Greenhouse $ python --version Python 2.7.16

但是当我运行程序时,它看起来像 ide 中的 shell 版本是:Python 3.7.3 (/usr/bin/python3)

标签: pythonraspberry-pi

解决方案


正如@JaFizz 在Pytorch 中所解释的那样,安装但不工作 ubuntu 18.04它通过提供别名并正确安装到所需的 python 版本来解决(在同一台机器上安装了两个 python)

首先需要指定python的位置,例如:

alias python=/usr/local/bin/python3.6

然后安装它:

python pip install <package name>

推荐阅读