Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Importing module error
#1
Hi everyone,

I stuck at a point in my code.

I have two files in a folder named gaming.py and char.py

These are some codes in gaming.py
import pygame,sys,random,time
import char

charclass=char.Char()

playerpos = charclass.get_position()
monsterpos = charclass.get_position()
And these are the codes of char.py
import gaming

class Char(object):
    
    def __init__(self):
        pass
    
    def get_position(self):
        while True:
            column = random.randint(2, mapheight - 3)
            row = random.randint(2, mapwidth - 3)
            if floormap[column][row] == "dirt":
                return [row, column]
I want to work with classes and split the project to the seperate python files like char,monsters etc. However when I try to import gaming.py to char.py and call get_position method it says;

Traceback (most recent call last):
File "c:\Users\...\Desktop\hello\gaming.py", line 2, in <module>
import char
File "c:\Users\...\Desktop\hello\char.py", line 1, in <module>
import gaming
File "c:\Users\...\Desktop\hello\gaming.py", line 129, in <module>
charclass=char.Char()
AttributeError: module 'char' has no attribute 'Char'

I couldn't solve this. Can you help?
Reply
#2
Looks like a circular import. Your gaming module imports char. Then your char module imports gaming. Then your gaming module tries to import char again. But Python thinks it's already done that and tries to access char.Char. But that hasn't been defined yet because char got stuck importing gaming.

It doesn't look like char needs gaming, so I would remove that import. Otherwise you need to move things between modules or create other modules to remove the circular import.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Hi,

When I remove "import gaming" from char.py, it says "Module has no attribute "Char" again Huh
Reply
#4
I don't get that error when I remove the import. I do get an error that random is not defined, because you need to import random into char (although from what you've posted you don't need it in gaming).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
I imported gaming into char.py to access the variables of gaming.py.

char.py
import random,pygame,Game

class Char():
    
    def __init__(self):
        pass
    
    def get_position(self,mapheight,mapwidth,floormap):
        while True:
            column = random.randint(2, mapheight - 3)
            row = random.randint(2, mapwidth - 3)
            if floormap[column][row] == "dirt":
                return [row, column]
    
    def bloock(self,ydir, xdir, pos, direct, nocrashdir,getblock,playerpos,floormapid,floormap):

        if getblock == Game.coal or getblock == Game.coal1 or getblock == Game.coal2:

            pygame.mixer.music.load("coal.wav")
            pygame.mixer.music.play()

            if getblock == Game.coal or Game.coal1:
                Game.click = floormapid[playerpos[1]+ydir][playerpos[0]+xdir]
                Game.click += 1
                floormapid[playerpos[1]+ydir][playerpos[0]+xdir] = Game.click
            if getblock == Game.coal and floormapid[playerpos[1]+ydir][playerpos[0]+xdir] == 4:
                floormap[playerpos[1]+ydir][playerpos[0]+xdir] = Game.coal1
            if getblock == Game.coal1 and floormapid[playerpos[1]+ydir][playerpos[0]+xdir] == Game.coaldur:
                floormap[playerpos[1]+ydir][playerpos[0]+xdir] = Game.coal2
            if getblock == Game.coal2:
                Game.click = 0
                floormap[playerpos[1]+ydir][playerpos[0]+xdir] = Game.dirt
When i run char.py, it is working perfectly, but char.py is not my main working module.It is gaming.py. However, when i run gaming.py it still says " module "char" has no attribute "Char" ".So, i am suppose to initialize gaming.py to play the game, not char.py. Isn't is true?

These are some important points of gaming.py. Maybe i am making a mistake at these points;

gaming.py
import pygame,sys,random,time
import char

pygame.init()
pygame.mixer.init()


tilesize = 50
...
charclass=char.Char()

playerpos = charclass.get_position(mapheight,mapwidth,floormap)
monsterpos = charclass.get_position(mapheight,mapwidth,floormap)
monster1pos = charclass.get_position(mapheight,mapwidth,floormap)

while True:

   for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

        elif event.type == pygame.KEYDOWN and event.key == pygame.K_RIGHT and playerpos[0] <= mapwidth - 1:
            if playerpos[0]==mapwidth-1:
                playerdir = playerright
            else:
                playerdir = playerright
                getblock = floormap[playerpos[1]][playerpos[0]+1]
                ydir = 0
                xdir = 1
                pos = 0
                direct = playerright
                nocrashdir = 1
                charclass.bloock(ydir, xdir, pos, direct, nocrashdir,getblock,playerpos,floormapid,floormap)
Reply
#6
I don't know what's going on. I don't have your full code (I don't even have Pygame), so I can't run all of it. But if I eliminate the imports I don't have, char imports and Char instantiates. You might have another variable char that is confusing things. You might have another file named char.py that is confusing things.

The way that you are importing things looks odd, and could cause other problems down the line. This is how I would recommend doing things. Have a separate file for each class, it's sub-classes, and any constants they need. None of them should have top level variables that are going to change, and none of the classes should have instances. If there is anything that two of those files need, but it into another file. Instances of the classes should only be made in other classes, or in a main program file responsible for running the overall code. These instances should be passed from class to class as attributes when needed.

For example, in my t_games project, there is a file for the Interface class. An instance of that is created in the main program file. The interface instance loads all of the game classes from their .py files, and creates an instance of the Human class from the player.py file. The interface instance creates an instance of the current game, passes the human instance to the game instance. The game instance creates instances of any AI players. These are defined in the game file, inheriting from the base classes in the player.py file. The human and the AI player instances are all passed a copy of the game instance so that they can interact with it.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  importing variables from module 8376459 1 245 Feb-18-2024, 02:24 PM
Last Post: deanhystad
  no module named 'docx' when importing docx MaartenRo 1 706 Dec-31-2023, 11:21 AM
Last Post: deanhystad
  pyarrow error when importing pandas sravva 1 883 Jun-06-2023, 05:09 PM
Last Post: snippsat
  My code displays too much output when importing class from a module lil_e 4 1,102 Oct-22-2022, 12:56 AM
Last Post: Larz60+
  Importing module in jupyter Noteboook ajitnayak1987 0 1,725 Jun-04-2021, 12:26 PM
Last Post: ajitnayak1987
  ERROR: importing desired module mbgamer28 0 1,648 Apr-05-2021, 07:46 PM
Last Post: mbgamer28
Bug Error while importing numpy Erfan 3 3,218 Nov-28-2020, 07:49 AM
Last Post: bowlofred
  importing module - not working jdhamblett 3 2,945 Jun-22-2020, 07:33 PM
Last Post: jdhamblett
  importing same python library in multiple custom module escape_freedom13 6 3,723 May-10-2020, 07:01 PM
Last Post: escape_freedom13
  vlc module error pythonprogrammer 1 2,819 Apr-23-2020, 04:16 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020