Linux下python多版本多環境介紹


 一、python多版本配置說明

安裝python相關依賴

[root@centos6 ~]# yum install -y gcc make patch gdbm-devel openssl-devel sqlite-devel readline-devel zlib-devel bzip2-devel

安裝git工具

[root@centos6 ~]# yum install -y git

創建python用戶

[root@centos6 ~]# useradd python

切換到python用戶

[root@centos6 ~]# su - python

從GitHub上拉取pyenv腳本並執行

[python@centos6 ~]$ curl -L https://raw.githubusercontent.com/pyenv/pyenv-installer/master/bin/pyenv-installer|bash

通過加入以下內容到python用戶的   ~/.bash_profile  文件中最后,使pyenv自動加載

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

使加入的內容生效

[python@centos6 ~]$ source ~/.bash_profile

命令行執行可以獲取pyenv相關的命令說明

[python@centos6 ~]$ pyenv pyenv 1.1.5 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版本,然后切換

查看當前python版本

[python@centos6 ~]$ python -V Python 2.6.6

列出所有可用版本

[python@centos6 ~]$ pyenv install -l

安裝指定版本

[python@centos6 ~]$ pyenv install 3.6.3 -v

注:

當這種在線方式安裝太慢時,可以選擇使用緩存安裝,也就是把需要安裝的對應源碼包下好,放入/home/python/.pyenv/cache  目錄下,cache目錄不存在時創建即可

[python@centos6 ~]$ mkdir -p /home/python/.pyenv/cache [python@centos6 ~]$ ll /home/python/.pyenv/cache/ total 38724
-rw-r--r-- 1 python python 16974296 Nov 17 10:19 Python-3.6.3.tar.xz -rw-r--r-- 1 python python 22673115 Nov 17 10:19 Python-3.6.3.tgz [python@centos6 ~]$ pyenv install 3.6.3 -v

安裝完成后,查看python版本(有 * 代碼當前生效的python版本,system就是系統默認版本,這里是python2.6.6)

[python@centos6 ~]$ pyenv versions * system (set by /home/python/.pyenv/version) 3.6.3

切換設置python版本,一般建議用python local設置

pyenv global 3.6.3
注意:root用戶下,盡量不適用global,會改變全局的python版本

pyenv shell 3.6.3
只作用於當前會話

python local 3.6.3
設置從當前工作目錄開始向下遞歸繼承該設置

這里演示python local的功能

[python@centos6 ~]$ mkdir test    #在家目錄新建一個test目錄
[python@centos6 ~]$ ll total 4 drwxrwxr-x 2 python python 4096 Nov 17 18:28 test [python@centos6 ~]$ cd test/        #切換到test目錄
[python@centos6 test]$ pyenv local 3.6.3 [python@centos6 test]$ pyenv versions system * 3.6.3 (set by /home/python/test/.python-version) [python@centos6 test]$ python -V    #這里應該是有bug,重新登錄或就正常了
Python 2.6.6 [python@centos6 test]$ python -V Python 3.6.3

#進入test的子目錄,也繼承了python版本設置
[python@centos6 test]$ mkdir child [python@centos6 test]$ cd child/ [python@centos6 child]$ pyenv versions system * 3.6.3 (set by /home/python/test/.python-version) [python@centos6 child]$ python -V Python 3.6.3
#進入其他目錄,python版本然是系統默認版本 [python@centos6 ~]$ pyenv versions * system (set by /home/python/.pyenv/version) 3.6.3 [python@centos6 ~]$ python -V Python 2.6.6

 

二、python多環境配置說明

創建一個3.6.3版本的獨立空間環境

[python@centos6 ~]$ pyenv virtualenv 3.6.3 martin    #最后的空間名,自己隨意指定
Requirement already satisfied: setuptools in /home/python/.pyenv/versions/3.6.3/envs/martin/lib/python3.6/site-packages Requirement already satisfied: pip in /home/python/.pyenv/versions/3.6.3/envs/martin/lib/python3.6/site-packages [python@centos6 ~]$ pyenv versions    #查看所有安裝的python版本
* system (set by /home/python/.pyenv/version) 3.6.3
  3.6.3/envs/martin martin [python@centos6 ~]$ cd test/ [python@centos6 test]$ pyenv local martin #設置當前目錄的python版本
(martin) [python@centos6 test]$ pyenv versions    #查看切換后的默認版本情況,設置獨立空間環境后,命令提示符前面會多一個自己設置的獨立空間的名字
 system 3.6.3
  3.6.3/envs/martin * martin (set by /home/python/test/.python-version)

配置好獨立空間后,安裝的包存在獨立目錄下

(martin) [python@centos6 test]$ pip -V pip 9.0.1 from /home/python/.pyenv/versions/3.6.3/envs/martin/lib/python3.6/site-packages (python 3.6)    #獨立空間的包存在該目錄下

 

三、安裝pip工具

切換到其他目錄,當提示匹配命令不存在時,安裝pip

官方文檔:https://pip.pypa.io/en/stable/installing/

 

[python@centos6 ~]$ pip -V
pip 9.0.1 from /home/python/.pyenv/versions/3.6.3/lib/python3.6/site-packages (python 3.6)

[python@centos6 ~]$ pip -V
pip 9.0.1 from /usr/lib/python2.6/site-packages (python 2.6)

 

使用pip安裝包

pip 通用配置,更改包搜索源
[python@abc ~]$ cat /home/python/.pip/pip.conf
[global]
index-url=https://mirrors.aliyun.com/pypi/simple/
trusted-host=mirrors.aliyun.com

 

列出pip已安裝的包
pip list

導出已安裝的包到requirements格式文件里
pip freeze > requirement

按照所給的requirements格式文件中的包名按照包
pip install -r requirement


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM