3

It's a game I'm making. Don't see the problems myself though.

Here's the error and the two .py files:

C:\Users\Rickard\My Programs\Python\slutarbete\New try>main.py
Traceback (most recent call last):
  File "C:\Users\Rickard\My Programs\Python\slutarbete\New try\main.py", line 6,
 in <module>
    from rabbits import Rabbit
  File "C:\Users\Rickard\My Programs\Python\slutarbete\New try\rabbits.py", line
 3, in <module>
    import main
  File "C:\Users\Rickard\My Programs\Python\slutarbete\New try\main.py", line 6,
 in <module>
    from rabbits import Rabbit
ImportError: cannot import name Rabbit

main.py

# -*- coding: utf-8 -*-

import pygame, sys, random, math
from rabbits import Rabbit
from pigs import Pig
from boars import Boar
from pygame.locals import *
from threading import Timer

pygame.init()
pygame.mixer.init()
mainClock = pygame.time.Clock()

WINDOW_WIDTH = 640
WINDOW_HEIGHT = 400

level = 1
while True:

...

And the rabbits.py file:

# -*- coding: utf-8 -*-
import pygame, sys, random, math
import main

class Rabbit(object):

    rabbitCounter = 0
    NEW_RABBIT = 40
    RABBIT_SIZE = 64

    ...

I sure can use some help with other obvious errors in this code.

Iguananaut
  • 21,810
  • 5
  • 50
  • 63
alsetalokin
  • 51
  • 1
  • 2
  • 8
  • 3
    Why are you importing `main` from `rabbits.py`? – BrenBarn Feb 01 '14 at 20:58
  • I removed the second error you added since it was unrelated to the first. You should consider asking for help on IRC or somewhere that can help with basic Python questions. In the meantime you should consider accepting one of the two answers you got for the original issue, both of which are correct and seem to have pointed you in the right direction. – Iguananaut Feb 01 '14 at 22:05
  • Possible duplicate of [ImportError: Cannot import name X](http://stackoverflow.com/questions/9252543/importerror-cannot-import-name-x) – kenorb Apr 28 '17 at 21:11

2 Answers2

14

You have a circular import. In your main module you try to import from rabbits. But from rabbits you import main. But main is not finished importing yet so that results in an ImportError when you try to import anything from the rabbits module.

I don't know why you have that import there, but you should restructure your modules so that rabbits does not require anything from main.

See also Circular (or cyclic) imports in Python

Also for any Python project consisting of more than one module you should make that into a package instead.

Community
  • 1
  • 1
Iguananaut
  • 21,810
  • 5
  • 50
  • 63
  • In the rabbits.py file I have a call to 2 integers in the main file `rabbits = [] for i in range (20): rabbits.append(pygame.Rect(random.randint(0, main.WINDOW_WIDTH - RABBIT_SIZE), random.randint (0, main.WINDOW_HEIGHT - RABBIT_SIZE), RABBIT_SIZE, RABBIT_SIZE))` – alsetalokin Feb 01 '14 at 21:08
  • Or pass any information about the screen dimensions that your class might need from main via the class's `__init__` or something of that sort. – Iguananaut Feb 01 '14 at 21:13
  • So confusing, look at these code modules and just tell me what to do. This is like my first program, but not the first coding, I've already had 120 hours of C++ courses. [The project](https://www.dropbox.com/sh/7s57lruhc9ykoz6/QqE1HxXJ08) – alsetalokin Feb 01 '14 at 21:19
  • I'm getting so many errors, laugh. I have the `WINDOW_WIDTH` and `WINDOW_HEIGHT` defined in a game.py file in the class game()\ __init__(self): And the rabbits.py is importing it with `from game import Game` – alsetalokin Feb 01 '14 at 21:53
12

The import statements in Python are executed as soon as Python reaches them in the file. In the case of this program, you run main.py, and Python sees

from rabbits import Rabbit

Python then goes to rabbits.py and sees

import main

This causes Python to loop back over to main.py. Python notices that it's already seen import statements for both of these modules and decides not to try importing again, to avoid infinite looping in cases like this.

The solution here is to refactor the code so that whatever rabbit.py needs is accessible from another module outside of main.

SimonT
  • 2,219
  • 1
  • 18
  • 32