CR0SS0V3R
CR0SS0V3R

Reputation: 336

Importing from a module and avoiding having to make duplicate imports/variables

I once again find myself here, desperately begging for answers :)

So I have a problem regarding imports in python. I have my main code importing a secondary module that contains only a single class. I am trying to import and use this class without having to import anything inside of the secondary module. HOWEVER, Python is catching me and saying certain things don't exist etc etc like so.

  File "C:\Users\CR0SS0V3R\Desktop\Pathogen\mutations.py", line 24, in draw
pygame.circle.draw(screen, self.color, (self.posx, self.posy), 5, 0)
NameError: global name 'pygame' is not defined

Python is asking for me to import Pygame again. I just want the class code loaded from the secondary module so I can use it in my main program. I can not create another instance of screen as I must only use one for graphics being drawn to the screen. I would really love to avoid the redundancy of importing modules again. I want my programs clean, OOP wise. Is creating a separate file for classes too much to ask? Please help. I've been up all night looking for answers but to no avail.

Prepare for a wall of text.

This is my secondary module's entire code. (A single class.)

class RadiantRegrowth:

def __init__(self):
    self.name = "Radiant Regrowth"
    self.posx = 0
    self.posy = 0
    self.spawned = False
    self.color = (0,255,0)

def applyaffect(self, target):
    if self.spawned == True:
        target.health += 1
    else:
        pass

def spawn(self, x, y):
    spawnx = random.randint(50, x-50)
    spawny = random.randint(50, y+50)
    self.posx = spawnx
    self.posy = spawny
    self.spawned = True

def draw(self):
    pygame.draw.circle(screen, self.color, (self.posx, self.posy), 5, 0)

It's catching me at the very last line, regarding the use of pygame defined inside of my definition INSIDE of my class. And yes the code is probably cluttered and full of useless trivial bits, but at this point I just want something that limps.

I am importing it like so.

from mutations import *

And using a simple call (The program isn't even getting this far, FYI.) after a few negligible sections of code.

radiantregrowth = RadiantRegrowth()

This is the first call to the secondary module. And like I said; The program halts as soon as it tries to import the secondary module mutations due to the fact that it wants me to import pygame again, and set up the screen, again. I am NOT going to make anything Global.

Sorry for any redundancy. I am trying to keep this as clear as possible.

Upvotes: 0

Views: 923

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599866

If code inside your class wants to call functions from pygame, you need to import that name within the module it lives in. That is simply the way Python works.

You don't however want to set up a new screen instance. Instead, you should probably pass it to the class - either directly to the draw method, or to the class itself when you initialize it so that you store it as another instance attribute and refer to that within draw.

(And I believe it is pygame.draw.circle, not pygame.circle.draw.)

Upvotes: 1

Related Questions