2017-07-27 220 views
-1
from functools import reduce 

我使用python 3.6.2,这是显示以下错误的唯一代码:python3.6导入错误:无法导入名称“减少”

Traceback (most recent call last): File "D:\Pythons\oop.py", line 50, in from functools import reduce
ImportError: cannot import name 'reduce' Process returned 1 (0x1) execution time : 0.145 s

我会找到这个问题因为我在做另一个代码错误,

from enum import Enum

它报告的错误:

Traceback (most recent call last): File "D:\Pythons\oop.py", line 50, in from enum import Enum File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\enum.py", line 3, in from functools import reduce ImportError: cannot import name 'reduce'

所以我只是看着enum. Py源,在3线

from functools import reduce

女士们,先生们发现,在centos7.2安装python3.6.2完全是出于什么问题,但在安装了Windows 10专业版,会出现上面的这些问题,好像我安装了这个问题,但是,很多次我卸载了,反复安装了很多次,还是不行,不知道没有这些文件,谁能告诉我如何通过在Windows环境下的命令行来安装它?

+0

所以Python不能从'functools'导入'reduce'。我无法在python 3中找到名为reduce的类/方法。https://github.com/python/cpython/blob/3.6/Lib/functools.py - 这是在Python 2中可用的https:// docs。 python.org/2/library/functools.html#functools.reduce –

+0

该代码在https://www.python.org/shell/上使用版本3.6.0时正常工作。也许重新安装,或使用所有python软件包进行完整安装? – Sheldon

+0

我刚刚安装了3.6.2,我无法复制。 'reduce'甚至列在[文档页面](https://docs.python.org/3/library/functools.html#functools.reduce) – Wondercricket

回答

0

Python 3.6应该减少functools。要调试您的问题,试试这个:

import functools 
for obj in dir(functools): 
    print(obj) 

我希望类似的输出(试图在这里:https://www.python.org/shell/):

MappingProxyType 
RLock 
WRAPPER_ASSIGNMENTS 
WRAPPER_UPDATES 
WeakKeyDictionary 
_CacheInfo 
_HashedSeq 
__all__ 
__builtins__ 
__cached__ 
__doc__ 
__file__ 
__loader__ 
recursive_repr 
__name__ 
__package__ 
__spec__ 
_c3_merge 
_c3_mro 
_compose_mro 
_convert 
_find_impl 
_ge_from_gt 
_ge_from_le 
_ge_from_lt 
_gt_from_ge 
_gt_from_le 
_gt_from_lt 
_le_from_ge 
_le_from_gt 
_le_from_lt 
_lru_cache_wrapper 
_lt_from_ge 
_lt_from_gt 
_lt_from_le 
_make_key 
cmp_to_key 
get_cache_token 
lru_cache 
namedtuple 
partial 
partialmethod 
recursive_repr 
reduce 
singledispatch 
total_ordering 
update_wrapper 
wraps 

我的猜测是超过减少将会丢失。无论如何,它看起来像是一个卸载而不是重新安装。您可能意外地编辑了文件或以某种方式损坏了文件。有时IDE可能会将您带到该功能,并且偶然编辑它会很容易。

+0

__builtins__ __cached__ __doc__ __file__ __loader__ __name__ __PACKAGE__ __spec__ functools我只是表明, – myxxqy

0

不要照顾这些错误。只是尝试使用functools在你的代码:

import functools 

# Create a list of strings: stark 
stark = ['robb', 'sansa', 'arya', 'eddard', 'jon'] 

# Use reduce() to apply a lambda function over stark: result 
result = functools.reduce((lambda item1,item2:item1 + item2), stark) 

或类似的:

# Import reduce from functools 
from functools import reduce 

# Create a list of strings: stark 
stark = ['robb', 'sansa', 'arya', 'eddard', 'jon'] 

# Use reduce() to apply a lambda function over stark: result 
result = reduce((lambda item1,item2:item1 + item2), stark) 
相关问题