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

标签 python ubuntu amazon-ec2 import python-import

我正在运行一个脚本,但出现错误:

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/

相关文章:

python - 运算符结果的 Numpy 总和,无需分配不必要的数组

python - 如何使用带有 python 子进程模块或 gst-launch-1.0 命令的 gstreamer 接收字节流?

ubuntu - symfony2 警告 : sqlite3 profiler database is readonly

用于创建名称为日期戳的新目录的 Linux 脚本

python - Numpy 安装 AWS EC2 报错

c# - 有没有其他方法可以在没有 VM 的情况下在 Azure 上运行窗口服务?

linux - Laravel 4.2 邮件突然停止在 Amazon ec2 linux 中工作

python - 蒙特卡洛模拟Python

python - 尝试使用 mod_wsgi 设置 flask

mongodb - 如何通过身份验证在 Upstart 服务中设置mongodb