【问题标题】:Import python module directory with "space" char使用“空格”字符导入 python 模块目录
【发布时间】:2019-09-06 14:34:26
【问题描述】:

我要导入当前文件目录中名为 DataGenerator 的类:

/content/gdrive/My Drive/mrcnn_fire/ssd/python_files/object_detection_2d_data_generator.py

我是这样导入的:

from content.gdrive.My Drive.mrcnn_fire.ssd.python_files.object_detection_2d_data_generator import DataGenerator

它给了我以下错误:

File "<ipython-input-3-7d861f8abbeb>", line 4
    from content.gdrive.My Drive.mrcnn_fire.ssd.python_files.object_detection_2d_data_generator import DataGenerator
                               ^
SyntaxError: invalid syntax

【问题讨论】:

  • 你的主脚本/content/gdrive/My Drive/mrcnn_fire/ssd/python_files吗?如果是这样,请尝试不使用路径导入:from object_detection_2d_data_generator import DataGenerator
  • 主脚本不在同一个目录
  • 文件名是模块名,但这意味着文件名必须遵守 Python 的模块名规则:不允许有空格。您导入模块,而不是任意文件。

标签: python python-import python-module


【解决方案1】:

你也可以这样试试:

# object_detection_2d_data_generator.py
import sys
sys.path.insert(0, '/content/gdrive/My Drive/mrcnn_fire/ssd/python_files')

from object_detection_2d_data_generator import DataGenerator

【讨论】:

    【解决方案2】:

    你可以这样导入:

    DataGenerator = __import__("content/gdrive/My Drive/mrcnn_fire/ssd/python_files/object_detection_2d_data_generator/DataGenerator")
    

    【讨论】:

      猜你喜欢
      • 2021-08-03
      • 1970-01-01
      • 1970-01-01
      • 2019-10-30
      • 1970-01-01
      • 2015-07-23
      • 2014-08-28
      • 2021-03-07
      • 1970-01-01
      相关资源
      最近更新 更多