Python ImportError: cannot import name ABC

Python 3.5.2 測試能夠運行python

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()

輸出web

MyClass func

python 2.7.12測試能夠運行shell

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()

輸出svg

MyClass func

上面python2的代碼在python3中也能夠正常輸出.
還有一種3.0-3.3的寫法 不寫了.測試

兼容後的代碼spa

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()
相關文章
相關標籤/搜索