gpt4 book ai didi

python 导入错误名称 lru_cache

转载 作者:行者123 更新时间:2023-11-28 18:19:52 25 4
gpt4 key购买 nike

在运行下面的代码时,我收到 Python 无法导入 lru_cache 的错误。但是,每次导入(数学等)都会发生这种情况。我已经尝试了我可以在互联网上找到的每一个教程来尝试让这个东西工作。我已经重新安装了 Python。还安装了 pip 和 homebrew。

#!usr/bin
from functools import lru_cache
import math

fibonacci_cache = {}

@lru_cache(maxsize = 1000)
def fibonacci(n):

if n == 1:
return 1
elif n == 2:
return 1
elif n > 2:
return fibonacci(n-1) + fibonacci(n-2)

for n in range(1, 501):
print(n, ":", fibonacci(n))

错误:

Kapoyas-MacBook-Pro:bin Keaton$ python python.py
Traceback (most recent call last):
File "python.py", line 2, in <module>
from functools import lru_cache
ImportError: cannot import name lru_cache

最佳答案

由于版本(python 2.7 或 3.x),在 documentation 中提供考虑使用:

try:
from functools import lru_cache
except ImportError:
from backports.functools_lru_cache import lru_cache

关于python 导入错误名称 lru_cache,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45819563/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com