Question

I have a class, robot, in a file called robot.py

I want to create a robot instance in my main script like so:

in robot.py:

class robot(object):
    def __init__(self,pygame,screen_width,screen_height):

in main.py:

import pygame, sys, copy
import mouse
import robot
import menu
import button
from pygame.locals import *
...
size = [1200,900]
my_robot = robot(pygame,size[0]-400,size[1]-100)

error:

me:cup-robot Me$ python run.py
Traceback (most recent call last):
  File "run.py", line 24, in <module>
    my_robot = robot(pygame,size[0]-400,size[1]-100)
TypeError: 'module' object is not callable

how do I do this?

Was it helpful?

Solution

Two choices :

import robot and then prefix all with robot :

my_robot = robot.robot(pygame,size[0]-400,size[1]-100)

or

from robot import robot and then your code should work. But then, you can see that you won't be able to distinguish if robot is a module or the class, so you should not name the class and the file with the same name.

OTHER TIPS

Change your import statement:

from robot import robot

You have robot defined both as a module (robot.py) as well as a class. You need to import the class from the module so it is publicly available.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top