python错误:TypeError: 'module' object is not callable

2023-05-16

TrainCorpusStructure.py 文件中的代码如下:

class TrainCorpusStructure:
    inputs = []

Demo.py中的代码如下:

from corpusProcess import TrainCorpusStructure

class Demo:
    def test(self):
        tcs = TrainCorpusStructure()
        tcs.inputs.append('fa')
        tcs.inputs.append('su')
        print(tcs.inputs)

        # TrainCorpusStructure.inputs.append('haohao')
        # TrainCorpusStructure.inputs.append('tttttttt')
        # print(TrainCorpusStructure.inputs)

if __name__ == '__main__':
    demo = Demo()
    demo.test()

其实想实现一个非常简单的功能,在TestFang.py文件中操作TrainCorpusStructure.py中的列表 inputs

但是遗憾的是提示有错误:

Traceback (most recent call last):
  File "/home/ubuntu/workspace/BioEvent/corpusProcess/TestFang.py", line 16, in <module>
    tf.test()
  File "/home/ubuntu/workspace/BioEvent/corpusProcess/TestFang.py", line 5, in test
    tcs = TrainCorpusStructure()
TypeError: 'module' object is not callable

于是在网上查询错误原因: 跟import导入机制有关

正确的写法有两种:

写法1:

from corpusProcess import TrainCorpusStructure

class Demo:
    def test(self):
        tcs = TrainCorpusStructure.TrainCorpusStructure()
        tcs.inputs.append('fang')
        tcs.inputs.append('su')
        print(tcs.inputs)

        # TrainCorpusStructure.inputs.append('haohao')
        # TrainCorpusStructure.inputs.append('tttttttt')
        # print(TrainCorpusStructure.inputs)

if __name__ == '__main__':
    demo = Demo()
    demo.test()

写法2:

import corpusProcess.TrainCorpusStructure as tc

class Demo:
    def test(self):
        tcs = tc.TrainCorpusStructure()
        tcs.inputs.append('fang')
        tcs.inputs.append('su')
        print(tcs.inputs)

        # TrainCorpusStructure.inputs.append('haohao')
        # TrainCorpusStructure.inputs.append('tttttttt')
        # print(TrainCorpusStructure.inputs)

if __name__ == '__main__':
    demo = Demo()
    demo.test()

仔细观察import导入的方式:明确一点的是,TrainCorpusStructure.py文件和TestDemo.py文件在同一个包里(即一个文件夹下)

写法1和写法2的区别有两点:

1,导入方式,写法1是:from Corpus import TrainCorpusStructure, 

                          写法2是:import corpusProcess.TrainCorpusStructure as tc

2, 调用方式不同,

写法1是: tcs = TrainCorpusStructure.TrainCorpusStructure(),

写法2是 tcs = tc.TrainCorpusStructure()

补充理解:其实明白了调用的规则非常容易理解,A.py 文件调用B.py文件的方法(注意不是函数)

规则:1,如果调用的是B.py文件中的方法,即B.py 中有Class B: 调用的原则是:模块名—类名—方法名

           2, 如果调用的是B.py文件中的函数:模块名—函数名

为了进一步测试:又写了一个类Wu.py

import corpusProcess.Demo as tfl
class Wu:
    def hehe(self):
        testF = tfl.Demo()
        testF.test()
        print('测试结果怎么样')
if __name__ == '__main__':
    ts = Wu()
    ts.hehe()

不明白的地方:如果我把Wu.py 命名为TestWu.py 或则 WuTest.py 的话, 运行Wu.py的时候就会进行单元测试而不会输出任何结果,我不明白为什么?待明确后补上

模块名如果有test或TEST等几个字符会进行单元测试,如果不是进行单元测试,不要使用'Test'这个字符

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

python错误:TypeError: 'module' object is not callable 的相关文章

  • Python 中的哈希映射

    我想用Python实现HashMap 我想请求用户输入 根据他的输入 我从 HashMap 中检索一些信息 如果用户输入HashMap的某个键 我想检索相应的值 如何在 Python 中实现此功能 HashMap
  • 安装了 32 位的 Python,显示为 64 位

    我需要运行 32 位版本的 Python 我认为这就是我在我的机器上运行的 因为这是我下载的安装程序 当我重新运行安装程序时 它会将当前安装的 Python 版本称为 Python 3 5 32 位 然而当我跑步时platform arch
  • 删除flask中的一对一关系

    我目前正在使用 Flask 开发一个应用程序 并且在删除一对一关系中的项目时遇到了一个大问题 我的模型中有以下结构 class User db Model tablename user user id db Column db String
  • 独立滚动矩阵的行

    我有一个矩阵 准确地说 是 2d numpy ndarray A np array 4 0 0 1 2 3 0 0 5 我想滚动每一行A根据另一个数组中的滚动值独立地 r np array 2 0 1 也就是说 我想这样做 print np
  • 使用字典映射数据帧索引

    为什么不df index map dict 工作就像df column name map dict 这是尝试使用index map的一个小例子 import pandas as pd df pd DataFrame one A 10 B 2
  • 在Python中连接反斜杠

    我是 python 新手 所以如果这听起来很简单 请原谅我 我想加入一些变量来生成一条路径 像这样 AAAABBBBCCCC 2 2014 04 2014 04 01 csv Id TypeOfMachine year month year
  • datetime.datetime.now() 返回旧值

    我正在通过匹配日期查找 python 中的数据存储条目 我想要的是每天选择 今天 的条目 但由于某种原因 当我将代码上传到 gae 服务器时 它只能工作一天 第二天它仍然返回相同的值 例如当我上传代码并在 07 01 2014 执行它时 它
  • 从Python中的字典列表中查找特定值

    我的字典列表中有以下数据 data I versicolor 0 Sepal Length 7 9 I setosa 0 I virginica 1 I versicolor 0 I setosa 1 I virginica 0 Sepal
  • Python,将函数的输出重定向到文件中

    我正在尝试将函数的输出存储到Python中的文件中 我想做的是这样的 def test print This is a Test file open Log a file write test file close 但是当我这样做时 我收到
  • 如何在不丢失注释和格式的情况下更新 YAML 文件 / Python 中的 YAML 自动重构

    我想在 Python 中更新 YAML 文件值 而不丢失 Python 中的格式和注释 例如我想改造 YAML 文件 value 456 nice value to value 6 nice value 界面类似于 y yaml load
  • “隐藏”内置类对象、函数、代码等的名称和性质[关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我很好奇模块中存在的类builtins无法直接访问的 例如 type lambda 0 name function of module
  • 如何使用 pybrain 黑盒优化训练神经网络来处理监督数据集?

    我玩了一下 pybrain 了解如何生成具有自定义架构的神经网络 并使用反向传播算法将它们训练为监督数据集 然而 我对优化算法以及任务 学习代理和环境的概念感到困惑 例如 我将如何实现一个神经网络 例如 1 以使用 pybrain 遗传算法
  • Numpy - 根据表示一维的坐标向量的条件替换数组中的值

    我有一个data多维数组 最后一个是距离 另一方面 我有距离向量r 例如 Data np ones 20 30 100 r np linspace 10 50 100 最后 我还有一个临界距离值列表 称为r0 使得 r0 shape Dat
  • Cython 和类的构造函数

    我对 Cython 使用默认构造函数有疑问 我的 C 类 Node 如下 Node h class Node public Node std cerr lt lt calling no arg constructor lt lt std e
  • import matplotlib.pyplot 给出 AttributeError: 'NoneType' 对象没有属性 'is_interactive'

    我尝试在 Pycharm 控制台中导入 matplotlib pyplt import matplotlib pyplot as plt 然后作为回报我得到 Traceback most recent call last File D Pr
  • 仅第一个加载的 Django 站点有效

    我最近向 stackoverflow 提交了一个问题 标题为使用mod wsgi在apache上多次请求后Django无限加载 https stackoverflow com questions 71705909 django infini
  • 使用特定颜色和抖动在箱形图上绘制数据点

    我有一个plotly graph objects Box图 我显示了箱形 图中的所有点 我需要根据数据的属性为标记着色 如下所示 我还想抖动这些点 下面未显示 Using Box我可以绘制点并抖动它们 但我不认为我可以给它们着色 fig a
  • Pandas 将多行列数据帧转换为单行多列数据帧

    我的数据框如下 code df Car measurements Before After amb temp 30 268212 26 627491 engine temp 41 812730 39 254255 engine eff 15
  • 实现 XGboost 自定义目标函数

    我正在尝试使用 XGboost 实现自定义目标函数 在 R 中 但我也使用 python 所以有关 python 的任何反馈也很好 我创建了一个返回梯度和粗麻布的函数 它工作正常 但是当我尝试运行 xgb train 时它不起作用 然后 我
  • 如何应用一个函数 n 次? [关闭]

    Closed 这个问题需要细节或清晰度 help closed questions 目前不接受答案 假设我有一个函数 它接受一个参数并返回相同类型的结果 def increment x return x 1 如何制作高阶函数repeat可以

随机推荐

  • np.maximum vs np.minimum

    一直按照字面意思理解 xff0c 以为maxmum取最大值 examples 原来是有广播机制的 xff0e np maximum 取对应位置上的大值 xff0c np minimum 取对应位置上的较小值 xff0e import num
  • tensorflow通过模型文件,使用tensorboard查看其模型图Graph

    Google提供了一个工具 xff0c TensorBoard xff0c 它能以图表的方式分析你在训练过程中汇总的各种数据 xff0c 其中包括Graph结构 所以我们可以简单的写几行Pyhton xff0c 加载Graph xff0c
  • numpy中np.maximum的使用

    np maximum X Y out 61 None X和Y逐位进行比较 选择最大值 xff0c 最少接受两个参数 gt gt gt np maximum 2 3 4 1 5 2 array 2 5 4 gt gt gt np maximu
  • [svn]status命令

    wangyetao 64 linux u1604 LinuxRoom SVN FILE 个人空间 xx wangyetao 64 linux u1604 LinuxRoom SVN FILE 个人空间 xx svn help status
  • 维护型项目的管理

    最近 xff0c 一直在维护一个项目 项目很大 xff0c 有很多个系统相互配合 xff0c 且使用的语言也不一样 有JAVA写的系统 xff0c 有PHP写的 xff0c 各系统用的数据库也不一样 xff0c 还有一些我说不出来的技术 项
  • cas5.2.6 搭建cas服务端

    1 打包cas服务器端war包 下载cas overlay template 5 2 zip 1 1配置pom xml lt dependencies gt lt dependency gt lt groupId gt org apereo
  • PHP516 用phpize增加扩展PDO_OCI和OCI8

    环境 xff1a centos5 5 PHP5 1 6 oracle10 2 0 5 客户端 1 从oracle官网下载oracle客户端包 oracle instantclient basic 10 2 0 5 1 i386 rpm or
  • npm ERR! enoent This is related to npm not being able to find a file.解决

    一 问题描述 运行sudo npm install color name出现如下错误 xff1a npm ERR path root blog node modules color namenpm ERR code ENOENT npm E
  • ROS中最重要的变量$ROS_PACKAGE_PATH

    昨天刚成功安装了ardrone autonomy 和 tum ardrone xff0c 运行也是通过了 今天又尝试了一下昨天的命令 xff0c 结果发现tum ardrone居然又运行不了了 xff0c 郁闷 xff01 说是没有在环境变
  • 用TIKZ在LaTex中画图

    我之前是用Edraw max画图的 xff0c 但是有一个致命的问题就是在图上写字母的时候与图解释中不一致 xff0c 所以尝试了一下LaTex画图 xff0c 哎呀 xff0c 耗费我一下午的时间呀 首先导入包 xff1a usepack
  • NLP中三种特征抽取器的优与劣

    RNN LSTM GRU xff1a 缺点 xff08 1 xff09 xff1a 无法并行 xff0c 因此速度较慢 xff08 2 xff09 xff1a RNN无法很好地学习到全局的结构信息 xff0c 尤其对于序列结构很长的 CNN
  • python List中元素两两组合

    aa span class token operator 61 span span class token punctuation span span class token string 39 a 39 span span class t
  • JRE not compatible with project .class file compatibility: 1.7

    电脑上刚装了jdk1 7 xff0c 运行一般程序的时候没有出现什么问题 xff0c 由于内存不够用 xff0c 在设置虚拟内存时却出现问题 xff0c 如下 xff1a 还好找到了解决办法 xff0c 错误的原因是JRE库配置与Java
  • BufferedWriter 的 flush() 方法

    package com corpus import java io import java util List import edu stanford nlp ling HasWord import edu stanford nlp lin
  • 正则表达式匹配连续多个空格或tab空格

    Pattern p 61 Pattern compile 34 s 2 t 34 Matcher m 61 p matcher str String strNoBlank 61 m replaceAll 34 34 System out p
  • LaTex中插入花体字母

    特别要注意的是 xff1a 在LaTeX中 xff0c 别把希腊字母和英文的花体字母搞混哦 xff0c 哈哈 举个例子 xff1a 后面显示的 X 不是希腊字母 西 即 也就是说不能通过 Chi 的方式插入这个特殊符号 xff0c 正确的花
  • 气哭了的C++调试,cmake 找不到 eigen

    这才刚刚开头 xff0c 可是就是不知道错误在哪里 xff1f 百度了问题后 xff0c 打开了很多很多相关的解答 xff0c 从昨天上午遇到这个问题 xff0c 历经昨天下午和晚上 xff0c 还是错误 xff0c 终于在今天上午圆满解决
  • 对ORACLE SCN的理解

    1 SCN数值实际来源于系统的timestamp xff0c 这个实际可以证明 select current scn from v database select timestamp to scn sysdate from dual 这两个
  • Ubuntu 下 终端界面转图形界面

    在运行程序的时候 xff0c 错误的使用了快捷键 ctrl 43 alt 43 F10 然后 unbuntu就黑屏了 xff0c 整个界面只剩下左上角有一个白色的字符在闪 xff0c 然后 Ctrl 43 alt 43 F2时跳出终端的登录
  • python错误:TypeError: 'module' object is not callable

    TrainCorpusStructure py 文件中的代码如下 xff1a class TrainCorpusStructure inputs 61 Demo py中的代码如下 xff1a from corpusProcess impor