【问题标题】:ImportError: Cannot import name httputilImportError:无法导入名称 httputil
【发布时间】:2018-10-09 06:09:06
【问题描述】:

我有两个不同的文件:data_sourceutil。我像这样在 data_source 导入 util

from util import HttpUtil

但是运行时出现了一些错误

from util import HttpUtil

ImportError: 无法导入名称 HttpUtil

我该如何解决这个问题?

这是模块的目录结构: enter image description here

实用程序

 class HttpUtil(object):

   @staticmethod   
   def request(host, port, method):

数据源

import json  
import urllib   
import time   
from util import HttpUtil

【问题讨论】:

  • 您能否提供有关文件内容的更多信息?我假设data_source 是您要导入util 的脚本。 util 是您的个人模块吗?你能分享模块的目录结构吗?你用pip安装了吗?
  • 这个 HttpUtil 是否在 Python 系统路径中?
  • 您的util 文件夹中有__init__.py 文件吗?
  • @raj data_sourceutil 是我的个人模块。
  • 可以直接导入utilimport util吗?您可能正在导入其他不包含HttpUtilutil(它是非常常见的模块)。可以把util的名字改成util2看看吗?

标签: python


【解决方案1】:
  • 请检查是否真的存在在 utils 中定义的 HttpUtil 函数。例如,请注意拼写或大小写错误。
  • 发布 .py 文件以供参考,以防错误仍然存​​在。

【讨论】:

  • tks。我发布了更多 .py 文件
  • 您的目录中有__init__.py 吗? This 否则可能会有所帮助。
  • 我的目录中有_init_.py
猜你喜欢
  • 1970-01-01
  • 2016-03-31
  • 2014-10-10
  • 2014-09-20
  • 2014-08-28
  • 2014-06-10
  • 2016-05-16
  • 2019-05-25
  • 2017-04-22
相关资源
最近更新 更多