pyenv、ipython、jupyter的安裝使用

scm1911發表於2019-03-25

安裝 pyenv

參考文件:https://github.com/pyenv/pyenv-installer

線上安裝方法

yum install git -y
# pyenv 在安裝python的時候是就地編譯的,需要下面的依賴,需要提前安裝一下。
yum install gcc make patch gdbm-devel openssl-devel sqlite-devel readline-devel zlib-devel bzip2-devel -y
id python
useradd python
id python
echo "python" | passwd python --stdin
su - python
curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash
echo 'export PATH="/home/python/.pyenv/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(pyenv init -)"'  >> ~/.bash_profile
echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bash_profile
tail -3  ~/.bash_profile
source ~/.bash_profile
which pyenv

# 說明:
eval "$(pyenv init -)"  # 初始化pyenv 
eval "$(pyenv virtualenv-init -)"   # 初始化虛擬化外掛

安裝過程中設定環境變數的提示

[python@linux-node1 ~]$ curl -L https://github.com/pyenv/pyenv-installer/raw/master/
  % Total    % Received % Xferd  Average Speed  Time    Time    Time  Current
                                Dload  Upload  Total  Spent    Left  Speed
100  148  100  148    0    0    116      0  0:00:01  0:00:01 --:--:--  116
100  2194  100  2194    0    0  1313      0  0:00:01  0:00:01 --:--:--  1313
Cloning into '/home/python/.pyenv'...
......................
​
WARNING: seems you still have not added 'pyenv' to the load path.
​
# Load pyenv automatically by adding
# the following to ~/.bash_profile:
​
export PATH="/home/python/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

離線安裝方法

主要的操作就是在github上clone專案的目錄

su - python
git clone https://github.com/pyenv/pyenv.git ~/.pyenv
git clone https://github.com/pyenv/pyenv-virtualenv.git ~/.pyenv/plugins/pyenv-virtualenv
git clone https://github.com/pyenv/pyenv-update.git ~/.pyenv/plugins/pyenv-update
git clone https://github.com/pyenv/pyenv-which-ext.git ~/.pyenv/plugins/pyenv-which-ext

配置環境變數(注意家目錄是哪一個)

vim ~/.bash_profile
export PATH="/home/python/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

source  ~/.bash_profile

pyenv 命令幫助

[root@linux-node1 ~]# su - python
Last login: Tue Sep 11 22:31:33 CST 2018 on pts/0
[python@linux-node1 ~]$ 
[python@linux-node1 ~]$ pyenv
pyenv 1.2.7
Usage: pyenv <command> [<args>]
​
Some useful pyenv commands are:
   commands    List all available pyenv commands
   local       Set or show the local application-specific Python version
   global      Set or show the global Python version
   shell       Set or show the shell-specific Python version
   install     Install a Python version using python-build
   uninstall   Uninstall a specific Python version
   rehash      Rehash pyenv shims (run this after installing executables)
   version     Show the current Python version and its origin
   versions    List all Python versions available to pyenv
   which       Display the full path to an executable
   whence      List all Python versions that contain the given executable
​
See `pyenv help <command>' for information on a specific command.
For full documentation, see: https://github.com/pyenv/pyenv#readme
[python@linux-node1 ~]$ 

通過pyenv安裝多版本的python

預設pyenv使用的是系統預設的python版本

[python@centos7-scm ~]$python -V
Python 2.7.5

我們可以通過下面的方法來安裝多版本的python環境

isntall

檢視幫助

pyenv help install

列出所有可用的版本

pyenv install --list

方法1:線上安裝指定的版本

pyenv install 3.5.3 -v
pyenv versions   # 檢視當前所處的pyhton環境

方法2:使用快取方式更快地安裝(離線安裝)

mkdir ~/.pyenv/cache
cd ~/.pyenv/cache
rz 將下面的包拷貝到上面的路徑中
Python-3.5.3.tar.xz  Python-3.5.3.tgz   # 由於在安裝的時候,預設選擇的包不固定,所以這裡我們直接將所有的包都拷貝到 ~/.pyenv/cache

執行安裝命令
pyenv install 3.5.3 -v
pyenv versions    # 檢視當前所處的pyhton環境
[python@centos7-scm ~]$pyenv versions
* system (set by /home/python/.pyenv/version)  # 前面有星號 '*' 的是當前使用的版本
  3.5.3   # 這個是剛剛安裝的版本

pyenv的python版本管理控制

提示:
因為pyenv安裝的時候只是安裝在使用者的家目錄中,所以pyenv的版本控制(global、local、shell)都是針對同一個使用者的

pyenv global(全域性設定)

pyenv global 3.5.3  # 設定全域性的python版本(慎用,會影響所有受pyenv控制的python環境)
pyenv version  # 檢視當前的pyhton環境
pyenv versions  # 檢視所有安裝的pyhton版本
python -V  # 檢視當前的pyhton版本
[python@linux-node1 ~]$ pyenv local 3.5.3
[python@linux-node1 ~]$ pyenv version
3.5.3 (set by /home/python/.python-version)
[python@linux-node1 ~]$ pyenv versions
  system
* 3.5.3 (set by /home/python/.python-version)
[python@linux-node1 ~]$ python -V
Python 3.5.3
[python@linux-node1 ~]$ 

pyenv local(本地設定--基於目錄的設定,並且是子目錄繼承父目錄的設定,最實用)

工作中要使用pyenv設定本地的python環境

pyenv local 3.5.3  # 設定本地的python版本
pyenv version  # 檢視當前的pyhton環境
pyenv versions  # 檢視所有安裝的pyhton版本
python -V  # 檢視當前的pyhton版本

使用 local 設定後,python的版本是和目錄繫結的,並且子目錄繼承父目錄的python版本。

[python@linux-node1 ~]$ pwd
/home/python
[python@linux-node1 ~]$ 
[python@linux-node1 ~]$ tree
.
├── scm
│?? └── test
└── test
​
3 directories, 0 files
[python@linux-node1 ~]$ 
[python@linux-node1 ~]$ python -V
Python 2.7.5
[python@linux-node1 ~]$ cd scm/
[python@linux-node1 scm]$ python -V
Python 3.5.3
[python@linux-node1 scm]$ cd test/
[python@linux-node1 test]$ python -V
Python 3.5.3
[python@linux-node1 test]$ cd ~/test/
[python@linux-node1 test]$ python -V
Python 2.7.5
[python@linux-node1 test]$ pwd
/home/python/test
[python@linux-node1 test]$ pyenv local 3.6.3
[python@linux-node1 test]$ python -V
Python 3.6.3
[python@linux-node1 test]$ 

pyenv shell(基於會話的設定)

pyenv shell 3.5.3  # 設定當前會話的python版本(只會影響pyenv控制的當前會話的python環境,影響面太小,會話斷開就會回到system的環境,不常使用)
pyenv version  # 檢視當前的pyhton環境
pyenv versions  # 檢視所有安裝的pyhton版本
python -V  # 檢視當前的pyhton版本

Virtuaenv 虛擬環境

為什麼要使用虛擬環境?
因為剛才使用的python環境都是一個公共的空間,如果有多個專案使用不同的py“”thon版本開發,或者使用不同的python版本部署執行,或者使用同樣的版本開發的但不同專案使用了不同版本的庫,等等這些問題都會帶來衝突,最好的解決辦法就是每一個專案獨立執行自己的"獨立小環境"中。

虛擬環境的安裝使用

1 建立一個虛擬環境

pyenv virtualenv 3.5.3 scm353

2 檢視python虛擬環境,會有一個 scm353

[python@centos7-scm ~]$pyenv versions
* system (set by /home/python/.pyenv/version)
  3.5.3
  3.5.3/envs/scm353
  scm353

實際上是複製了一個單獨的版本,並建立了一個軟連線檔案指向一該版本

[python@centos7-scm ~]$ll /home/python/.pyenv/versions
total 0
drwxr-xr-x 7 python python 68 Dec 25 21:58 3.5.3
lrwxrwxrwx 1 python python 46 Dec 25 22:12 scm353 -> /home/python/.pyenv/versions/3.5.3/envs/scm353
[python@centos7-scm ~]$ll /home/python/.pyenv/versions/3.5.3/envs/scm353
total 4
drwxrwxr-x 2 python python 186 Dec 25 22:27 bin
drwxrwxr-x 2 python python   6 Dec 25 22:27 include
drwxrwxr-x 3 python python  23 Dec 25 22:27 lib
lrwxrwxrwx 1 python python   3 Dec 25 22:27 lib64 -> lib
-rw-rw-r-- 1 python python  99 Dec 25 22:27 pyvenv.cfg
[python@centos7-scm ~]$

3 切換到指定的專案目錄下,執行命令 pyenv local mag353 設定python虛擬環境版本

[python@linux-node1 test]$ python -V
Python 3.6.3
[python@linux-node1 test]$ pyenv local scm353
(scm353) [python@linux-node1 test]$ 
(scm353) [python@linux-node1 test]$ python -V
Python 3.5.3
(scm353) [python@linux-node1 test]$
(scm353) [python@linux-node1 test]$ cd 
[python@linux-node1 ~]$ 
[python@linux-node1 ~]$ cd -
/home/python/test
(scm353) [python@linux-node1 test]$ 

4 刪除指定的虛擬環境

pyenv uninstall scm353

5 匯出包
虛擬環境的好處就在於和其他專案運環境隔離,每一個獨立的環境都可以使用pip命令匯出已經安裝好的包,在另一個環境中安裝這些包。

pip freeze > requirement

6 在新的虛擬環境中安裝上面的包
可以在不同版本的環境中安裝

pip install -r requirement

關於虛擬環境的說明

實際上虛擬環境就是在原來的環境中獨立出一個小的環境,以後使用該環境的專案中使用的所有包組都是安裝在這個小的環境中的site-packages目錄中,這樣就實現了環境的隔離。

1 沒有虛擬環境的時候,所有的包組都是安裝在下面的目錄中

/home/python/.pyenv/versions/3.5.3/lib/python3.5/site-packages/

2 安裝有虛擬環境,使用某個虛擬環境的專案的所有的包組都是安裝在下面的目錄中

/home/python/.pyenv/versions/3.5.3/envs/scm353/lib/python3.5/site-packages/

ipython 增強的互動式Python命令列工具

提示:

ipython的安裝需要切換到安裝了pip命令的虛擬環境的目錄中去,系統預設的python環境沒有安裝pip命令

[python@centos7-scm ~]$pip install ipython
pyenv: pip: command not found

The `pip' command exists in these Python versions:
  3.5.3
  3.5.3/envs/scm353
  scm353

[python@centos7-scm ~]$pyenv version
system (set by /home/python/.pyenv/version)
[python@centos7-scm ~]$

配置pip的國內映象

安裝Ipython使用到的是pip包管理器,由於網路原因需要配置一下國內的映象

su - python
mkdir ~/.pip
cat > ~/.pip/pip.conf << EOF
[global]
index-url=https://mirrors.aliyun.com/pypi/simple/
trusted-host=mirrors.aliyun.com
EOF

驗證是否可以使用配置的映象安裝的方法:在不同的虛擬環境中,安裝redis包,使用pip list看看效果。

pip -V
pip install pkgname  # 是以後經常要使用的安裝python包的命令

windows系統 windows下pip的配置檔案在~/pip/pip.ini,內容同上

安裝ipython

cd test  #
pip install ipython  # 安裝ipython
ipython   # 啟動Ipython
pip install --upgrade pip   # 最後可能會有更新的提示,建議操作一下,否則後面會經常有提示

出現下面的問題需要更新一下 pip

pip install --upgrade pip

演示

[python@centos7-scm ~]$cd test
(scm353) [python@centos7-scm test]$pip install ipython
Collecting ipython
  Downloading https://mirrors.aliyun.com/pypi/packages/f0/b4/a9ea018c73a84ee6280b2e94a1a6af8d63e45903eac2da0640fa63bca4db/ipython-7.2.0-py3-none-any.whl (765kB)
    100% |████████████████████████████████| 768kB 183kB/s 
......
You are using pip version 9.0.1, however version 18.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
(scm353) [python@centos7-scm test]$
(scm353) [python@centos7-scm test]$ipython
Python 3.5.3 (default, Dec 25 2018, 10:48:30) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.2.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]:

In [1]:

jupyter 的安裝使用

jupyter 是基於WEB 的互動式筆記本,其中可以非常方便地使用python
安裝Jupyter的時候會自動安裝ipython的

安裝Jupyter

su - python
pip install jupyter

常用命令

su - python
jupyter notebook --help
jupyter notebook password
輸入密碼
jupyter notebook password 
jupyter notebook   # 預設是監聽在 127.0.0.1 上的8888埠
netstat -lntup 

jupyter notebook --ip=0.0.0.0 --port=8888  # 指定監聽所有地址(0.0.0.0)

啟動jupyter (注意關閉防火牆)

(mag353) [python@linux-node1 scm]$ jupyter notebook --ip=0.0.0.0 --port=8888
[I 07:08:18.080 NotebookApp] Serving notebooks from local directory: /home/python/scm
[I 07:08:18.080 NotebookApp] The Jupyter Notebook is running at:
[I 07:08:18.080 NotebookApp] http://(linux-node1.example.com or 127.0.0.1):8888/
[I 07:08:18.080 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[W 07:08:18.081 NotebookApp] No web browser found: could not locate runnable browser.

[I 07:08:32.264 NotebookApp] 302 GET / (192.168.56.2) 1.87ms
[I 07:08:46.938 NotebookApp] Creating new notebook in 
[I 07:08:46.965 NotebookApp] Writing notebook-signing key to /home/python/.local/share/jupyter/notebook_secret
[I 07:08:48.735 NotebookApp] Kernel started: 95013ea0-ff49-473e-9001-34b0847e7859
[I 07:08:49.998 NotebookApp] Adapting to protocol v5.1 for kernel 95013ea0-ff49-473e-9001-34b0847e7859
[I 07:10:48.600 NotebookApp] Saving file at /Untitled.ipynb
[I 07:11:06.071 NotebookApp] Saving file at /Untitled.ipynb
[I 07:11:09.719 NotebookApp] Saving file at /Untitled.ipynb
[I 07:12:22.409 NotebookApp] Saving file at /Untitled.ipynb
[I 07:12:47.911 NotebookApp] Saving file at /Untitled.ipynb
[I 07:13:26.105 NotebookApp] Saving file at /Untitled.ipynb
[I 07:14:28.230 NotebookApp] Saving file at /Untitled.ipynb
[I 07:14:37.027 NotebookApp] Starting buffering for 95013ea0-ff49-473e-9001-34b0847e7859:7820a0037d2c467081d28217dbb54482
[I 07:14:40.873 NotebookApp] Adapting to protocol v5.1 for kernel 95013ea0-ff49-473e-9001-34b0847e7859
[I 07:15:07.229 NotebookApp] Adapting to protocol v5.1 for kernel 95013ea0-ff49-473e-9001-34b0847e7859
[I 07:17:07.181 NotebookApp] Saving file at /Untitled.ipynb
[I 07:17:29.435 NotebookApp] Saving file at /Untitled.ipynb

瀏覽器訪問

http://192.168.27.100:8888

輸入上面設定的密碼


圖片名稱

介面展示

圖片名稱

本文連結:https://www.cnblogs.com/shichangming/p/10171575.html

相關文章