python - 为什么我必须将这个 import 语句写两次?

标签 python import package

这是我的文件夹结构:

/ 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)

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

最佳答案

因为 Thermal_Formatter 模块位于同名的包内。尝试:

from Thermal_Formatter import Thermal_Formatter
Thermal_Formatter.processAndPrint(something)

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

关于python - 为什么我必须将这个 import 语句写两次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21384852/

相关文章:

java - 老 C++ 程序员,Java 新手,需要有关包中 Swing JFrame 的帮助

python - 如何在 Python 上将文本转换为 URL 语法?

python - 如何从 python 获取(解析)XML 中的子项

python - 在并行区域中调用 python 脚本(使用 matplotlib)时,OpenMP 线程 C 程序停止

python - 如何在 python igraph 中从 .gml 文件创建加权图

android - 如何导入电子邮件附件?

Java 包 com 和 org

r - 计算繁重的R小插曲

python - 如何在 Heroku 上运行一次性 python 脚本

java - 从 pdf 中获取页面并使用 itext 将其保存为图像文件