Python ImportError: cannot import name ABC

Python 3.5.2 测试可以运行

import sys                                                                      
from abc import ABC,abstractmethod                                              
                                                                                
class MyBase(ABC):                                                              
    @abstractmethod                                                             
    def func(self):                                                             
        '''Implement in subclass'''                                             
                                                                                
class MyClass(MyBase):                                                          
    def func(self):                                                             
        con = "%s %s" % (self.__class__.__name__,sys._getframe().f_code.co_name)
        print(con)                                                              
                                                                                
obj = MyClass()                                                                 
obj.func() 

输出

MyClass func

python 2.7.12测试可以运行

import sys                                                                                                                  
from abc import ABCMeta, abstractmethod                                         
                                                                                
class MyBase():                                                                 
    __metaclass__ = ABCMeta                                                     
    @abstractmethod                                                             
    def func(self):                                                             
        '''Implement in subclass'''                                             
                                                                                
class MyClass(MyBase):                                                          
    def func(self):                                                             
        con = "%s %s" % (self.__class__.__name__,sys._getframe().f_code.co_name)
        print(con)                                                              
                                                                                
obj = MyClass()                                                                 
obj.func()  

输出

MyClass func

上面python2的代码在python3中也可以正常输出.
还有一种3.0-3.3的写法 不写了.

兼容后的代码

from __future__ import absolute_import                                          
from __future__ import division                                                 
from __future__ import print_function                                           
                                                                                
import sys                                                                      
if sys.version > '3':                                                           
    from abc import ABC,abstractmethod                                          
else:                                                                           
    from abc import ABCMeta, abstractmethod                                     
                                                                                
if sys.version > '3':                                                           
    class MyBase(ABC):                                                          
        @abstractmethod                                                         
        def func(self):                                                         
            '''Implement in subclass'''                                         
else:                                                                           
    class MyBase():                                                             
        __metaclass__ = ABCMeta                                                 
        @abstractmethod                                                         
        def func(self):                                                         
            '''Implement in subclass'''                                         
                                                                                
class MyClass(MyBase):                                                          
    def func(self):                                                             
        con = "%s %s" % (self.__class__.__name__,sys._getframe().f_code.co_name)
        print(con)                                                              
                                                                                
obj = MyClass()                                                                 
obj.func()

转载于:https://www.cnblogs.com/vercont/p/10210172.html

你可能感兴趣的:(Python ImportError: cannot import name ABC)