在Linux使用Python以及安裝pip

apt指令安裝Lunux系統軟體
pip3指令

在Ubuntu Linux中測試以下指令:

pip
pip2
pip3

pip -V
pip2 -V
pip3 -V

Ubuntu18.04開始,預設不安裝pip軟體,而且只支援Python3
在Unintu Linux測試以下指令:

python -V
python2 -V
python3 -V

Python官網:

  1. www.python.org
  2. www.pypi.org
  3. www.pypa.io

注意,不要再使用apt install python3-pip指令安裝pip

在Linux安裝Linux的方法:

  1. https://packaging.python.org/tutorials/installing-packages/
  2. 複製 Securely Download get-pip.py 的網址
  3. 在Linux終端機,進到家目錄,輸入指令 wget https://bootstrap.pypa.io/get-pip.py
  4. 更新倉庫清單並安裝軟體
  5. 先安裝python3的開發工具箱:apt install python3-dev,因為屆時要對pip編譯,所以要先安裝Python3的開發工具箱
  6. python3執行 get-pip.pypython3 get-pip.py
  7. 視情況安裝相依性到件來測試

目標:不要再安裝到pip2的到套件

再次執行以下指令以驗證Python版本:

pip -V (Python3)
pip2 -V (無)
pip3 -V (Python3)

安裝Jupyter Lab
指令:pip3 install jupyterlab

同時會自動安裝以下套件:
Installing collected packages: tornado, jinja2, ipython-genutils, traitlets, jupyter-core, attrs, pyrsistent, jsonschema, nbformat, defusedxml, testpath, pygments, jupyterlab-pygments, webencodings, pyparsing, packaging, bleach, pyzmq, jupyter-client, async-generator, nest-asyncio, nbclient, mistune, pandocfilters, nbconvert, ptyprocess, terminado, pycparser, cffi, argon2-cffi, parso, jedi, wcwidth, prompt-toolkit, pickleshare, decorator, backcall, ipython, ipykernel, Send2Trash, prometheus-client, notebook, json5, jupyterlab-server, jupyterlab
Successfully installed Send2Trash-1.5.0 argon2-cffi-20.1.0 async-generator-1.10 attrs-20.2.0 backcall-0.2.0 bleach-3.2.1 cffi-1.14.3 decorator-4.4.2 defusedxml-0.6.0 ipykernel-5.3.4 ipython-7.19.0 ipython-genutils-0.2.0 jedi-0.17.2 jinja2-2.11.2 json5-0.9.5 jsonschema-3.2.0 jupyter-client-6.1.7 jupyter-core-4.6.3 jupyterlab-2.2.9 jupyterlab-pygments-0.1.2 jupyterlab-server-1.2.0 mistune-0.8.4 nbclient-0.5.1 nbconvert-6.0.7 nbformat-5.0.8 nest-asyncio-1.4.2 notebook-6.1.4 packaging-20.4 pandocfilters-1.4.3 parso-0.7.1 pickleshare-0.7.5 prometheus-client-0.8.0 prompt-toolkit-3.0.8 ptyprocess-0.6.0 pycparser-2.20 pygments-2.7.2 pyparsing-2.4.7 pyrsistent-0.17.3 pyzmq-19.0.2 terminado-0.9.1 testpath-0.4.4 tornado-6.1 traitlets-5.0.5 wcwidth-0.2.5 webencodings-0.5.1

用系統管理員安裝程式,用一般使用者跑程式
執行jupyterlab指令 exit,離開系統管理員模式,進入一般使用者模式。

執行指令cd,進入家目錄。

輸入指令:jupyter lab,啟動jupyter lab。

檢查 TCP/IP 的連線資訊:

  1. lsof -nPi
  2. lsof -nPi | grep ':8888'
  3. lsof -nPi | grep ':8888.*LISTEN'
  4. lsof -nPi | grep -i ':8888.*LISTEN'

讓Jupyter Notebook可以接受外部連線

首先先到Jupyter的文件,查看相關資訊:https://jupyter-notebook.readthedocs.io/en/latest/

jupyter notebook --generate-config

到Ubuntu家目錄,執行指令:jupyter notebook --generate-config

執行結果:

[email protected]:~$ jupyter notebook --generate-config
Writing default config to: /home/benjamin/.jupyter/jupyter_notebook_config.py

系統回應,已新增一個jupyter_notebook_config.py/home/benjamin/.jupyter/目錄中

編輯此文件:
nano /home/benjamin/.jupyter/jupyter_notebook_config.py

在nano編輯器顯示行號的快捷鍵:Alt + Shift + 3

整列複製:Alt + 6

貼上:Ctrl + u

修改後的設定檔:

## The IP address the notebook server will listen on.
#  Default: 'localhost'
# c.NotebookApp.ip = 'localhost'
c.NotebookApp.ip = '*'

若要查詢有無修改成功,可輸入指令:
grep 'App\.ip' /home/benjamin/.jupyter/jupyter_notebook_config.py

啟動jupyter lab,
指令:jupyter lab

回覆訊息:

[W 19:43:53.306 LabApp] WARNING: The notebook server is listening on all IP addresses and not using encryption. This is not recommended.
[I 19:43:53.310 LabApp] JupyterLab extension loaded from /usr/local/lib/python3.8/dist-packages/jupyterlab
[I 19:43:53.310 LabApp] JupyterLab application directory is /usr/local/share/jupyter/lab
[I 19:43:53.312 LabApp] Serving notebooks from local directory: /home/benjamin
[I 19:43:53.312 LabApp] Jupyter Notebook 6.1.4 is running at:
[I 19:43:53.312 LabApp] http://ubuntu:8888/?token=f0b3104212653e292c7e753a006c259543eb6fa598a8761f
[I 19:43:53.312 LabApp]  or http://127.0.0.1:8888/?token=f0b3104212653e292c7e753a006c259543eb6fa598a8761f
[I 19:43:53.312 LabApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 19:43:53.362 LabApp] 
    
    To access the notebook, open this file in a browser:
        file:///home/benjamin/.local/share/jupyter/runtime/nbserver-36194-open.html
    Or copy and paste one of these URLs:
        http://ubuntu:8888/?token=f0b3104212653e292c7e753a006c259543eb6fa598a8761f
     or http://127.0.0.1:8888/?token=f0b3104212653e292c7e753a006c259543eb6fa598a8761f

螢幕有顯示Token,這個例子中,Token為f0b3104212653e292c7e753a006c259543eb6fa598a8761f

從另一台電腦連線到jupyter lab:http://192.168.214.131:8888/

會出現安全登入畫面,只要輸入Token (f0b3104212653e292c7e753a006c259543eb6fa598a8761f) 即可正常進入。