Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 相对导入错误_Python 3.x - Fatal编程技术网

Python 3.x 相对导入错误

Python 3.x 相对导入错误,python-3.x,Python 3.x,我一直在使用以下relative方法导入项目: from .utils import * 然而,它似乎并没有始终如一地工作,我经常会在python3.6和python3.7(在mac和ubuntu上)上遇到以下错误: ImportError:尝试在没有已知父包的情况下进行相对导入 我实施的黑客修复程序如下: try: from .utils import * except ImportError: from utils import * 但这似乎是最糟糕的方法。为什么有时会发

我一直在使用以下
relative
方法导入项目:

from .utils import *
然而,它似乎并没有始终如一地工作,我经常会在python3.6和python3.7(在mac和ubuntu上)上遇到以下错误:

ImportError:尝试在没有已知父包的情况下进行相对导入

我实施的黑客修复程序如下:

try:
    from .utils import *
except ImportError:
    from utils import *

但这似乎是最糟糕的方法。为什么有时会发生此错误,最好的解决方法是什么?

目录结构是什么?这是否回答了您的问题?
try:
    from .utils import *
except ImportError:
    from utils import *