2023年2月21日 星期二

Mapleboard MP510-50 測試 (十三) : 安裝 Python 套件時出現 "not on PATH" 警告問題

在前一篇中試圖用 pip install 安裝 Thonny 時出現了 '/home/one/.local/bin' which is not on PATH 的警告訊息, 後來在安裝 Flask 與 Django 套件時也出現了相同問題, 所以我把此問題的解決辦法移到這篇來記錄. 本系列測試文章參考 :


以下是安裝 Flask 套件的紀錄 :

one@LX2438:~$ pip install flask    
Defaulting to user installation because normal site-packages is not writeable
Collecting flask
  Downloading Flask-2.2.3-py3-none-any.whl (101 kB) 101.8/101.8 KB 631.3 kB/s eta 0:00:00
Requirement already satisfied: click>=8.0 in /usr/lib/python3/dist-packages (from flask) (8.0.3)
Collecting itsdangerous>=2.0
  Downloading itsdangerous-2.1.2-py3-none-any.whl (15 kB)
Collecting Werkzeug>=2.2.2
  Downloading Werkzeug-2.2.3-py3-none-any.whl (233 kB) 233.6/233.6 KB 3.1 MB/s eta 0:00:00
Collecting Jinja2>=3.0
  Downloading Jinja2-3.1.2-py3-none-any.whl (133 kB) 133.1/133.1 KB 5.2 MB/s eta 0:00:00
Requirement already satisfied: MarkupSafe>=2.0 in /usr/lib/python3/dist-packages (from Jinja2>=3.0->flask) (2.0.1)
Collecting MarkupSafe>=2.0
  Downloading MarkupSafe-2.1.2-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (26 kB)
Installing collected packages: MarkupSafe, itsdangerous, Werkzeug, Jinja2, flask
  WARNING: The script flask is installed in '/home/one/.local/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.
Successfully installed Jinja2-3.1.2 MarkupSafe-2.1.2 Werkzeug-2.2.3 flask-2.2.3 itsdangerous-2.1.2

雖然出現了  '/home/one/.local/bin' which is not on PATH 警告, 但 Flask 其實是有安裝成功的 :

one@LX2438:~$ python3  
Python 3.10.6 (main, Nov 14 2022, 16:10:14) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import flask     
>>> flask.__version__    
'2.2.3'

下面用 Thonny 來寫一個 Hello World 來測試 Flask 網頁伺服器, 程式如下 :

#hello.py
from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello():
    return 'Hello World!'

if __name__ == "__main__":
    app.run(debug=True)

存檔後執行, 然後開啟瀏覽器連線 127.0.0.1:5000, 結果 OK :




安裝 Django 也是一樣出現 The script XXXX is installed in '/home/one/.local/bin' which is not on PATH 的警告訊息 : 

one@LX2438:~$ pip install django   
Defaulting to user installation because normal site-packages is not writeable
Collecting django
  Downloading Django-4.1.7-py3-none-any.whl (8.1 MB)
 8.1/8.1 MB 6.1 MB/s eta 0:00:00
Collecting sqlparse>=0.2.2
  Downloading sqlparse-0.4.3-py3-none-any.whl (42 kB)
 42.8/42.8 KB 2.3 MB/s eta 0:00:00
Collecting asgiref<4,>=3.5.2
  Downloading asgiref-3.6.0-py3-none-any.whl (23 kB)
Installing collected packages: sqlparse, asgiref, django
  WARNING: The script sqlformat is installed in '/home/one/.local/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.
  WARNING: The script django-admin is installed in '/home/one/.local/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.
Successfully installed asgiref-3.6.0 django-4.1.7 sqlparse-0.4.3

爬文在下面這篇文章找到解決辦法 : 


原來只要用下列兩個指令將目前的 bin 路徑加入系統變數 $PATH 中就可以了 :

one@LX2438:~$ echo 'export PATH=/home/one/.local/bin:$PATH' >> ~/.bashrc   
one@LX2438:~$ source ~/.bashrc   

用 echo 指令輸出 $PATH 變數就可以看到目前使用者 one 的執行路徑已被加入 $PATH 中 :

one@LX2438:~$ echo $PATH   
/home/one/.local/bin:/home/one/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

可見 $PATH 中的每個路徑都是以冒號隔開的. 這個 .bashrc 是使用者目錄 (此處為 /home/one) 下的一個隱藏檔, 每次開啟 shell 終端機時就會執行, 此檔案紀錄使用者的環境變數 PATH, 用來指示 shell 要到那些目錄下尋找所執行的命令或程式. 注意, .bashrc 是套用在個別使用者的, 修改其內容只會影響到該使用者. 

注意, 以上的設定后必須在新開啟的終端機中才會有效, 所以我新開一個終端機來安裝另一個套件 virtualenv :

one@LX2438:~$ pip install virtualenv
Defaulting to user installation because normal site-packages is not writeable
Collecting virtualenv
  Downloading virtualenv-20.19.0-py3-none-any.whl (8.7 MB)
 8.7/8.7 MB 5.9 MB/s eta 0:00:00
Requirement already satisfied: platformdirs<4,>=2.4 in ./.local/lib/python3.10/site-packages (from virtualenv) (3.0.0)
Collecting distlib<1,>=0.3.6
  Downloading distlib-0.3.6-py2.py3-none-any.whl (468 kB)
 468.5/468.5 KB 6.3 MB/s eta 0:00:00
Collecting filelock<4,>=3.4.1
  Downloading filelock-3.9.0-py3-none-any.whl (9.7 kB)
Installing collected packages: distlib, filelock, virtualenv
Successfully installed distlib-0.3.6 filelock-3.9.0 virtualenv-20.19.0

果然這個 Warning 訊息就不見了. 

參考 : 

沒有留言 :