gpt4 book ai didi

python - 当脚本位于子目录中时无法导入 numpy

转载 作者:太空宇宙 更新时间:2023-11-04 04:11:15 25 4
gpt4 key购买 nike

我有这样的文件夹结构

root/        
A0/
A1/
B0/
B1/

假设我有一个名为 test.py 的文件。在其中,我像这样导入 numpy

import numpy as np

这就是文件中包含的全部内容。当它位于根目录、子文件夹 A0、A1、B0 中时,这可以正常工作,但在子文件夹 B1 中会引发错误。是否报错如下:

Traceback (most recent call last):
File "pose/utils/test.py", line 1, in <module>
import numpy as np
File "/home/adrian/.local/lib/python3.6/site-packages/numpy/__init__.py", line 187, in <module>
from .testing import Tester
File "/home/adrian/.local/lib/python3.6/site-packages/numpy/testing/__init__.py", line 10, in <module>
from unittest import TestCase
File "/usr/lib/python3.6/unittest/__init__.py", line 59, in <module>
from .case import (TestCase, FunctionTestCase, SkipTest, skip, skipIf,
File "/usr/lib/python3.6/unittest/case.py", line 6, in <module>
import logging
File "/home/adrian/Projects/lpr-pose-estimation/pose/utils/logging.py", line 3, in <module>
from pose.utils.utils import mkdir_if_missing
File "/home/adrian/Projects/lpr-pose-estimation/pose/utils/utils.py", line 4, in <module>
from scipy.misc import imresize
File "/home/adrian/.local/lib/python3.6/site-packages/scipy/misc/__init__.py", line 68, in <module>
from scipy.interpolate._pade import pade as _pade
File "/home/adrian/.local/lib/python3.6/site-packages/scipy/interpolate/__init__.py", line 175, in <module>
from .interpolate import *
File "/home/adrian/.local/lib/python3.6/site-packages/scipy/interpolate/interpolate.py", line 32, in <module>
from .interpnd import _ndim_coords_from_arrays
File "interpnd.pyx", line 1, in init scipy.interpolate.interpnd
File "/home/adrian/.local/lib/python3.6/site-packages/scipy/spatial/__init__.py", line 98, in <module>
from .kdtree import *
File "/home/adrian/.local/lib/python3.6/site-packages/scipy/spatial/kdtree.py", line 8, in <module>
import scipy.sparse
File "/home/adrian/.local/lib/python3.6/site-packages/scipy/sparse/__init__.py", line 230, in <module>
from .base import *
File "/home/adrian/.local/lib/python3.6/site-packages/scipy/sparse/base.py", line 9, in <module>
from scipy._lib._numpy_compat import broadcast_to
File "/home/adrian/.local/lib/python3.6/site-packages/scipy/_lib/_numpy_compat.py", line 16, in <module>
_assert_warns = np.testing.assert_warns
AttributeError: module 'numpy' has no attribute 'testing'

作为模块导入(例如 cv2)也会导致错误,但仅限于 numpy:

ImportError: numpy.core.multiarray failed to import
Traceback (most recent call last):
File "pose/utils/test.py", line 1, in <module>
import cv2 as cv
File "/home/adrian/.local/lib/python3.6/site-packages/cv2/__init__.py", line 3, in <module>
from .cv2 import *
ImportError: numpy.core.multiarray failed to import

出现这种错误的原因可能是什么?

最佳答案

如果您的启动脚本位于 .../pose/utils 中,那么每个绝对导入也会在那里查找模块。此目录包含一个名为 logging 的模块(类似于标准库中的模块)。

numpy 包的初始化期间(执行其 __init__.py)和在 numpy.testing 可用之前,通常的导入链发生(如回溯中所示)导致错误的 logging 模块,进而导致导入 _numpy_compat 试图访问 numpy.testing 太早了。

要避免这种循环导入问题,您可以重命名您的 logging 模块或将启动脚本移动到另一个目录。

关于python - 当脚本位于子目录中时无法导入 numpy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56229616/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com