导入错误,即使文件明显在路径中

Import error even though file is clearly in path

我是 运行 脚本,我收到错误:

Traceback (most recent call last):
  File "common/tensorflow/run_tf_benchmark.py", line 30, in <module>
    from common.base_benchmark_util import BaseBenchmarkUtil
ModuleNotFoundError: No module named 'common'

我 运行 在具有以下 AMI 的 ec2 实例上:深度学习 AMI (Ubuntu 16.04) 版本 25.0 (ami-025c308193ac1a136) 我也在 [=26= 】 anaconda环境,来自AMI。我的 Ubuntu 目录结构如下:

home
--ubuntu
  --benchmark_models
    --benchmark    
      --common
        --__init__.py
        --base_benchmark_util.py
        --tensorflow
          --run_tf_benchmark.py
          --__init__.py

请注意,目录中还有其他文件,但这些是相关的 python 文件。 目前,run_tf_benchmark.py 顶部的导入是

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function


import sys
sys.path.append('/home/ubuntu/benchmark_models/benchmarks/common')
print(sys.path)
from argparse import ArgumentParser
from common.base_benchmark_util import BaseBenchmarkUtil

打印出来的 sys.path 是

['/home/ubuntu/benchmark_models/benchmarks/common/tensorflow', '/home/ubuntu/src/cntk/bindings/python',
 '/home/ubuntu/benchmark_models/models/image_recognition/tensorflow/mobilenet_v2', '/home/ubuntu/models',
 '/home/ubuntu/models/research', '/home/ubuntu/models/research/slim', 
'/home/ubuntu/anaconda3/lib/python36.zip', '/home/ubuntu/anaconda3/lib/python3.6', 
'/home/ubuntu/anaconda3/lib/python3.6/lib-dynload', '/home/ubuntu/anaconda3/lib/python3.6/site-packages',
 '/home/ubuntu/benchmark_models/benchmarks/common']

在python路径下可以清楚的看到common,但是导入错误还是会发生。我做错了什么?

只需在导入时删除 'common',因为您已经在 'common' 目录中

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function


import sys
sys.path.append('/home/ubuntu/benchmark_models/benchmarks/common')
print(sys.path)
from argparse import ArgumentParser
from base_benchmark_util import BaseBenchmarkUtil

好的,我解决了这个问题。问题是我添加了 /home/ubuntu/benchmark_models/benchmarks/common,而不是 /home/ubuntu/benchmark_models/benchmarks/。通过导入基准测试,它还允许我的所有其他代码也导入 common。