AttributeError: 'module' object has no attribute Error












-1















So I've been working on this game. I have 2 files, Rooms.py and Monsters.py. When I run Rooms.py the error AttributeError: 'module' object has no attribute 'Monster' comes up. It's from the line Monsters.Monster.create_monsters(self) in the create_loot_monsters function. What is going on?



Bits ofRooms.py:



def create_15by15_rooms():
roomlist =
for i in range(-7 ,8):
e = -7
i + 1
for e in range(-7 ,8):
room = Room(i - 1, e, [i - 1, e], , )
e + 1
roomlist.append(room)
Room.create_loot_monsters(room)
return roomlist

def create_loot_monsters(self):
factor = random.randint(0, 100)
if factor <= 20:
pass
elif factor > 20 and factor <= 50:
pass #loot room (function to generate loot)
else:
Monsters.Monster.create_monsters(self)

return self


Bits of Monsters.py:



def create_monsters(self):
num = random.randint(0,200)
if num <= 20:
self.monsters = Monster.Slime
elif num > 20 and num <= 50:
self.monsters = Monster.Zombie
elif num > 50 and num <= 80:
self.monsters = Monster.Skeleton
elif num > 80 and num <= 120:
self.monsters = Monster.Infected
else:
self.monsters = Monster.Spirit
return self.monsters


EDIT: Imports from both:
Rooms.py



import random
import Monsters


Monsters.py



import random
import Rooms


So what is going on? Why does the function not work?










share|improve this question




















  • 1





    Welcome to SO! This looks like an import problem. Can you edit your question to show the import lines in your code?

    – Laurent S
    Nov 22 '18 at 9:59











  • Your only show the Monsters module. What is Monsters.Monsters? Do you have a class called Monsters inside the Monsters module?

    – MisterMiyagi
    Nov 22 '18 at 10:01











  • I have a class "Room" inside of Rooms.py and class "Monster" inside Monsters.py

    – The Super Block
    Nov 22 '18 at 10:15
















-1















So I've been working on this game. I have 2 files, Rooms.py and Monsters.py. When I run Rooms.py the error AttributeError: 'module' object has no attribute 'Monster' comes up. It's from the line Monsters.Monster.create_monsters(self) in the create_loot_monsters function. What is going on?



Bits ofRooms.py:



def create_15by15_rooms():
roomlist =
for i in range(-7 ,8):
e = -7
i + 1
for e in range(-7 ,8):
room = Room(i - 1, e, [i - 1, e], , )
e + 1
roomlist.append(room)
Room.create_loot_monsters(room)
return roomlist

def create_loot_monsters(self):
factor = random.randint(0, 100)
if factor <= 20:
pass
elif factor > 20 and factor <= 50:
pass #loot room (function to generate loot)
else:
Monsters.Monster.create_monsters(self)

return self


Bits of Monsters.py:



def create_monsters(self):
num = random.randint(0,200)
if num <= 20:
self.monsters = Monster.Slime
elif num > 20 and num <= 50:
self.monsters = Monster.Zombie
elif num > 50 and num <= 80:
self.monsters = Monster.Skeleton
elif num > 80 and num <= 120:
self.monsters = Monster.Infected
else:
self.monsters = Monster.Spirit
return self.monsters


EDIT: Imports from both:
Rooms.py



import random
import Monsters


Monsters.py



import random
import Rooms


So what is going on? Why does the function not work?










share|improve this question




















  • 1





    Welcome to SO! This looks like an import problem. Can you edit your question to show the import lines in your code?

    – Laurent S
    Nov 22 '18 at 9:59











  • Your only show the Monsters module. What is Monsters.Monsters? Do you have a class called Monsters inside the Monsters module?

    – MisterMiyagi
    Nov 22 '18 at 10:01











  • I have a class "Room" inside of Rooms.py and class "Monster" inside Monsters.py

    – The Super Block
    Nov 22 '18 at 10:15














-1












-1








-1








So I've been working on this game. I have 2 files, Rooms.py and Monsters.py. When I run Rooms.py the error AttributeError: 'module' object has no attribute 'Monster' comes up. It's from the line Monsters.Monster.create_monsters(self) in the create_loot_monsters function. What is going on?



Bits ofRooms.py:



def create_15by15_rooms():
roomlist =
for i in range(-7 ,8):
e = -7
i + 1
for e in range(-7 ,8):
room = Room(i - 1, e, [i - 1, e], , )
e + 1
roomlist.append(room)
Room.create_loot_monsters(room)
return roomlist

def create_loot_monsters(self):
factor = random.randint(0, 100)
if factor <= 20:
pass
elif factor > 20 and factor <= 50:
pass #loot room (function to generate loot)
else:
Monsters.Monster.create_monsters(self)

return self


Bits of Monsters.py:



def create_monsters(self):
num = random.randint(0,200)
if num <= 20:
self.monsters = Monster.Slime
elif num > 20 and num <= 50:
self.monsters = Monster.Zombie
elif num > 50 and num <= 80:
self.monsters = Monster.Skeleton
elif num > 80 and num <= 120:
self.monsters = Monster.Infected
else:
self.monsters = Monster.Spirit
return self.monsters


EDIT: Imports from both:
Rooms.py



import random
import Monsters


Monsters.py



import random
import Rooms


So what is going on? Why does the function not work?










share|improve this question
















So I've been working on this game. I have 2 files, Rooms.py and Monsters.py. When I run Rooms.py the error AttributeError: 'module' object has no attribute 'Monster' comes up. It's from the line Monsters.Monster.create_monsters(self) in the create_loot_monsters function. What is going on?



Bits ofRooms.py:



def create_15by15_rooms():
roomlist =
for i in range(-7 ,8):
e = -7
i + 1
for e in range(-7 ,8):
room = Room(i - 1, e, [i - 1, e], , )
e + 1
roomlist.append(room)
Room.create_loot_monsters(room)
return roomlist

def create_loot_monsters(self):
factor = random.randint(0, 100)
if factor <= 20:
pass
elif factor > 20 and factor <= 50:
pass #loot room (function to generate loot)
else:
Monsters.Monster.create_monsters(self)

return self


Bits of Monsters.py:



def create_monsters(self):
num = random.randint(0,200)
if num <= 20:
self.monsters = Monster.Slime
elif num > 20 and num <= 50:
self.monsters = Monster.Zombie
elif num > 50 and num <= 80:
self.monsters = Monster.Skeleton
elif num > 80 and num <= 120:
self.monsters = Monster.Infected
else:
self.monsters = Monster.Spirit
return self.monsters


EDIT: Imports from both:
Rooms.py



import random
import Monsters


Monsters.py



import random
import Rooms


So what is going on? Why does the function not work?







python






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 10:25









Alasdair

183k26311313




183k26311313










asked Nov 22 '18 at 9:56









The Super BlockThe Super Block

64




64








  • 1





    Welcome to SO! This looks like an import problem. Can you edit your question to show the import lines in your code?

    – Laurent S
    Nov 22 '18 at 9:59











  • Your only show the Monsters module. What is Monsters.Monsters? Do you have a class called Monsters inside the Monsters module?

    – MisterMiyagi
    Nov 22 '18 at 10:01











  • I have a class "Room" inside of Rooms.py and class "Monster" inside Monsters.py

    – The Super Block
    Nov 22 '18 at 10:15














  • 1





    Welcome to SO! This looks like an import problem. Can you edit your question to show the import lines in your code?

    – Laurent S
    Nov 22 '18 at 9:59











  • Your only show the Monsters module. What is Monsters.Monsters? Do you have a class called Monsters inside the Monsters module?

    – MisterMiyagi
    Nov 22 '18 at 10:01











  • I have a class "Room" inside of Rooms.py and class "Monster" inside Monsters.py

    – The Super Block
    Nov 22 '18 at 10:15








1




1





Welcome to SO! This looks like an import problem. Can you edit your question to show the import lines in your code?

– Laurent S
Nov 22 '18 at 9:59





Welcome to SO! This looks like an import problem. Can you edit your question to show the import lines in your code?

– Laurent S
Nov 22 '18 at 9:59













Your only show the Monsters module. What is Monsters.Monsters? Do you have a class called Monsters inside the Monsters module?

– MisterMiyagi
Nov 22 '18 at 10:01





Your only show the Monsters module. What is Monsters.Monsters? Do you have a class called Monsters inside the Monsters module?

– MisterMiyagi
Nov 22 '18 at 10:01













I have a class "Room" inside of Rooms.py and class "Monster" inside Monsters.py

– The Super Block
Nov 22 '18 at 10:15





I have a class "Room" inside of Rooms.py and class "Monster" inside Monsters.py

– The Super Block
Nov 22 '18 at 10:15












2 Answers
2






active

oldest

votes


















1














It looks like you have a circular import. Rooms.py imports from Monsters.py, but Monsters.py also imports from Rooms.py.



It's best to avoid circular imports like this. In the code you have shown, Monsters.py does not use anything from Rooms.py, so you should be able to fix the issue by removing the import Rooms import.



Note that the Python style guide suggests that you use lowercase for module names, e.g. rooms.py and monsters.py. This makes it easier to see the difference between your module rooms and your class Room in your code.






share|improve this answer































    0














    solution:



    change
    Monsters.Monster.create_monsters(self) to Monsters.Monster.create_monsters()
    and it should work



    checklist:



    1 make sure the Monsters.Monster.create_monsters() function accepts the instance of the the class from room.py



    reasons:




    Monsters.Monster.create_monsters(self)




    in this line seems you care calling the class function create_monsters.The parameter u r passing in is the entire class in the room.py(whatever that class is)
    ,while from the code Monster seems, you do not accept other parameters than the class it self.






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53428268%2fattributeerror-module-object-has-no-attribute-error%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      It looks like you have a circular import. Rooms.py imports from Monsters.py, but Monsters.py also imports from Rooms.py.



      It's best to avoid circular imports like this. In the code you have shown, Monsters.py does not use anything from Rooms.py, so you should be able to fix the issue by removing the import Rooms import.



      Note that the Python style guide suggests that you use lowercase for module names, e.g. rooms.py and monsters.py. This makes it easier to see the difference between your module rooms and your class Room in your code.






      share|improve this answer




























        1














        It looks like you have a circular import. Rooms.py imports from Monsters.py, but Monsters.py also imports from Rooms.py.



        It's best to avoid circular imports like this. In the code you have shown, Monsters.py does not use anything from Rooms.py, so you should be able to fix the issue by removing the import Rooms import.



        Note that the Python style guide suggests that you use lowercase for module names, e.g. rooms.py and monsters.py. This makes it easier to see the difference between your module rooms and your class Room in your code.






        share|improve this answer


























          1












          1








          1







          It looks like you have a circular import. Rooms.py imports from Monsters.py, but Monsters.py also imports from Rooms.py.



          It's best to avoid circular imports like this. In the code you have shown, Monsters.py does not use anything from Rooms.py, so you should be able to fix the issue by removing the import Rooms import.



          Note that the Python style guide suggests that you use lowercase for module names, e.g. rooms.py and monsters.py. This makes it easier to see the difference between your module rooms and your class Room in your code.






          share|improve this answer













          It looks like you have a circular import. Rooms.py imports from Monsters.py, but Monsters.py also imports from Rooms.py.



          It's best to avoid circular imports like this. In the code you have shown, Monsters.py does not use anything from Rooms.py, so you should be able to fix the issue by removing the import Rooms import.



          Note that the Python style guide suggests that you use lowercase for module names, e.g. rooms.py and monsters.py. This makes it easier to see the difference between your module rooms and your class Room in your code.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 '18 at 10:22









          AlasdairAlasdair

          183k26311313




          183k26311313

























              0














              solution:



              change
              Monsters.Monster.create_monsters(self) to Monsters.Monster.create_monsters()
              and it should work



              checklist:



              1 make sure the Monsters.Monster.create_monsters() function accepts the instance of the the class from room.py



              reasons:




              Monsters.Monster.create_monsters(self)




              in this line seems you care calling the class function create_monsters.The parameter u r passing in is the entire class in the room.py(whatever that class is)
              ,while from the code Monster seems, you do not accept other parameters than the class it self.






              share|improve this answer




























                0














                solution:



                change
                Monsters.Monster.create_monsters(self) to Monsters.Monster.create_monsters()
                and it should work



                checklist:



                1 make sure the Monsters.Monster.create_monsters() function accepts the instance of the the class from room.py



                reasons:




                Monsters.Monster.create_monsters(self)




                in this line seems you care calling the class function create_monsters.The parameter u r passing in is the entire class in the room.py(whatever that class is)
                ,while from the code Monster seems, you do not accept other parameters than the class it self.






                share|improve this answer


























                  0












                  0








                  0







                  solution:



                  change
                  Monsters.Monster.create_monsters(self) to Monsters.Monster.create_monsters()
                  and it should work



                  checklist:



                  1 make sure the Monsters.Monster.create_monsters() function accepts the instance of the the class from room.py



                  reasons:




                  Monsters.Monster.create_monsters(self)




                  in this line seems you care calling the class function create_monsters.The parameter u r passing in is the entire class in the room.py(whatever that class is)
                  ,while from the code Monster seems, you do not accept other parameters than the class it self.






                  share|improve this answer













                  solution:



                  change
                  Monsters.Monster.create_monsters(self) to Monsters.Monster.create_monsters()
                  and it should work



                  checklist:



                  1 make sure the Monsters.Monster.create_monsters() function accepts the instance of the the class from room.py



                  reasons:




                  Monsters.Monster.create_monsters(self)




                  in this line seems you care calling the class function create_monsters.The parameter u r passing in is the entire class in the room.py(whatever that class is)
                  ,while from the code Monster seems, you do not accept other parameters than the class it self.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 22 '18 at 10:32









                  陈海栋陈海栋

                  504




                  504






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53428268%2fattributeerror-module-object-has-no-attribute-error%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

                      ts Property 'filter' does not exist on type '{}'

                      mat-slide-toggle shouldn't change it's state when I click cancel in confirmation window