1.安裝操作系統
系統版本CentOS release 6.5 (Final)mini安裝
- 關閉SELinux
- 關閉防火牆功能
- 關閉SSH的UseDNS功能
- 配置IP地址
- 配置DNS服務器
- 配置NTP服務器
- 配置主機名
2. Pyenv安裝方式
2.1 安裝Git軟件
- yum install git -y
2.2 安裝Python依賴包
- yum -y install gcc make patch gdbm-devel openssl-devel sqlite-devel zlib-devel bzip2-devel
2.3 創建普通用戶
- useradd python
- passwd python
2.4 使用python這個普通用戶安裝pyenv軟件
2.4.1 添加yum更新源
- cd /etc/yum.repos.d/
- vim CentOS-Base.repo
- #添加更新源
- [updates]
- name=CentOS-Updates
- baseurl=https://mirrors.aliyun.com/centos/6.9/os/x86_64
- gpgcheck=0
- yum repolist
- yum update nss
- 輸入“y”
- su - python
- curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash
- vi ~/.bash_profile
- #文件末尾添加
- export PATH="/home/python/.pyenv/bin:$PATH"
- eval "$(pyenv init -)"
- eval "$(pyenv virtualenv-init -)"
- source ~/.bash_profile
安裝過程中注意的問題:
執行過程中報(35)SSL連接錯誤
解決方法:
(1)在https://github.com/pyenv/pyenv-installer 有安裝文檔
(2)如果curl出現curl: (35) SSL connect error,這是由於nss版本低的問題,需要更新它。
(3)通過root用戶添加更新源,也就是按上述步驟操作。
2.4.2 pyenv的使用
(1)查看Python版本及path路徑
- $python --version 查看Python版本信息
- $python -V 查看Python版本信息
- $echo $PATH 查看路徑變量
(2)pyenv命令介紹
(2)install命令
- $pyenv help install 查看命令幫助
- $ pyenv install --list 列出所有可用版本
(3)在線安裝執行版本
- $ pyenv install 3.6.6 使用pyenv安裝Python 3.6.6軟件包
- $ pyenv versions 查看當前環境Python運行版本
注:這樣安裝可能比較慢,為了提速,可以選用cashe方法。
(4)使用cache緩存方式安裝
在~/.pyenv目錄下,新建cache目錄,放入下載好的待安裝版本的所有壓縮文件。安裝時它是隨機選擇的,不確定選擇哪一個壓縮文件,把下載的2到3個不通類型壓縮格式的文件都放進去。Python3.6.6 只有兩個包,Python3.4版本可能有3個。
- $ pyenv install 3.6.6 -v 顯示詳細安裝信息
為了方便演示,請用客戶端再打開兩個會話窗口。
提前安裝備用
2.4.3 通過cache緩存方式安裝Python軟件
(1)上傳Python軟件包到cache目錄
- mkdir -p /home/python/.pyenv/cache
通過FTP工具(例如:Xshell、SecureCRT)上傳Python安裝包到/home/python/.pyenv/cache目錄下
(2)安裝Python軟件
- pyenv install 3.6.6
安裝過程需要等待幾分鍾。
(3)驗證Python是否安裝成功
這里需要注意的是
- $python -V 查看當前Python的運行版本
- $pyenv version 顯示當前的Python版本
- $pyenv versions 顯示所有可用的Python版本,和當前版本。
這里CentOS6.5默認的Python版本為Python2.6.6 也就是* system表示的。
而3.6.6則是剛剛通過pyenv安裝好的。
2.4.4 Pyenv的Python版本控制介紹
(1)version與versions的區別
- $pyenv version 顯示當前的Python版本
- $pyenv versions 顯示所有可用的Python版本,和當前版本。
(2)global全局設置
- $ pyenv global 3.6.6
可以看到所有受pyenv控制的窗口都是3.6.6的Python版本了。
這里用global是作用於非root用戶Python用戶上,如果是root用戶安裝,請不要使用global,否則影響太大。
比如:這里使用的Centos6.5就是Python2.6,使用了global就成了3.X,就會帶來很不好的影響。
- $pyenv global system
(3)shell會話設置
影響只作用於當前會話
- $ pyenv shell 3.6.6
(4)local 本地設置
使用pyenv local設置從當前工作目錄開始下上遞歸都繼承這個設置。
- $ pyenv local 3.6.6
注意:
A. 不建議在root用戶下安裝Python和pyenv。
B. local本地設置的問題在於同一個Python指定的3.6.6,如果不同項目包混在一起,包的多版本混在一起,相互干擾。
C. global影響面比較大,后期建議不用。
(5)Virtualenv 虛擬環境設置
問題:為什么要使用虛擬環境?
因為剛才使用的Python環境都是一個公共的空間,如果多個項目使用不同Python版本開發,或者使用不同的Python版本部署運行,或者使用同樣的版本開發的但不同項目使用了不同版本的庫,等等。這些問題都會帶來沖突。最好的解決方法就是每個項目獨立運行自己的“獨立小環境”中。
使用插件,在plugins/pyenv-virtualenv中
- $ pyenv virtualenv 3.6.6 ql366
使用Python3.6.6版本創建出一個獨立的虛擬空間。
注意問題:虛擬環境下Python的庫文件,與Python版本的隔離以及使用庫的隔離。可不要混淆了。
例如:
虛擬Python庫目錄
/home/python/.pyenv/versions/3.6.6/envs/py366/lib/python3.6
Python版本庫目錄
/home/python/.pyenv/versions/3.6.6/lib/python3.6
2.4.5 使用Virtualenv 虛擬環境設置
- mkdir /home/python/cmdb
- cd /home/python/cmdb
- pyenv virtualenv 3.6.6 py366
- pyenv local py366
2.4.6 驗證
使用pyenv virtualenv 3.6.6 py366命令后查看
使用pyenv local py366 命令后查看
3. pip、ipython和jupyter的安裝和配置
3.1 簡介
3.1.1 pip工具
pip是Python的包管理工具,3.X的版本直接帶了,可以直接使用。
Linux系統配置:
- $ mkdir ~/.pip
- $ touch ~/.pip/pip.conf
編輯配置文件加入下面參數:
- #添加參數,目的在於和yum一樣為了使用國內鏡像。
- [global]
- index-url=https://mirrors.aliyun.com/pypi/simple/
- trusted-host=mirrors.aliyun.com
在不同的虛擬環境中,安裝redis包,使用pip list命令看看效果
- $ pip -V 查看pip的版本信息,以及配置文件的路徑。
- $ pip install pkgname 以后經常要使用的安裝Python包的命令。
注意:Windows下 pip的配置文件在~/pip/pip.ini 文件中。
3.1.2 ipython工具
ipython是增強的交互式Python命令行工具。
- $pip install ipython 安裝ipython工具
- $ipython 確認ipython工具是否安裝好
3.1.3 jupyter工具
jupyter是基於WEB的交互式筆記本,其中可以非常方便的使用Python。
- $ pip install jupyter 安裝jupyter工具
- $ jupyter notebook help 查看jupyter notebook命令幫助
- $ jupyter notebook --ip=0.0.0.0 --no-browser 配置默認IP
- $ jupyter notebook --ip=192.168.103.43 --port=8080 配置jupyter服務器IP地址與端口
- $ ss -tanl 查看jupyter notebook進程
3.2 pip配置
- mkdir -p /home/python/.pip
- cd /home/python/.pip/
- touch pip.conf
- vi pip.conf
- #添加參數,目的在於和yum一樣為了使用國內鏡像。
- [global]
- index-url=https://mirrors.aliyun.com/pypi/simple/
- trusted-host=mirrors.aliyun.com
注:這里輸入pip命令發現命令找不到,是由於默認CentOS6.5的Python 2.6 是不支持pip命令的。但是在虛擬項目中發現Python3.6.6版本是支持的。
- cd /home/python/cmdb
- pip install --upgrade pip
- pip install ipython
- pip install jupyter
- jupyter notebook password
- jupyter notebook --ip=192.168.103.43 --port 8080
3.3 驗證
下圖為更新pip包的過程,因為默認CentOS6.5是安裝的pip 10.0.1版本的,接下來我們如果要安裝ipython與jupyter工具則需要pip更新到pip 18.1版本 才行。
下圖為安裝ipython工具的過程,這里我們可以使用echo $? 命令來查看程序是否安裝成功,一般“0”表示程序執行成功。
也可以如下圖,直接在虛擬Python環境下使用ipython的命令查看,是否能夠進入進行代碼的編寫與交互。
下圖為安裝jupyter的安裝過程
下圖為該Python虛擬環境下pip安裝的軟件包文件的存放地點。
下圖為jupyter notebook創建一個密碼
下圖為jupyter工具登錄后的web界面
3.4 導出包
虛擬環境的好處就在於和其他項目運行環境隔離。每一個獨立的環境就可以使用pip命令導出已經安裝的包,在另一個環境中安裝這些包。
- (py366) [python@localhost cmdb]$ pip freeze > requirement #將cmdb目錄下的Python虛擬環境的pip包冷凍壓縮生成一個名為requirement的文件(這個名字隨意)
- (py366) [python@localhost cmdb]$ mkdir ~/test/projects/pod1 #創建一個/home/python/test/projects/pod1的目錄
- (py366) [python@localhost cmdb]$ cd ~/test/projects/ pod1 #切換到這個目錄下
- [python@localhost pod1]$ pyenv install --list #查看pyenv的安裝數據包的列表
- [python@localhost pod1]$ pyenv install 3.5.2 #使用pyenv工具安裝Python 3.5.2
- [python@localhost pod1]$ pyenv virtualenv 3.5.2 py352 #制作Python的virtualenv虛擬環境
- [python@localhost pod1]$ pyenv local py352 #在這個/home/python/test/projects/pod1目錄本地應用py352這個虛擬環境
- (py352)[python@localhost pod1]$ mv ~/cmdb/repuirement ./ #將之前/home/python/cmdb/requirement文件移動到/home/python/test/projects/pod1這個當前目錄
- (py352)[python@localhost pod1]$ pip install -r requirement #使用pip命令安裝這個冷凍的requirement的Python 3.6.6環境下的安裝包。
4. 詳細安裝過程
4.1 安裝Git軟件包
[root@localhost ~]# yum install git -y
Loaded plugins: fastestmirror, security
base | 3.7 kB 00:00
http://mirrors.njupt.edu.cn/centos/6.10/os/x86_64/repodata/1aa8754bde2f3921d67cca4bb70d9f587fb858a24cc3d1f66d3315292a89fc20-primary.sqlite.bz2: [Errno 12] Timeout on http://10.10.254.10/cache/3/02/mirrors.njupt.edu.cn/cb1261e2fba3c41dba70da70f1451fcf/1aa8754bde2f3921d67cca4bb70d9f587fb858a24cc3d1f66d3315292a89fc20-primary.sqlite.bz2: (28, 'connect() timed out!')
Trying other mirror.
base/primary_db | 4.7 MB 00:00
省略。。。。。。
Updating : perl-Git-1.7.1-9.el6_9.noarch 1/4
Updating : git-1.7.1-9.el6_9.x86_64 2/4
Cleanup : perl-Git-1.7.1-3.el6_4.1.noarch 3/4
Cleanup : git-1.7.1-3.el6_4.1.x86_64 4/4
Verifying : git-1.7.1-9.el6_9.x86_64 1/4
Verifying : perl-Git-1.7.1-9.el6_9.noarch 2/4
Verifying : git-1.7.1-3.el6_4.1.x86_64 3/4
Verifying : perl-Git-1.7.1-3.el6_4.1.noarch 4/4
Updated:
git.x86_64 0:1.7.1-9.el6_9
Dependency Updated:
perl-Git.noarch 0:1.7.1-9.el6_9
Complete!
[root@localhost ~]#
4.2 安裝Python依賴包
[root@localhost ~]# yum -y install gcc make patch gdbm-devel openssl-devel sqlite-devel zlib-devel bzip2-devel
Loaded plugins: fastestmirror, security
Determining fastest mirrors
* base: mirrors.cn99.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package bzip2-devel.x86_64 0:1.0.5-7.el6_0 will be installed
---> Package gcc.x86_64 0:4.4.7-4.el6 will be updated
--> Processing Dependency: gcc = 4.4.7-4.el6 for package: gcc-gfortran-4.4.7-4.el6.x86_64
省略。。。。。。
Dependency Updated:
cpp.x86_64 0:4.4.7-23.el6 e2fsprogs.x86_64 0:1.41.12-24.el6
e2fsprogs-libs.x86_64 0:1.41.12-24.el6 gcc-c++.x86_64 0:4.4.7-23.el6
gcc-gfortran.x86_64 0:4.4.7-23.el6 gdbm.x86_64 0:1.8.0-39.el6
keyutils-libs.x86_64 0:1.4-5.el6 krb5-libs.x86_64 0:1.10.3-65.el6
libcom_err.x86_64 0:1.41.12-24.el6 libgcc.i686 0:4.4.7-23.el6
libgcc.x86_64 0:4.4.7-23.el6 libgfortran.x86_64 0:4.4.7-23.el6
libgomp.x86_64 0:4.4.7-23.el6 libselinux.x86_64 0:2.0.94-7.el6
libselinux-utils.x86_64 0:2.0.94-7.el6 libss.x86_64 0:1.41.12-24.el6
libstdc++.x86_64 0:4.4.7-23.el6 libstdc++-devel.x86_64 0:4.4.7-23.el6
openssl.x86_64 0:1.0.1e-57.el6 sqlite.x86_64 0:3.6.20-1.el6_7.2
Complete!
[root@localhost ~]#
4.3 新建Python用戶
[root@localhost ~]# useradd python
[root@localhost ~]# passwd python
Changing password for user python.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
4.4 添加yum更新源
[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# vim CentOS-Base.repo
[updates]
name=CentOS-Updates
baseurl=https://mirrors.aliyun.com/centos/6.9/os/x86_64
gpgcheck=0
[root@localhost yum.repos.d]# yum repolist
Loaded plugins: fastestmirror, security
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirror.bit.edu.cn
* updates: mirrors.aliyun.com
base | 3.7 kB 00:00
extras | 3.4 kB 00:00
updates | 3.4 kB 00:00
repo id repo name status
base CentOS-6 - Base 6,713
extras CentOS-6 - Extras 35
updates CentOS-Updates 251
repolist: 6,999
[root@localhost yum.repos.d]# yum update nss
Loaded plugins: fastestmirror, security
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirror.bit.edu.cn
* updates: mirrors.aliyun.com
省略。。。。。。
Dependency Updated:
nspr.x86_64 0:4.19.0-1.el6
nss-softokn.x86_64 0:3.14.3-23.3.el6_8
nss-softokn-freebl.i686 0:3.14.3-23.3.el6_8
nss-softokn-freebl.x86_64 0:3.14.3-23.3.el6_8
nss-sysinit.x86_64 0:3.36.0-9.el6_10
nss-tools.x86_64 0:3.36.0-9.el6_10
nss-util.x86_64 0:3.36.0-1.el6
Complete!
4.5 安裝pyenv
[root@localhost yum.repos.d]# su - python
[python@localhost ~]$ curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
104 2188 104 2188 0 0 1709 0 0:00:01 0:00:01 --:--:-- 265k
Initialized empty Git repository in /home/python/.pyenv/.git/
省略。。。。。。
remote: Enumerating objects: 20, done.
remote: Counting objects: 100% (20/20), done.
remote: Compressing objects: 100% (11/11), done.
remote: Total 20 (delta 3), reused 16 (delta 2), pack-reused 0
Unpacking objects: 100% (20/20), done.
WARNING: seems you still have not added 'pyenv' to the load path.
# Load pyenv automatically by adding
# the following to ~/.bashrc:
export PATH="/home/python/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
[python@localhost ~]$ vi ~/.bash_profile
export PATH="/home/python/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
[python@localhost ~]$ source ~/.bash_profile
4.6 查看Python版本與驗證pyenv命令
[python@localhost ~]$ python --version
Python 2.6.6
[python@localhost ~]$ python -V
Python 2.6.6
[python@localhost ~]$ echo $PATH
/home/python/.pyenv/plugins/pyenv-virtualenv/shims:/home/python/.pyenv/shims:/home/python/.pyenv/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/python/bin:/home/python/bin
[python@localhost ~]$ pyenv
pyenv 1.2.8
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
4.7 通過cache緩存方式安裝Python軟件
[python@localhost .pyenv]$ mkdir -p /home/python/.pyenv/cache
[python@localhost .pyenv]$ cd cache/
[python@localhost cache]$ pwd
/home/python/.pyenv/cache
[python@localhost cache]$ ls
Python-3.6.6.tar.xz Python-3.6.6.tgz
[python@localhost cache]$ pyenv install 3.6.6
Installing Python-3.6.6...
WARNING: The Python readline extension was not compiled. Missing the GNU readline lib?
Installed Python-3.6.6 to /home/python/.pyenv/versions/3.6.6
[python@localhost cache]$
驗證
[python@localhost cache]$ python -V
Python 2.6.6
[python@localhost cache]$ pyenv version
system (set by /home/python/.pyenv/version)
[python@localhost cache]$ pyenv versions
* system (set by /home/python/.pyenv/version)
3.6.6
4.8 使用virtualenv 安裝虛擬環境設置
[python@localhost ~]$ mkdir -p /home/python/cmdb
[python@localhost ~]$ cd /home/python/cmdb
[python@localhost cmdb]$ pyenv virtualenv 3.6.6 py366
Looking in links: /tmp/tmpw0yws0yu
Requirement already satisfied: setuptools in /home/python/.pyenv/versions/3.6.6/envs/py366/lib/python3.6/site-packages (39.0.1)
Requirement already satisfied: pip in /home/python/.pyenv/versions/3.6.6/envs/py366/lib/python3.6/site-packages (10.0.1)
[python@localhost cmdb]$
[python@localhost cmdb]$ pyenv local py366
驗證:
(py366) [python@localhost cmdb]$ pyenv versions
system
3.6.6
3.6.6/envs/py366
* py366 (set by /home/python/cmdb/.python-version)
(py366) [python@localhost cmdb]$ pyenv version
py366 (set by /home/python/cmdb/.python-version)
(py366) [python@localhost cmdb]$
4.9 安裝pip、ipython和jupyter的安裝和配置
[python@localhost ~]$ mkdir -p /home/python/.pip
[python@localhost ~]$ cd /home/python/.pip/
[python@localhost .pip]$ touch pip.conf
[python@localhost .pip]$ vi pip.conf
[global]
index-url=https://mirrors.aliyun.com/pypi/simple/
trusted-host=mirrors.aliyun.com
驗證pip命令
(py366) [python@localhost cmdb]$ pip
Usage:
pip <command> [options]
Commands:
install Install packages.
download Download packages.
uninstall Uninstall packages.
freeze Output installed packages in requirements format.
list List installed packages.
show Show information about installed packages.
check Verify installed packages have compatible dependencies.
config Manage local and global configuration.
search Search PyPI for packages.
wheel Build wheels from your requirements.
hash Compute hashes of package archives.
completion A helper command used for command completion.
help Show help for commands.
省略。。。。。。。
--no-color Suppress colored output
(py366) [python@localhost cmdb]$
(py366) [python@localhost cmdb]$cd /home/python/cmdb
(py366) [python@localhost cmdb]$pip install --upgrade pip
(py366) [python@localhost cmdb]$pip install ipython
(py366) [python@localhost cmdb]$pip install jupyter
(py366) [python@localhost cmdb]$jupyter notebook password
(py366) [python@localhost cmdb]$jupyter notebook --ip=192.168.103.43 --port 8080