0

I am trying to learn how to change my programs so that they use code from multiple python scripts. I have two scripts (these are big files, so cutting them down to only what's needed)

main.py

import pygame
import player #Imports the player.py script
p1 = hero("woody.png",[2,2]) #Creates an instance of hero

player.py

import pygame
import main

class hero:
    def __init__(self,image,speed):
        self.image = pygame.image.load(image)
        self.speed = speed
        self.pos = self.image.get_rect()

Running this gives me the following error:

AttributeError: 'module' object has no attribute 'hero'

I'm not quite understanding why it's trying to get an attribute instead of creating an instance. I've tried looking at other examples and how they are solving the problem but when I attempt to apply it to the code above it does not solve my issue.

4
  • 1
    from player import hero ?? Commented Aug 10, 2016 at 4:09
  • Why are you importing main in player??? Commented Aug 10, 2016 at 4:09
  • 5
    you are importing player in main and main in player. That usually causes problems as it creates a kind of loop. Commented Aug 10, 2016 at 4:12
  • Give us the whole error Traceback. As written, I would expect a NameError for hero Commented Aug 10, 2016 at 4:24

3 Answers 3

1
  1. To import hero from another module, you should write player.hero, or simply from player import hero.

  2. Importing player in main and main in player would cause "circular references".


Here is the modified code:

main.py

import pygame
from player import hero # Imports the player.py script

p1 = hero("woody.png",[2,2]) # Creates an instance of hero

player.py

import pygame    

class hero:
    def __init__(self,image,speed):
        self.image = pygame.image.load(image)
        self.speed = speed
        self.pos = self.image.get_rect()#.....etc
Sign up to request clarification or add additional context in comments.

3 Comments

Sorry dude. Didn't mean do edit your question. Thought is was mine.
@Mr.Python I've approved your edit, so never mind :)
Ok thanks. Fingers just got ahead of the brain for a second :)
0

Like Athena said above, don't import main into player and player into main. This causes a import loop. Just simply import player into main

Secondly, you must say player.hero() if you want to use the class hero from the player module. Or if you want to just say hero(), you can say from player import*. This tells python to import all the files from player into the namespace main.

Be careful using this though, as a function or class from your player file may conflict with an already exiting function or class, with the same name.

And as a side note, classes in python general have their first letter capitalized.

here is how your files should look:

main.py

import pygame
import player #Imports the player.py script
p1 = hero("woody.png",[2,2]) #Creates an instance of hero

player.py

import pygame

class hero:
    def __init__(self,image,speed):
        self.image = pygame.image.load(image)
        self.speed = speed
        self.pos = self.image.get_rect()#.......etc

4 Comments

I don't get why people suggest import *, then have to give a warning. There are two perfectly good (and better) ways of doing it. player.hero() and hero() after importing hero from player. python.org/dev/peps/pep-0020 ;)
@JamesMcCormac Well that may be a viable option for him. I'm just giving him a warning. I'll let him choose which works best.
Well given the OP is having trouble with imports it might be best to suggest the two explicit approaches and ignore the implicit?
@JamesMcCormac yeah I guess in the end, it might be best to steer him to two explicit approaches, to avoid confusion. But it doesn't hurt to how him more a advanced approach.
0

Drop the import main in player.py and change the last line in main.py to:

p1 = player.hero("woody.png",[2,2])

Edit: Python does not know what class/function hero is. It needs you to tell it hero is a class in the player module. And that's what player.hero means.

Also never import one module from another and vice versa. You can get an import loop which is very hard to debug.

Lastly, in python it is common to name a class with a capital letter as Hero instead of hero.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.