python - 相对导入的问题

标签 python cython python-import pytorch

我运行一个名为 test.py 的文件,它具有这样的导入

from .nms.cpu_nms import cpu_nms, cpu_soft_nms
from .nms.gpu_nms import gpu_nms

当我运行该文件时,我收到此错误:

Traceback (most recent call last):
  File "test.py", line 9, in <module>
    from utils.nms_wrapper import nms
  File /media/ryan/shakira/InsightFace_Pytorch/FaceBoxes.PyTorch/utils/nms_wrapper.py",     line 7, in <module>
    from .nms.cpu_nms import cpu_nms, cpu_soft_nms
ModuleNotFoundError: No module named 'utils.nms.cpu_nms'

我尝试过这样做

sys.path.append('/path/to/the/main/directory/')

但这也行不通,

编辑:

这是我的目录结构:

FaceBoxes.PyTorch/
├── data
│   ├── AFW
│   │   └── img_list.txt
│   ├── config.py
│   ├── data_augment.py
│   ├── FDDB
│   │   └── img_list.txt
│   ├── __init__.py
│   ├── PASCAL
│   │   └── img_list.txt
│   ├── __pycache__
│   │   ├── config.cpython-36.pyc
│   │   ├── data_augment.cpython-36.pyc
│   │   ├── __init__.cpython-36.pyc
│   │   └── wider_voc.cpython-36.pyc
│   ├── WIDER_FACE
│   │   └── img_list.txt
│   └── wider_voc.py
├── layers
│   ├── functions
│   │   ├── prior_box.py
│   │   └── __pycache__
│   │       └── prior_box.cpython-36.pyc
│   ├── __init__.py
│   ├── modules
│   │   ├── __init__.py
│   │   ├── multibox_loss.py
│   │   └── __pycache__
│   │       ├── __init__.cpython-36.pyc
│   │       └── multibox_loss.cpython-36.pyc
│   └── __pycache__
│       └── __init__.cpython-36.pyc
├── LICENSE
├── make.sh
├── models
│   ├── faceboxes.py
│   └── __init__.py
├── README.md
├── test.py
├── train.py
└── utils
    ├── box_utils.py
    ├── build.py
    ├── __init__.py
    ├── nms
    │   ├── cpu_nms.c
    │   ├── cpu_nms.pyx
    │   ├── gpu_nms.cpp
    │   ├── gpu_nms.hpp
    │   ├── gpu_nms.pyx
    │   ├── __init__.py
    │   ├── nms_kernel.cu
    │   ├── __pycache__
    │   │   └── __init__.cpython-36.pyc
    │   └── py_cpu_nms.py
    ├── nms_wrapper.py
    ├── __pycache__
    │   ├── box_utils.cpython-36.pyc
    │   ├── __init__.cpython-36.pyc
    │   └── nms_wrapper.cpython-36.pyc
    └── timer.py

任何建议都会非常有帮助,提前致谢。

最佳答案

导入中没有错误,问题是有一个我必须编译的 cython 文件,我已经掩盖了它。编译后,问题就消失了。

关于python - 相对导入的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54722814/

相关文章:

python - 如何将 Hermite 多项式与随机梯度下降 (SGD) 结合使用?

python - 如何在python中加速多个内积

cython - 使用扩展类型(cdef)和设置纯Python代码有什么区别?

python - "from package import *"和 "import package"之间的性能

google-app-engine - Google App Engine 从父文件夹导入库

python - 我使用 python3.5 和 OpenCV 3.1.0,OpenCV 函数 cv2.countNonZero(img) 我得到一个错误

python - 查找字符串的条件

python - 如何在 Cython 中将函数指针传递给外部程序

python - 导入非打包模块的python类

python - 在seaborn catplot中为每个类别添加从最小点到最大点的水平线