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的解决方法(简单粗暴)相关推荐

  1. ImportError: cannot import name ‘_validate_lengths‘ from ‘numpy.lib.arraypad‘完美解决方法

    报错原因 numpy版本与skimage版本不匹配 解决方法 打开Anaconda3的arraycrop.py,该文件我是在这里的C:\ProgramData\Anaconda3\Lib\site-p ...

  2. [bug解决] cannot import name ‘_validate_lengths‘ from ‘numpy.lib.arraypad‘

    文章目录 问题描述: 原因分析: 解决方案: 方案1(不推荐): 方案2(推荐): 解决步骤: 可能遇到的报错 问题描述: skimage 报错 from skimage import io 报错信息 ...

  3. 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 ...

  4. 解决“ImportError: cannot import name ‘_validate_lengths‘”问题

    问题描述 在运行程序的时候报错: Traceback (most recent call last):File "demo_heat_map.py", line 2, in < ...

  5. 运行tensorflow程序,出现ImportError: cannot import name '_validate_lengths'错误的解决办法

    运行tensorflow程序,出现ImportError: cannot import name '_validate_lengths'错误的解决办法 如下图: 原因 问题原因:这是skimage版本 ...

  6. 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 ...

  7. 缺少lib库文件解决方法

    缺少lib库文件解决方法 1.缺少依赖包ld-linux.so.2 /lib/ld-linux.so.2: bad ELF interpreter: 没有那个文件或目录 yum install gli ...

  8. 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 ...

  9. 关于报错ImportError: cannot import name ‘AbstractKeyedTuple‘ from ‘sqlalchemy.util._collections‘的解决

    导入:from sqlalchemy.util._collections import AbstractKeyedTuple 报错信息:ImportError: cannot import name ...

  10. python使用from Crypto.Random import random时候出现winrandom导入失败的解决方法

    问题描述:今天在使用Crypto.Random.random的时候出现错误,具体错误原因如下: >>> from Crypto.Random import random Traceb ...

最新文章

  1. Android library module生成aar文件
  2. Windows 10下安装Anaconda(Anaconda3-5.1.0)
  3. 适配器模式理解和使用
  4. UVA 220 Othello
  5. TensorFlow:实战Google深度学习框架(六)图像数据处理
  6. javaScript获取url中的参数
  7. 用代码获取Oracle服务名清单
  8. Quartz2D指定显示范围
  9. 极简代码(六)—— 返回 0/1 构成的布尔向量
  10. [论文笔记]ALBERT: A LITE BERT FOR SELF-SUPERVISED LEARNING OF LANGUAGE REPRESENTATIONS
  11. Python-实现九宫格
  12. CORS Filter
  13. STM32F103 - 延迟函数 -unfinished -unfinished-unfinished
  14. 培训机构让考java证书有必要吗在线等
  15. ESD防护选型思路(二)
  16. GC8418 数字光纤音频解码芯片 光纤解码芯片 CS8418替代 MS8413替代
  17. 王者荣耀高清壁纸脚本Python文件
  18. SAP 散装物料作用及设置
  19. 心态好的人,都有这5种生活方式
  20. 计算机网络 概述重点(全)

热门文章

  1. 涉及欧拉常数的一道数学题
  2. zabbix3.2.7升级到zabbix3.4.1
  3. Spring——DI
  4. python3怎么将函数的用法通过help导出到文件
  5. 推荐几款网页截图工具可以全屏截图,也可对图片编辑
  6. atomic原子类实现机制_atomic实现原理
  7. 嵌入式系统上电,程序的运行过程
  8. 与现代教育技术有关的计算机知识,基于计算机的现代教育技术手段的利用与开发...
  9. ETL——实现Kettle作业定时任务
  10. js使用在指定数据前面或后面插入数据,对List数据排序