2017-07-27 33 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) 
相關問題