【问题标题】:Why do I have to write this import statement twice?为什么我必须写两次这个导入语句?
【发布时间】:2014-01-27 15:31:56
【问题描述】:

这是我的文件夹结构:

/ Thermal_Formatter
  Thermal_Formatter.py
  __init__.py

test.py

Thermal_Formatter.py我有这个方法:

def processAndPrint(text):

test.py 这不起作用:

import Thermal_Formatter
Thermal_Formatter.processAndPrint(something)

但确实如此:

import Thermal_Formatter.Thermal_Formatter
Thermal_Formatter.Thermal_Formatter.processAndPrint(something)

为什么我在 import 语句和模块调用中两次写入模块名称时它会起作用?

【问题讨论】:

  • 因为两次都不是模块名;你有一个包和一个同名的模块。
  • from Thermal_Formatter import Thermal_Formatter(Wooble 说得对。)
  • Importing packages in Python 的可能重复项

标签: python import packages


【解决方案1】:

因为Thermal_Formatter 模块位于同名包中。试试:

from Thermal_Formatter import Thermal_Formatter
Thermal_Formatter.processAndPrint(something)

如果您想要更明智的使用方式。

【讨论】:

    猜你喜欢
    • 2015-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-14
    • 1970-01-01
    相关资源
    最近更新 更多