前言
我們都知道
cookiecutter
是一個非常方便的基於Python
的項目模板工具,它就相當於一些IDE工具的創建項目工程的嚮導工具,創建的工程具有統一的結構。
背景
同事之前寫了一個golang
的項目模板,今天在使用cookiecutter
創建新工程時,出現了問題Unable to load extension: No module named functools_lru_cache
。詢問身邊的同事有沒有碰到這樣的問題,得到的答覆是沒有。他們用的系統都是Mac OS
。而我用的是Ubuntu
。我的系統版本是Ubuntu 18.04.4 LTS
。
解決過程
看提示像是在ubuntu系統上cookiecutter
缺少了依賴functools_lru_cache
,很可能是沒有安裝它。於是就執行下面的命令去安裝functools_lru_cache
。
$ pip install backports.functools_lru_cache
然後再去執行:
$ cookiecutter git@XXX/go-swagger-template.git
幾秒鐘後,問題依然存在:
Unable to load extension: No module named functools_lru_cache
然後就開始拿出了面向google編程的本領,開始了一頓搜索。搜索結果就是很多人在Ubuntu上都遇到過這個問題。在github上一個開源項目裏,就有人提了這樣一個issue[^1].
在這個issue[^1]裏,有人提出卸載functools_lru_cache
後再重新安裝。並有一些人嘗試之後確實解決了這個問題。
$ pip uninstall backports.functools_lru_cache
$ pip install backports.functools_lru_cache
我也安裝這個方式嘗試了兩次,但都不行。於是繼續往下看這個issue,看是否有別的解決方案。還真有!harprit講,看起來是functools_lru_cache
相關的文件丟失造成的。他說python matplotlib在ubuntu上也有這個問題。按照他的建議使用下面的命令安裝python-matplotlib
。
$ sudo apt-get install python-matplotlib
安裝完後,再嘗試使用cookiecutter來創建新項目工程,問題消失了,工程創建成功。非常感謝google。
在前面的issue[^1]討論中,有人是用Fedora
系統,他的解決方案是執行下面的命令:
dnf install python2-backports-functools_lru_cache
結束語
寫此博文存粹是幫助自己把這個問題的解決方法記錄下來,以備後續再遇到,減少自己重複的工作。也給將來遇到這個問題的小夥伴提供一個方案,沒準能幫助到你們,那將是我今天寫這篇博客的榮幸。
參考
- [^1]https://github.com/awslabs/aws-sam-cli/issues/474
- https://askubuntu.com/questions/1046554/python-matplotlib-importerror-no-module-named-functools-lru-cache?answertab=oldest#tab-top
- https://stackoverflow.com/questions/47179433/python-2-7-functools-lru-cache-does-not-import-although-installed/47317710#47317710