关于MacOS:python模块在2.7上工作,而不是在3.5上工作

Python module working on 2.7 but not on 3.5

本问题已经有最佳答案,请猛点这里访问。

这是我的代码:

Animals/__init__.py

1
2
3
from Mammals import Mammals

from Bird import Bird

动物/哺乳动物.py

1
2
3
4
5
6
7
8
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)

动物/鸟

1
2
3
4
5
6
7
8
9
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)

Py

1
2
3
4
5
6
7
8
from Animals import Mammals, Bird

mam = Mammals()
bird = Bird()

mam.print_members()

bird.print_members()

我已经安装了python 3(macosx),并将其与virtualenv一起使用。这个代码在2.7中可以正常工作,但在python3.5中不起作用。它总是给EDOCX1[1]


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

您的代码在python2中运行,因为解析器暗示了BirdsMammals的相对导入,但是python3停止了这样做。

运行2to3,您的文件将修复它。

1
2
from .Mammals import Mammals
from .Bird import Bird