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()
Python ImportError: cannot import name ABC的更多相关文章
- python: ImportError: cannot import name 'Style' from 'openpyxl.styles' 解决方法
import os, openpyxl from openpyxl.styles import Font, Style os.chdir("C:\\") wb = openpyxl ...
- python import eventlet包时提示ImportError: cannot import name eventlet
root@zte-desktop:/home/ubuntu/python-threads# cat eventlet.py #!/usr/bin python import eventlet from ...
- Windows下Python安装: requires numpy+mkl 和ImportError: cannot import name NUMPY_MKL
最近写了一篇关于“微软开源分布式高性能GB框架LightGBM安装使用”的文章,有小伙伴安装Python环境遇到了问题.我个人也尝试安装了一下,确实遇到了很多问题."Windows7下pyt ...
- python 运行脚本报错 from keyword import iskeyword as _iskeyword ImportError: cannot import name iskeyword,说明python环境坏了,得重装,尚不知具体原因,
C:\Python27\Scripts>python task_test.pyTraceback (most recent call last): File "task_test.p ...
- Python模块Scrapy导入出错:ImportError: cannot import name xmlrpc_client
Mac(OS version: OS X Yosemite 10.10.5)上安装Scrapy模块,使用时出现: from six.moves import xmlrpc_client as xmlr ...
- [python] 关于错误 ImportError: cannot import name compile_command
我的pydev某一天开始就无法debug 了,执行debug 就会报 ImportError: cannot import name compile_command 原因居然是:我自己写了一个code ...
- 终于解决了python 3.x import cv2 “ImportError: DLL load failed: 找不到指定的模块” 及“pycharm关于cv2没有代码提示”的问题
终于解决了python 3.x import cv2 “ImportError: DLL load failed: 找不到指定的模块” 及“pycharm关于cv2没有代码提示”的问题 参考 :h ...
- 关于python pip安装第三方库 jieba 中文分词工具后提示"ImportError: cannot import name 'Random'"报错问题
具体错误提示如下: >>> import jieba Traceback (most recent call last): File "<stdin>" ...
- 在eclipse中用java调用python报错 Exception in thread "main" ImportError: Cannot import site module and its dependencies
最近做项目需要用java调用python,配置了jython后,运行了例子代码: 获得一个元组里面的元素: import org.python.util.PythonInterpreter; publ ...
随机推荐
- 在C++11编译环境中,简单自测了一下C++标准库中的string/vector和迭代器,记录一下
#include <iostream> #include <vector> using namespace std; int main() { //////////////// ...
- 一文看懂String类中的常用方法
1.int length(): 返回字符串的长度: return value.length 2.char charAt(int index): 返回某索引处的字符return value[index] ...
- sql语句异常向数据库插入数据报错
在php编程向数据库插入数据时报如下错误: [Err] 1064 - You have an error in your SQL syntax; check the manual that corre ...
- 安卓使用讯飞sdk报错
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.iflytek.cloud.SpeechSy ...
- POJ题目 1003Hangover(叠放纸牌)
POJ 1003 叠放纸牌 描述 您可以将多张纸牌悬在桌子上多远?如果您有一张卡,则可以创建一个最大长度为卡长的一半.(我们假设这些卡片必须垂直于桌子.)使用两张卡片,您可以使最上面的卡片悬垂在底部的 ...
- 【PHP数据结构】队列的相关逻辑操作
在逻辑结构中,我们已经学习了一个非常经典的结构类型:栈.今天,我们就来学习另外一个也是非常经典的逻辑结构类型:队列.相信不少同学已经使用过 redis . rabbitmq 之类的缓存队列工具.其实, ...
- PHP的Mhash扩展函数的学习
这次我们要学习的又是一个 Hash 加密扩展.不过这个扩展 Mhash 已经集成在了 Hash 扩展中.同时也需要注意的是,这个扩展已经不推荐使用了,我们应该直接使用 Hash 扩展中的函数来进行 H ...
- ecshop后台设置模板的地方显示自己新建模板的操作界面
我建立了一个叫test.dwt文件怎样在后台设置模板里面出现呢.1首先找到ecshop目录下的languages\zh_cn\admin/template.php 这个php文件 当然如果这只是简体中 ...
- Linux系列(10) - 命令搜索命令whereis与which
whereis 只能搜索系统命令,不能搜索自己凭空创建的普通文件 命令格式: whereis [命令名] 选项: -b:只查找可执行文件 -m:只查找帮助文件 which 搜索命令所在路径及别名:不是 ...
- 对代理IP进行检测是否可用
第一种方法是使用telnetlib import telnetlib import requests from lxml import etree #解析此url页面的IP url = 'http:/ ...