背景:本來打算用scrapy 創一個爬蟲專案,但是無論如何都顯示zsh: command not found: scrapy
,看了很多篇blog才解決了問題,決定記錄一下。
主要參考的blog:
問題分析:
我在重新安裝scrapy時顯示:
WARNING: The script scrapy is installed in
'/Library/Frameworks/Python.framework/Versions/3.9/bin' which is not on PATH.
Consider adding this directory to PATH
or, if you prefer to suppress this warning, use --no-warn-script-location.
說明/Library/Frameworks/Python.framework/Versions/3.9/bin
並不在PATH中,需要我們新增這個路徑到環境變數裡(Consider adding this directory to PATH )。
解決辦法:
第一步:在.zshrc檔案的末尾加上source ~/.bash_profile
。
- 開啟finder,同時按下command+shift+g,輸入.zshrc,開啟.zshrc檔案,然後在檔案末尾寫上
source ~/.bash_profile
。
- 按下command+s 儲存
- 開啟終端,輸入
source ~/.zshrc
,執行檔案。
第二步:在.bash_profile檔案中新增環境變數
- 開啟finder,同時按下command+shift+g,輸入.bash_profile,開啟.bash_profile檔案。
- 在最後一行寫入:
export PATH="/Library/Frameworks/Python.framework/Versions/3.9/bin:$PATH"
注意️:Versions後的數字為python的版本號,要根據自己的python版本自行修改,如果版本為2.7則改為:
export PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:$PATH"
- 按下command+s 儲存。
- 開啟終端,輸入
source ~/.bash_profile
,執行檔案。 - 最後可以在終端輸入
echo $PATH
看一下有沒有將環境變數新增進去。 - 可以看到已經將
/Library/Frameworks/Python.framework/Versions/2.7/bin
新增進去了(而且還添加了很多個 )。 - 最後輸入scapy終於可以使用了!!!