開始想使用virtual實現不同的版本的py隔離,然后發現不太方便,然后發現了這貨。
pyenv安裝(ubuntu環境
➜ ~ git clone git://github.com/yyuu/pyenv.git ~/.pyenv Cloning into '/home/wang/.pyenv'... remote: Counting objects: 12600, done. remote: Compressing objects: 100% (15/15), done. remote: Total 12600 (delta 7), reused 0 (delta 0), pack-reused 12583 Receiving objects: 100% (12600/12600), 2.24 MiB | 47.00 KiB/s, done. Resolving deltas: 100% (8749/8749), done. Checking connectivity... done. ➜ ~ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc ➜ ~ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc ➜ ~ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc ➜ ~ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc ➜ ~ echo 'eval "$(pyenv init -)"' >> ~/.bashrc ➜ ~ echo 'eval "$(pyenv init -)"' >> ~/.zshrc ➜ ~ exec $SHELL -l
注:如果你沒有使用zsh,那么zshrc相關的都可以不用執行
驗證安裝是否成功:
➜ ~ pyenv commands help local shell --version version-name which completions hooks prefix shims version-file version-origin exec init rehash uninstall version-file-read versions global install root version version-file-write whence
輸入pyenv然后tab會出現各種補全的選項
pyenv的基本使用:
➜ ~ pyenv --version # pyenv版本 pyenv 20160422-3-gd0fae57 ➜ ~ pyenv versions # 當前py版本信息,如果裝了其他版本那么會顯示多個 * system (set by /home/wang/.pyenv/version) ➜ ~ ➜ ~ pyenv help 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/yyuu/pyenv#readme ➜ ~ pyenv init # 如果輸入pyenv之后使用tab不補全,可以使用這個命令初始化一下,然后就可以使用補全命令了 # Load pyenv automatically by appending # the following to ~/.zshrc: eval "$(pyenv init -)"
顯示可以安裝的列表
➜ ~ pyenv install -l # 可以安裝的版本,此處只列舉幾個(因為實在是太多了啊 Available versions: 2.1.3 2.2.3 2.3.7 2.4 2.4.1
pyenv安裝py
➜ ~ pyenv install 3.3.1 # install py3.3.1 Downloading Python-3.3.1.tgz... -> http://www.python.org/ftp/python/3.3.1/Python-3.3.1.tgz Installing Python-3.3.1... patching file ./Modules/readline.c Hunk #1 succeeded at 225 (offset -6 lines). Hunk #2 succeeded at 759 (offset -14 lines). Hunk #3 succeeded at 812 (offset -14 lines). Hunk #4 succeeded at 870 (offset -14 lines). Hunk #5 succeeded at 918 with fuzz 2 (offset -25 lines). patching file ./Lib/ssl.py Hunk #2 succeeded at 609 (offset -40 lines). patching file ./Modules/_ssl.c Hunk #1 succeeded at 1683 (offset -63 lines). Hunk #2 succeeded at 2751 (offset -93 lines). WARNING: The Python bz2 extension was not compiled. Missing the bzip2 lib? WARNING: The Python readline extension was not compiled. Missing the GNU readline lib? WARNING: The Python sqlite3 extension was not compiled. Missing the SQLite3 lib? Installing pip from https://bootstrap.pypa.io/get-pip.py... 2016-05-08 21:29:03 URL:https://bootstrap.pypa.io/get-pip.py [1522812/1522812] -> "-" [1] Installed Python-3.3.1 to /home/wang/.pyenv/versions/3.3.1 ➜ ~ pyenv rehash # 更新本地數據庫 ➜ ~ ➜ ~ pyenv versions # 查看本機版本信息 * system (set by /home/wang/.pyenv/version) 3.3.1
global, shell, local的使用
➜ ~ pyenv versions * system (set by /home/wang/.pyenv/version) 3.3.1 ➜ ~ ➜ ~ python --version Python 2.7.6 ➜ ~ ➜ ~ pyenv global 3.3.1 # 更改本機版本,重啟不會造成再次更改 ➜ ~ ➜ ~ python --version Python 3.3.1 ➜ ~ ➜ ~ pyenv local system # 更改本地的版本,只是臨時生效,重啟什么的會恢復系統版本 ➜ ~ ➜ ~ python --version Python 2.7.6 ➜ ~ ➜ ~ python shell 3.3.1 # 更改本shell的版本,臨時生效,優先級高於global,local的也高於global,都是臨時生效 python: can't open file 'shell': [Errno 2] No such file or directory ➜ ~ pyenv shell 3.3.1 ➜ ~ pyenv versions system * 3.3.1 (set by PYENV_VERSION environment variable) ➜ ~ ➜ ~ python Python 3.3.1 (default, May 8 2016, 21:27:17) [GCC 4.8.4] on linux Type "help", "copyright", "credits" or "license" for more information. >>>
global, loacl優先級比較
➜ ~ pyenv global 3.3.1 # 這個例子可以看出優先級 ➜ ~ pyenv --version pyenv 20160422-3-gd0fae57 ➜ ~ pyenv versions * system (set by /home/wang/.python-version) 3.3.1 ➜ ~ pyenv local 3.3.1 ➜ ~ pyenv versions system * 3.3.1 (set by /home/wang/.python-version) ➜ ~ pyenv shell system ➜ ~ pyenv versions * system (set by PYENV_VERSION environment variable) 3.3.1 ➜ ~ python global python: can't open file 'global': [Errno 2] No such file or directory ➜ ~ pyenv global system ➜ ~ pyenv versions * system (set by PYENV_VERSION environment variable) 3.3.1
pyenv和virtualenv的搭配使用
➜ ~ cd .pyenv/plugins # 安裝virtualenv插件 ➜ plugins git:(master) git clone https://github.com/yyuu/pyenv-virtualenv.git Cloning into 'pyenv-virtualenv'... remote: Counting objects: 1781, done. remote: Compressing objects: 100% (19/19), done. remote: Total 1781 (delta 8), reused 0 (delta 0), pack-reused 1762 Receiving objects: 100% (1781/1781), 518.17 KiB | 46.00 KiB/s, done. Resolving deltas: 100% (1208/1208), done. Checking connectivity... done. ➜ plugins git:(master) pyenv virtualenv 3.3.1 my_pro_3.3.1 # 創建3.3.1版本虛擬環境 Installing pip from https://bootstrap.pypa.io/get-pip.py... 2016-05-08 21:42:21 URL:https://bootstrap.pypa.io/get-pip.py [1522812/1522812] -> "-" [1] Collecting pip Using cached pip-8.1.1-py2.py3-none-any.whl Collecting setuptools Using cached setuptools-21.0.0-py2.py3-none-any.whl Collecting wheel Using cached wheel-0.29.0-py2.py3-none-any.whl Installing collected packages: pip, setuptools, wheel Successfully installed pip-8.1.1 setuptools-21.0.0 wheel-0.29.0 ➜ plugins git:(master) pyenv virtualenvs # 顯示環境,為什么有2個我有點不太懂 3.3.1/envs/my_pro_3.3.1 (created from /home/wang/.pyenv/versions/3.3.1) my_pro_3.3.1 (created from /home/wang/.pyenv/versions/3.3.1) ➜ plugins git:(master) pyenv versions * system (set by PYENV_VERSION environment variable) 3.3.1 3.3.1/envs/my_pro_3.3.1 my_pro_3.3.1 ➜ plugins git:(master) pyenv activate 3.3.1/envs/my_pro_3.3.1 # 激活使用,有錯誤,可以重啟shell Failed to activate virtualenv. Perhaps pyenv-virtualenv has not been loaded into your shell properly. Please restart current shell and try again.
vitualenv的使用
➜ ~ pyenv activate 3.3.1/envs/my_pro_3.3.1 # 兩個都可以進去 pyenv-virtualenv: prompt changing will be removed from future release. configure `export PYENV_VIRTUALENV_DISABLE_PROMPT=1' to simulate the behavior. (3.3.1/envs/my_pro_3.3.1) ➜ ~ pyenv deactivate ➜ ~ pyenv activate my_pro_3.3.1 pyenv-virtualenv: prompt changing will be removed from future release. configure `export PYENV_VIRTUALENV_DISABLE_PROMPT=1' to simulate the behavior. (my_pro_3.3.1) ➜ ~ pyenv deactivate # 離開 ➜ ~ rm -rf .pyenv/versions/3.3.1 # 刪除 ➜ ~ pyenv versions # 看來其中一個應該是我們安裝的版本信息 pyenv: version `3.3.1' is not installed (set by /home/wang/.python-version) system ➜ ~ rm -rf .pyenv/versions/my_pro_3.3.1 # 當安裝了virtualenv插件之后就會都顯示,相當於也使用了virtualenv ➜ ~ pyenv versions # 實現的,但是沒有安裝就沒有顯示 pyenv: version `3.3.1' is not installed (set by /home/wang/.python-version) system
參考:
http://www.it165.net/pro/html/201405/13603.html
http://seisman.info/python-pyenv.html
https://github.com/comynli/pybooklet/blob/master/install.md