ImportError: cannot import name ‘_validate_lengths‘ from ‘numpy.lib.arraypad的解决方法(简单粗暴)
ImportError: cannot import name ‘_validate_lengths’ from 'numpy.lib.arraypad’解决方法
安装scikit-image库时,同时安装了numpy依赖库,运行某个程序时,出现上面的错误。
网上找了很多方法,有的说时版本太高了,但是安装了低版本也没有解决。直到在一篇博客找到了方法,虽然这个方法简单粗暴,但是好用的没得说。
cannot import name ‘_validate_lengths意思是不能导入这个函数,那就直接找到保存这个函数的所在文件把他给写进去就好了。
- 方法:
找到python环境下的这个路径的文件(arraypad.py), ……/python3.7/site-packages/numpy/lib/arraypad.py ,用记事本打开这个文件,复制拷贝下面函数,在文件末尾添加下面的函数保存即可,要重启环境,pycharm会自动更新。
--------------------------------------------------------------------------------------------------def _normalize_shape(ndarray, shape, cast_to_int=True):"""Private function which does some checks and normalizes the possiblymuch simpler representations of 'pad_width', 'stat_length','constant_values', 'end_values'.Parameters----------narray : ndarrayInput ndarrayshape : {sequence, array_like, float, int}, optionalThe width of padding (pad_width), the number of elements on theedge of the narray used for statistics (stat_length), the constantvalue(s) to use when filling padded regions (constant_values), or theendpoint target(s) for linear ramps (end_values).((before_1, after_1), ... (before_N, after_N)) unique number ofelements for each axis where `N` is rank of `narray`.((before, after),) yields same before and after constants for eachaxis.(constant,) or val is a shortcut for before = after = constant forall axes.cast_to_int : bool, optionalControls if values in ``shape`` will be rounded and cast to intbefore being returned.Returns-------normalized_shape : tuple of tuplesval => ((val, val), (val, val), ...)[[val1, val2], [val3, val4], ...] => ((val1, val2), (val3, val4), ...)((val1, val2), (val3, val4), ...) => no change[[val1, val2], ] => ((val1, val2), (val1, val2), ...)((val1, val2), ) => ((val1, val2), (val1, val2), ...)[[val , ], ] => ((val, val), (val, val), ...)((val , ), ) => ((val, val), (val, val), ...)"""ndims = ndarray.ndim# Shortcut shape=Noneif shape is None:return ((None, None), ) * ndims# Convert any input `info` to a NumPy arrayshape_arr = np.asarray(shape)try:shape_arr = np.broadcast_to(shape_arr, (ndims, 2))except ValueError:fmt = "Unable to create correctly shaped tuple from %s"raise ValueError(fmt % (shape,))# Cast if necessaryif cast_to_int is True:shape_arr = np.round(shape_arr).astype(int)# Convert list of lists to tuple of tuplesreturn tuple(tuple(axis) for axis in shape_arr.tolist())def _validate_lengths(narray, number_elements):"""Private function which does some checks and reformats pad_width andstat_length using _normalize_shape.Parameters----------narray : ndarrayInput ndarraynumber_elements : {sequence, int}, optionalThe width of padding (pad_width) or the number of elements on the edgeof the narray used for statistics (stat_length).((before_1, after_1), ... (before_N, after_N)) unique number ofelements for each axis.((before, after),) yields same before and after constants for eachaxis.(constant,) or int is a shortcut for before = after = constant for allaxes.Returns-------_validate_lengths : tuple of tuplesint => ((int, int), (int, int), ...)[[int1, int2], [int3, int4], ...] => ((int1, int2), (int3, int4), ...)((int1, int2), (int3, int4), ...) => no change[[int1, int2], ] => ((int1, int2), (int1, int2), ...)((int1, int2), ) => ((int1, int2), (int1, int2), ...)[[int , ], ] => ((int, int), (int, int), ...)((int , ), ) => ((int, int), (int, int), ...)"""normshp = _normalize_shape(narray, number_elements)for i in normshp:chk = [1 if x is None else x for x in i]chk = [1 if x >= 0 else -1 for x in chk]if (chk[0] < 0) or (chk[1] < 0):fmt = "%s cannot contain negative values."raise ValueError(fmt % (number_elements,))return normshp
思路来源于这篇博主,
ImportError: cannot import name ‘_validate_lengths‘ from ‘numpy.lib.arraypad的解决方法(简单粗暴)相关推荐
- ImportError: cannot import name ‘_validate_lengths‘ from ‘numpy.lib.arraypad‘完美解决方法
报错原因 numpy版本与skimage版本不匹配 解决方法 打开Anaconda3的arraycrop.py,该文件我是在这里的C:\ProgramData\Anaconda3\Lib\site-p ...
- [bug解决] cannot import name ‘_validate_lengths‘ from ‘numpy.lib.arraypad‘
文章目录 问题描述: 原因分析: 解决方案: 方案1(不推荐): 方案2(推荐): 解决步骤: 可能遇到的报错 问题描述: skimage 报错 from skimage import io 报错信息 ...
- scikit-image安装 from numpy.lib.arraypad import _validate_lengths ImportError: cannot import name ‘_va
[写在前面]提示没有skimage.io 安装界面如下 [报错] from numpy.lib.arraypad import _validate_lengths ImportError: canno ...
- 解决“ImportError: cannot import name ‘_validate_lengths‘”问题
问题描述 在运行程序的时候报错: Traceback (most recent call last):File "demo_heat_map.py", line 2, in < ...
- 运行tensorflow程序,出现ImportError: cannot import name '_validate_lengths'错误的解决办法
运行tensorflow程序,出现ImportError: cannot import name '_validate_lengths'错误的解决办法 如下图: 原因 问题原因:这是skimage版本 ...
- react 中使用import()实现按需加载报错 解决方法 --‘import’ and ‘export’ may only appear at the top level
react 中使用import()实现按需加载报错 解决方法 --'import' and 'export' may only appear at the top level 参考文章: (1)rea ...
- 缺少lib库文件解决方法
缺少lib库文件解决方法 1.缺少依赖包ld-linux.so.2 /lib/ld-linux.so.2: bad ELF interpreter: 没有那个文件或目录 yum install gli ...
- Unable to import maven project: See logs for details错误解决方法
Unable to import maven project: See logs for details错误解决方法 Unable to import maven project: See logs ...
- 关于报错ImportError: cannot import name ‘AbstractKeyedTuple‘ from ‘sqlalchemy.util._collections‘的解决
导入:from sqlalchemy.util._collections import AbstractKeyedTuple 报错信息:ImportError: cannot import name ...
- python使用from Crypto.Random import random时候出现winrandom导入失败的解决方法
问题描述:今天在使用Crypto.Random.random的时候出现错误,具体错误原因如下: >>> from Crypto.Random import random Traceb ...
最新文章
- Android library module生成aar文件
- Windows 10下安装Anaconda(Anaconda3-5.1.0)
- 适配器模式理解和使用
- UVA 220 Othello
- TensorFlow:实战Google深度学习框架(六)图像数据处理
- javaScript获取url中的参数
- 用代码获取Oracle服务名清单
- Quartz2D指定显示范围
- 极简代码(六)—— 返回 0/1 构成的布尔向量
- [论文笔记]ALBERT: A LITE BERT FOR SELF-SUPERVISED LEARNING OF LANGUAGE REPRESENTATIONS
- Python-实现九宫格
- CORS Filter
- STM32F103 - 延迟函数 -unfinished -unfinished-unfinished
- 培训机构让考java证书有必要吗在线等
- ESD防护选型思路(二)
- GC8418 数字光纤音频解码芯片 光纤解码芯片 CS8418替代 MS8413替代
- 王者荣耀高清壁纸脚本Python文件
- SAP 散装物料作用及设置
- 心态好的人,都有这5种生活方式
- 计算机网络 概述重点(全)