Python 导入错误:无法导入名称“_validate_lengths”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/54241226/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ImportError: cannot import name '_validate_lengths'
提问by John
I have started learning Tensorflow. I am using Pycharm and my environment is Ubuntu 16.04. I am following the tutorial. I cross check the nump. It is up-to-date. I don't know the reason of this error.
我已经开始学习 Tensorflow。我正在使用 Pycharm,我的环境是 Ubuntu 16.04。我正在关注教程。我交叉检查了数字。它是最新的。我不知道这个错误的原因。
from numpy.lib.arraypad import _validate_lengths
ImportError: cannot import name '_validate_lengths'
从 numpy.lib.arraypad 导入 _validate_lengths
导入错误:无法导入名称“_validate_lengths”
Need hint to resolve this error. Thank you.
需要提示来解决此错误。谢谢你。
import tensorflow as tf
from skimage import transform
from skimage import data
import matplotlib.pyplot as plt
import os
import numpy as np
from skimage.color import rgb2gray
import random
#listdir: This method returns a list containing the names of the entries in the directory given by path.
# Return True if path is an existing directory
def load_data(data_dir):
# Get all subdirectories of data_dir. Each represents a label.
directories = [d for d in os.listdir(data_dir)
if os.path.isdir(os.path.join(data_dir, d))]
# Loop through the label directories and collect the data in
# two lists, labels and images.
labels = []
images = []
for d in directories:
label_dir = os.path.join(data_dir, d)
file_names = [os.path.join(label_dir, f)
for f in os.listdir(label_dir)
if f.endswith(".ppm")]
for f in file_names:
images.append(data.imread(f))
labels.append(int(d))
return images, labels
ROOT_PATH = "/home/tahir/PhD Study/Traffic Signs Using Tensorflow/"
train_data_dir = os.path.join(ROOT_PATH, "TrafficSigns/Training")
test_data_dir = os.path.join(ROOT_PATH, "TrafficSigns/Testing")
images, labels = load_data(train_data_dir)
# Print the `images` dimensions
print(images.ndim)
# Print the number of `images`'s elements
print(images.size)
# Print the first instance of `images`
images[0]
回答by Wey Shi
I updated my skimage package.
我更新了我的 skimage 包。
pip install --upgrade scikit-image
And the problem was solved. It's a problem of version of Skimage, which is solved in 0.14.2. PLus, this version is quite stable.
问题就解决了。是Skimage版本的问题,0.14.2解决了。另外,这个版本相当稳定。
Installing collected packages: dask, scikit-image
Found existing installation: dask 0.19.1
Uninstalling dask-0.19.1:
Successfully uninstalled dask-0.19.1
Found existing installation: scikit-image 0.13.0
Uninstalling scikit-image-0.13.0:
Successfully uninstalled scikit-image-0.13.0
Successfully installed dask-1.0.0 scikit-image-0.14.2
回答by Alka
scikit-image 0.14.2 worked with numpy 1.16.3. I installed numpy first and then installed scikit image
scikit-image 0.14.2 与 numpy 1.16.3 一起使用。我先安装了numpy,然后安装了scikit image
回答by eaithy
回答by ErenO
I had the same error, I did the following steps:
我有同样的错误,我做了以下步骤:
uninstall scikit-image
卸载 scikit-image
pip uninstall scikit-image
or
或者
conda uninstall scikit-image
and then
进而
pip install scikit-image
or
或者
conda install -c conda-forge scikit-image
回答by Guy Gaziv
For me the magic dependency was:
对我来说,神奇的依赖是:
pip install scikit-image==0.13.1
pip install numpy==1.15
For python 3.5 and python 3.6
对于 python 3.5 和 python 3.6
回答by JiangKui
upgrade scikit-image to latest, OR downgrade NumPy to 1.15.
将 scikit-image 升级到最新版本,或者将 NumPy 降级到 1.15。
pip install -U scikit-image
or
或者
pip install numpy==1.15
if the pip isn't latest. You may need this:
如果 pip 不是最新的。你可能需要这个:
python -m pip install --upgrade pip
refer to Getting ImportError: cannot import name '_validate_lengths' #3906.
回答by user12997402
Seems I also had the same issue due to two versions installed sametime.
由于同时安装了两个版本,我似乎也遇到了同样的问题。
I solved it by uninstalling scikit-image multiple times till none was left.
我通过多次卸载 scikit-image 来解决它,直到没有留下任何东西。
pip uninstall scikit-image
Then Reinstall:
然后重新安装:
pip uninstall scikit-image
It worked for me.
它对我有用。