我有一个文件结构
math/
snippets/
numerical_methods.py
homework1/
main.py
homework2/
main.py在家庭作业1的main.py中,我想做
from ..snippets.numerical_methods import fixed-point-iteration这样我就不用为我使用的每一个作业重写这个算法了。但是我得到了“父模块‘未加载,无法执行相对导入”的错误。我做错了什么?
发布于 2014-09-26 20:46:45
您不能从层次结构中高于主脚本的位置导入,即比homework1文件夹更高的内容。您可以做的是将路径添加到要导入的脚本中:
import sys
sys.path.append("..")
from snippets.numerical_methods import fixed-point-iterationhttps://stackoverflow.com/questions/26067972
复制相似问题