2

Here is my code :

Animals/__init__.py

from Mammals import Mammals

from Bird import Bird

Animals/Mammals.py

class Mammals(object):

    def __init__(self):
        self.members = ['Tiger', 'Elephant','Wild Cat']

    def print_members(self):
        for member in self.members :
            print('this is a member :' + member)

Animals/Bird.py

class Bird(object):

    def __init__(self):
        self.birds = ['sparrow','robbin','duck']

    def print_members(self):
        print('printing birds in bird class')
        for bird in self.birds:
            print('this is a bird '+ bird)

test.py

from Animals import Mammals, Bird

mam = Mammals()
bird = Bird()

mam.print_members()

bird.print_members()     

I have installed Python 3 (MacOSX) and I am using it with virtualenv. This code works fine with 2.7, but it doesn't work with python3.5. It always gives ImportError: No module named Mammals

Community
  • 1
  • 1
vidyasagarr7
  • 513
  • 2
  • 6
  • 12

1 Answers1

3

Python 3 makes a distinction between relative and absolute imports, dropping support for implicit relative imports.

Your code runs in python2 because the parser implies relative imports for Birds and Mammals, but python3 stops doing this.

Run 2to3 for your files will fix it.

from .Mammals import Mammals
from .Bird import Bird
gdlmx
  • 6,479
  • 1
  • 21
  • 39