背景:本來打算用scrapy 創一個爬蟲專案,但是無論如何都顯示zsh: command not found: scrapy,看了很多篇blog才解決了問題,決定記錄一下。

主要參考的blog:

  1. https://www.jianshu.com/p/51196153f804

  2. https://stackoverflow.com/questions/17607944/scrapy-not-installed-correctly-on-mac

問題分析:

我在重新安裝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

  1. 開啟finder,同時按下command+shift+g,輸入.zshrc,開啟.zshrc檔案,然後在檔案末尾寫上source ~/.bash_profile

  2. 按下command+s 儲存
  3. 開啟終端,輸入source ~/.zshrc,執行檔案。

第二步:在.bash_profile檔案中新增環境變數

  1. 開啟finder,同時按下command+shift+g,輸入.bash_profile,開啟.bash_profile檔案。
  2. 在最後一行寫入:

    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"
  3. 按下command+s 儲存。
  4. 開啟終端,輸入source ~/.bash_profile,執行檔案。
  5. 最後可以在終端輸入 echo $PATH看一下有沒有將環境變數新增進去。
  6. 可以看到已經將/Library/Frameworks/Python.framework/Versions/2.7/bin新增進去了(而且還添加了很多個 )。
  7. 最後輸入scapy終於可以使用了!!!