TypeError:'模块对象不可调用' [英] TypeError: 'module object is not callable'

查看:83
本文介绍了TypeError:'模块对象不可调用'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我是Python的新手,在尝试使用另一个文件中的一个类实例化一个对象时,收到了这个错误消息

目录并想知道我做错了什么。


我有一个Step.py类:

class Step(object)

def __init __(self,sName):

"初始化一个新的Step实例"

self.sName = sName

self.depSteps = []

self.remDepSteps = []

self.isCompleted = 0

然后我创建了一个新的同一目录中的文件名为

main.py:


import步骤

a = Step(" magn")


但是下面会产生错误

Traceback(最近一次调用最后一次):

文件" main.py",第3行,in?

a = Step(magn)

TypeError:''module''对象不可调用


如果有人能帮我指点正确的方向,如何解决这个

问题,我将不胜感激。

克里斯

Hi

I am new to Python and have recieved this error message when trying to
instantiate an object from a class from another file within the same
directory and wondered what I have done wrong.

I have a Step.py class:
class Step(object)
def __init__(self, sName):
"Initialise a new Step instance"
self.sName = sName
self.depSteps = []
self.remDepSteps = []
self.isCompleted = 0
Then I have created a new file within the same directory called
main.py:

import Step
a = Step("magn")

The following however generates the error
Traceback (most recent call last):
File "main.py", line 3, in ?
a = Step("magn")
TypeError: ''module'' object is not callable

If anyone could help point me in the right direction, how to fix this
problem it would be much appreciated.
Chris

推荐答案

2007年7月3日, ch ********* **** @ hotmail.com < ch ************* @ hotmail.comwrote:
On 9/3/07, ch*************@hotmail.com <ch*************@hotmail.comwrote:




我是Python的新手,在尝试使用同一个文件中的另一个文件从一个类实例化一个对象时收到了这个错误消息

目录并想知道我做错了什么。


我有一个Step.py类:

class Step(object)

def __init __(self,sName):
Hi

I am new to Python and have recieved this error message when trying to
instantiate an object from a class from another file within the same
directory and wondered what I have done wrong.

I have a Step.py class:
class Step(object)
def __init__(self, sName):


"初始化一个新的Step实例"

self。 sName = sName

self.depSteps = []

self.remDepSteps = []

self.isCompleted = 0


然后我创建了一个新的fil e在同一目录中名为

main.py:


import步骤

a = Step(" magn")


但是下面会产生错误

Traceback(最近一次调用最后一次):

文件" main.py",第3行,in?

a = Step(magn)

TypeError:''module''对象不可调用


如果有人能帮我指出正确的方向,如何解决这个

问题,我将不胜感激。

Chris
"Initialise a new Step instance"
self.sName = sName
self.depSteps = []
self.remDepSteps = []
self.isCompleted = 0
Then I have created a new file within the same directory called
main.py:

import Step
a = Step("magn")

The following however generates the error
Traceback (most recent call last):
File "main.py", line 3, in ?
a = Step("magn")
TypeError: ''module'' object is not callable

If anyone could help point me in the right direction, how to fix this
problem it would be much appreciated.
Chris



由于您对

名称感到困惑,因此提出了例外情况,即:您有一个名为Step的类。在名为Step.py的模块中。

并且通过导入模块唯一的步骤 python看到的是模块

而不是模块中声明的名称。


要访问模块中定义的名称,你可以这样做:


example1:

import步骤

a = Step.Step(" magn")#它指的是名称步骤。在模块中定义Step


示例2:
来自Step import的
步骤#您直接导入课程(并制作

名称在您的名字空间中可见)

a = Step(magn)

另请查看:
http://effbot.org/zone/import-confusion.htm
< a rel =nofollowhref =http://docs.python.org/ref/import.html\"target =_ blank> http://docs.python.org/ref/import.html

干杯,


-

----

Amit Khemka

网站: www.onyomo.com

wap -site: www.owap.in

The exception is being raised as you are being confused about the
names ie: you have a class named "Step" in a module named "Step.py" .
And by importing the module the only "Step" python sees is the module
not the names declared within the module.

To access a name defined in the module you can do :

example1:
import Step
a = Step.Step("magn") # It refers the name "Step" defined IN the module "Step"

example 2:
from Step import Step # You directly import the class ( and make the
name visible in your name space)
a = Step("magn")
Also have a look at:
http://effbot.org/zone/import-confusion.htm
http://docs.python.org/ref/import.html
Cheers,

--
----
Amit Khemka
website: www.onyomo.com
wap-site: www.owap.in


9月3日上午10点48分,christophert ... @ hotmail.com写道:
On Sep 3, 10:48 am, christophert...@hotmail.com wrote:




我是Python的新手并且已经收到了回复当尝试从同一个

目录中的另一个文件中的类实例化一个对象并且想知道我做错了什么时,我想知道这个错误消息。


我有一个Step.py类:

class Step(对象)

def __init __(self,sName):

初始化一个新的Step实例

self.sName = sName

self.depSteps = []

self.remDepSteps = []

self.isCompleted = 0

然后我在同一目录中创建了一个名为

main.py的新文件:


导入步骤

a =步骤(magn)


但是以下情况会产生错误

回溯(最近一次调用最后一次):

文件" main.py",第3行,在?

a = Step(" magn")

TypeError:''module''对象不可调用
Hi

I am new to Python and have recieved this error message when trying to
instantiate an object from a class from another file within the same
directory and wondered what I have done wrong.

I have a Step.py class:
class Step(object)
def __init__(self, sName):
"Initialise a new Step instance"
self.sName = sName
self.depSteps = []
self.remDepSteps = []
self.isCompleted = 0

Then I have created a new file within the same directory called
main.py:

import Step
a = Step("magn")

The following however generates the error
Traceback (most recent call last):
File "main.py", line 3, in ?
a = Step("magn")
TypeError: ''module'' object is not callable



这是因为当你问题导入步骤你实际上导入了

步骤 _module_,而不是步骤。模块里面的课程。


你可以用至少两种方式做到这一点:


------

import步骤

a = Step.Step(" magn")#访问Step模块中的Step类

------
<步骤导入步骤#在当前命名空间中插入Step类




------

>
a = Step(magn)

------

This is because when you issue "import Step" you''re actually importing
the "Step" _module_, not the "Step" class inside the module.

You can do this in at least two ways:

------
import Step
a = Step.Step("magn") # access the Step class inside Step module
------

or

------
from Step import Step # insert in the current namespace the Step class
a = Step("magn")
------


>

如果有人可以帮助我指出正确的方向,如何解决这个

问题,我将不胜感激。

Chris
>
If anyone could help point me in the right direction, how to fix this
problem it would be much appreciated.
Chris



HTH

HTH


ch ************* @ hotmail.com 写道:
ch*************@hotmail.com wrote:




我是Python的新手,在尝试使用同一个文件中的另一个文件从一个类实例化一个对象时收到了这个错误消息/>
目录并想知道我做错了什么。


我有一个Step.py类:

class Step(object)

def __init __(self,sName):

"初始化一个新的Step实例"

self.sName = sName

self.depSteps = []

self.remDepSteps = []

self.isCompleted = 0


然后我创建了同一目录中的一个新文件名为

main.py:


import步骤

a = Step(" magn")


但是下面会产生错误

Traceback(最近一次调用最后一次):

文件" main.py",第3行,在?

a = Step(magn)

TypeError:''module''对象不可调用


如果有人能帮助我指出正确的方向,如何解决这个问题,我将非常感激。
Hi

I am new to Python and have recieved this error message when trying to
instantiate an object from a class from another file within the same
directory and wondered what I have done wrong.

I have a Step.py class:
class Step(object)
def __init__(self, sName):
"Initialise a new Step instance"
self.sName = sName
self.depSteps = []
self.remDepSteps = []
self.isCompleted = 0
Then I have created a new file within the same directory called
main.py:

import Step
a = Step("magn")

The following however generates the error
Traceback (most recent call last):
File "main.py", line 3, in ?
a = Step("magn")
TypeError: ''module'' object is not callable

If anyone could help point me in the right direction, how to fix this
problem it would be much appreciated.



您是否有机会来自java背景?


您的实际问题可以通过以下方式解决:

导入步骤


Step.Step(" magn")


那是因为Step.py是一个模块,而_not_类 - 没有

文件类对应,因为它在例如Java。


Diez

Do you by any chance come from a java-background?

Your actual problem is simply solved by this:
import Step

Step.Step("magn")

That''s because Step.py is a module, and _not_ the class - there is no
file-class-correspondence as it is in e.g. Java.

Diez


这篇关于TypeError:'模块对象不可调用'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆