我正在运行一个脚本,但出现错误:
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),并且我还在来自 AMI 的 tensorflow_p36 anaconda 环境中工作。我的 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
关于python - 即使文件明显在路径中,导入错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60570009/