1. 程式人生 > >Ubuntu16.04 安裝 python3.7 以及jupyter

Ubuntu16.04 安裝 python3.7 以及jupyter

安裝python3.7

1.安裝依賴包

sudo apt-get update

sudo apt-get install build-essential python-dev python-setuptools python-pip python-smbus

sudo apt-get install build-essential libncursesw5-dev libgdbm-dev libc6-dev

sudo apt-get install zlib1g-dev libsqlite3-dev tk-dev

sudo apt-get install libssl-dev openssl

sudo apt-get install libffi-dev

 2. 安裝pyenv

 git clone git://github.com/yyuu/pyenv.git ~/.pyenv
 echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
 echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
 echo 'eval "$(pyenv init -)"' >> ~/.bashrc
 exec $SHELL -l

3. 安裝 python3.7.0

pyenv install 3.7.0 -v
安裝完成之後,需要使用如下命令對資料庫進行更新:
pyenv rehash
檢視已經安裝的python版本:
pyenv versions
* system (set by /root/.pyenv/version)
3.7.0

4.設定全域性python版本

pyenv global 3.7.0 

之後輸入python 預設就是 python3.7

python
Python 3.7.0 (default, Sep 30 2018, 11:18:04) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

安裝jupyter

pip3.7 install --upgrade pip //更新pip
pip3.7 install jupyter   //安裝jupyter

此時輸入

jupyter-notebook

會返回   jupyter: command not found

遇到這個狀況退出重新登入就好。

仍有問題可以嘗試再次安裝

sudo apt install jupyter-notebook

這樣本地就能執行 jupyter 了但是如果要是在伺服器端設定的jupyter。想要通過 IP 訪問,需要進行如下操作

配置伺服器端jupyter

1. 生成一個 notebook 配置檔案

預設情況下,配置檔案 ~/.jupyter/jupyter_notebook_config.py 並不存在,需要自行建立。使用下列命令生成配置檔案:

jupyter notebook --generate-config
  • 如果是 root 使用者執行上面的命令,會發生一個問題:
Running as root it not recommended. Use --allow-root to bypass.
  • 提示資訊很明顯,root 使用者執行時需要加上 --allow-root 選項。
jupyter notebook --generate-config --allow-config
  • 執行成功後,會出現下面的資訊:
Writing default config to: /root/.jupyter/jupyter_notebook_config.py

2. 生成密碼

自動生成

從 jupyter notebook 5.0 版本開始,提供了一個命令來設定密碼:jupyter notebook password,生成的密碼儲存在 jupyter_notebook_config.json

$ jupyter notebook password
Enter password:  ****
Verify password: ****
[NotebookPasswordApp] Wrote hashed password to /Users/you/.jupyter/jupyter_notebook_config.json

手動生成

除了使用提供的命令,也可以通過手動安裝,我是使用的手動安裝,因為jupyter notebook password 出來一堆內容,沒耐心看。開啟 ipython 執行下面內容:

In [1]: from notebook.auth import passwd
In [2]: passwd()
Enter password:
Verify password:
Out[2]: 'sha1:67c9e60bb8b6:9ffede0825894254b2e042ea597d771089e11aed'

sha1:67c9e60bb8b6:9ffede0825894254b2e042ea597d771089e11aed

這一串就是要在 jupyter_notebook_config.py 新增的密碼。

c.NotebookApp.password = 'sha1:67c9e60bb8b6:9ffede0825894254b2e042ea597d771089e11aed'

修改配置檔案

jupyter_notebook_config.py 中找到下面的行,取消註釋並修改。

vim .jupyter/jupyter_notebook_config.py
c.NotebookApp.allow_remote_access = True # 允許遠端訪問
c.NotebookApp.ip='*'
c.NotebookApp.password = 'sha:ce...剛才複製的那個密文'
c.NotebookApp.open_browser = False
c.NotebookApp.port =8888 #可自行指定一個埠, 訪問時使用該埠
c.NotebookApp.terminals_enabled = True #允許開啟終端
  • 以上設定完以後就可以在伺服器上啟動 jupyter notebook, root 使用者使用 jupyter notebook --allow-root
  • 開啟 IP:指定的埠, 輸入密碼就可以訪問了。