Python不会从导入另一个模块的文件夹中导入模块
所以我有一个这样的项目Python不会从导入另一个模块的文件夹中导入模块,python,Python,所以我有一个这样的项目 root/ api/ nest/ nest.py tests.py __init__.py import unittest from nest.nest import JsonParser 当我运行pythonnest/nest.py-args时,它工作得很好,但是当我运行pythonnest/tests.py时,它崩溃了,说我在tests.py中有一个导入错误 ModuleNotFoundError:没有名为“nest.nest”的模
root/
api/
nest/
nest.py
tests.py
__init__.py
import unittest
from nest.nest import JsonParser
当我运行pythonnest/nest.py-args时,它工作得很好,但是当我运行pythonnest/tests.py时,它崩溃了,说我在tests.py中有一个导入错误
ModuleNotFoundError:没有名为“nest.nest”的模块;'“nest”不是一个包
我的tests.py导入如下所示
root/
api/
nest/
nest.py
tests.py
__init__.py
import unittest
from nest.nest import JsonParser
我还在api/api.py模块中使用这个JsonParser类,它工作得很好
此外,如果我在pycharm中运行tests.py,它会正常工作,但如果我在console中尝试,它会引发此异常问题是,当在模块范围外运行时,您的interpreter不知道该模块所在的路径 解决方案可以是在代码中添加模块的绝对路径:
import sys
sys.path.append('my/path/to/module/folder')
import module-of-interest
哦,伙计,你让我想起了很久以前我用来导入sys.path.insert0的一个解决方案,“./”导入感兴趣的模块他希望这能帮助你@nexla: