Mac下python多版本html
安裝步驟:python
如下安裝採用brewgit
1. 查找是否有pyenv包github
# Teron @ TeronsMac in ~ [22:26:16] $ brew search pyenv ==> Searching local taps... pyenv pyenv-pip-migrate pyenv-virtualenvwrapper pyenv-ccache pyenv-virtualenv pyenv-which-ext ==> Searching taps on GitHub... ==> Searching blacklisted, migrated and deleted formulae...
2. 安裝pyenvsql
# Teron @ TeronsMac in ~ [22:26:36] $ brew install pyenv Updating Homebrew... ==> Auto-updated Homebrew! Updated 1 tap (homebrew/core). ==> Updated Formulae angular-cli mongoose shairport-sync convox paket vim docker-machine-parallels povray xorriso mongo-c-driver pygobject3 xtensor ==> Downloading https://homebrew.bintray.com/bottles/pyenv-1.1.3.sierra.bottle.t Already downloaded: /Users/Teron/Library/Caches/Homebrew/pyenv-1.1.3.sierra.bottle.tar.gz ==> Pouring pyenv-1.1.3.sierra.bottle.tar.gz 🍺 /usr/local/Cellar/pyenv/1.1.3: 581 files, 2.2MB
3. brew list查看pyenv是否安裝docker
# Teron @ TeronsMac in ~ [22:29:43] $ brew list autoconf openssl readline brew-cask-completion pkg-config sqlite gdbm pyenv xz makedepend python3
4. pyenv -v 查看已安裝shell
# Teron @ TeronsMac in ~ [22:32:00] $ pyenv -v pyenv 1.1.3 # Teron @ TeronsMac in ~ [22:32:08] $ pyenv versions * system (set by /Users/Teron/.python-version)
5. pyenv基本命令vim
$ pyenv version # 查看當前系統使用的python版本 $ pyenv versions # 查看當前系統擁有的python版本 $ pyenv install --list: # 列出全部能夠下載的python版本. $ pyenv install 3.4.1 # 安裝3.4.1,可以使用-v參數查看詳細輸出 $ pyenv uninstall 3.4.1 # 卸載 $ pyenv local 3.4.1 # local僅對當前目錄及子目錄生效,告訴當前目錄使用版本2.7.5, $ pyenv global # 告訴全局環境使用某個版本,爲了避免破壞系統環境,不建議使用global設置全局版本 $ pyenv rehash # 重建環境變量,每當你增刪 Python 版本或帶有可執行文件的包(如 pip)之後,都應該執行一次本命令
6. 安裝python2.7.13,3.6.2版本ruby
# Teron @ TeronsMac in ~ [22:36:26] C:2 $ pyenv install 2.7.13 Downloading Python-2.7.13.tar.xz... -> https://www.python.org/ftp/python/2.7.13/Python-2.7.13.tar.xz Installing Python-2.7.13... Installed Python-2.7.13 to /Users/Teron/.pyenv/versions/2.7.13 # Teron @ TeronsMac in ~ [22:38:33] $ pyenv install 3.6.2 Installing Python-3.6.2... Installed Python-3.6.2 to /Users/Teron/.pyenv/versions/3.6.2 # Teron @ TeronsMac in ~ [22:42:55] $ pyenv versions * system (set by /Users/Teron/.python-version) 2.7.13 3.6.2 # Teron @ TeronsMac in ~ [22:43:55] $ pyenv rehash //Run this command after you install a new version of Python, or install a package that provides binaries.
7. 切換版本,發現沒切過去,須要執行if which pyenv > /dev/null; then eval "$(pyenv init -)"; fibash
# Teron @ TeronsMac in ~ [22:43:32] $ pyenv local 2.7.13 # Teron @ TeronsMac in ~ [23:17:58] $ python -V Python 2.7.10 # Teron @ TeronsMac in ~ [23:20:15] $ if which pyenv > /dev/null; then eval "$(pyenv init -)"; fi # Teron @ TeronsMac in ~ [23:20:29] $ python -V Python 2.7.13
8. 須要往當前用戶shell配置文件中寫入以上配置,個人是zsh環境,則在.zshrc文件最後寫入
if which pyenv > /dev/null; then eval "$(pyenv init -)"; fi
pyenv的魔法可以實現是由於它實際上重定義了你的Python命令:
$ which python /Users/Teron/.pyenv/shims/python
當你嘗試運行Python的時候,它首先查看當前目錄下的.python-version目錄來決定到底哪一個版本的Python該運行。若是它沒有發現這個文件,那麼它以後回繼續查找用戶級別的文件~/.pyenv/version。
9. 安裝pyenv-virtualenv
# Teron @ TeronsMac in ~ [23:23:49] $ brew install pyenv-virtualenv Updating Homebrew... ==> Auto-updated Homebrew! Updated 1 tap (homebrew/core). ==> Updated Formulae ruby-build streamlink ==> Downloading https://github.com/yyuu/pyenv-virtualenv/archive/v1.1.0.tar.gz Already downloaded: /Users/Teron/Library/Caches/Homebrew/pyenv-virtualenv-1.1.0.tar.gz ==> ./install.sh ==> Caveats To enable auto-activation add to your profile: if which pyenv-virtualenv-init > /dev/null; then eval "$(pyenv virtualenv-init -)"; fi ==> Summary 🍺 /usr/local/Cellar/pyenv-virtualenv/1.1.0: 20 files, 60.2KB, built in 1 second # Teron @ TeronsMac in ~ [23:24:31] $ brew list autoconf openssl python3 brew-cask-completion pkg-config readline gdbm pyenv sqlite makedepend pyenv-virtualenv xz
10. 安裝完同時也要在.zshrc中加入以下內容
if which pyenv-virtualenv-init > /dev/null; then eval "$(pyenv virtualenv-init -)"; fi
11. pyenv-virtualenv usage
pyenv virtualenv 2.7.13 env-test:建立名爲env-test的虛擬環境,對應的版本爲2.7.13 pyenv activate env-test:切換到env-test虛擬環境 pyenv deactivate:退回系統環境 # Teron @ TeronsMac in ~ [23:28:10] $ pyenv virtualenv 2.7.13 env-test Collecting virtualenv Using cached virtualenv-15.1.0-py2.py3-none-any.whl Installing collected packages: virtualenv Successfully installed virtualenv-15.1.0 New python executable in /Users/Teron/.pyenv/versions/2.7.13/envs/env-test/bin/python2.7 Also creating executable in /Users/Teron/.pyenv/versions/2.7.13/envs/env-test/bin/python Installing setuptools, pip, wheel...done. Requirement already satisfied: setuptools in /Users/Teron/.pyenv/versions/2.7.13/envs/env-test/lib/python2.7/site-packages Requirement already satisfied: pip in /Users/Teron/.pyenv/versions/2.7.13/envs/env-test/lib/python2.7/site-packages # Teron @ TeronsMac in ~ [23:31:23] $ pyenv versions system 2.7.13 2.7.13/envs/env-test * 3.6.2 (set by /Users/Teron/.python-version) env-test #### 後面的 set by /Users/Teron/.python-version意思指: #### 我當前目錄是/Users/Teron/,pyenv設置版本時,其實是在當前目錄建立.python-version文件記錄版本; $ cat .python-version 3.6.2 # Teron @ TeronsMac in ~ [23:31:34] $ pyenv activate env-test Failed to activate virtualenv. Perhaps pyenv-virtualenv has not been loaded into your shell properly. Please restart current shell and try again. # 按要求重啓終端窗口ok了 # Teron @ TeronsMac in ~ [23:32:47] $ pyenv activate env-test pyenv-virtualenv: prompt changing will be removed from future release. configure `export PYENV_VIRTUALENV_DISABLE_PROMPT=1' to simulate the behavior. (env-test) # Teron @ TeronsMac in ~ [23:32:50] $ pip list DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning. pip (9.0.1) setuptools (36.4.0) wheel (0.30.0) (env-test) # Teron @ TeronsMac in ~ [23:33:53] $ pip install requests Collecting requests Retrying (Retry(total=4, connect=None, read=None, redirect=None)) after connection broken by 'NewConnectionError('<pip._vendor.requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x10bffbb90>: Failed to establish a new connection: [Errno 65] No route to host',)': /simple/requests/ Downloading requests-2.18.4-py2.py3-none-any.whl (88kB) 100% |████████████████████████████████| 92kB 104kB/s Collecting certifi>=2017.4.17 (from requests) Downloading certifi-2017.7.27.1-py2.py3-none-any.whl (349kB) 100% |████████████████████████████████| 358kB 54kB/s Collecting chardet<3.1.0,>=3.0.2 (from requests) Downloading chardet-3.0.4-py2.py3-none-any.whl (133kB) 100% |████████████████████████████████| 143kB 48kB/s Collecting idna<2.7,>=2.5 (from requests) Downloading idna-2.6-py2.py3-none-any.whl (56kB) 100% |████████████████████████████████| 61kB 30kB/s Collecting urllib3<1.23,>=1.21.1 (from requests) Downloading urllib3-1.22-py2.py3-none-any.whl (132kB) 100% |████████████████████████████████| 133kB 18kB/s Installing collected packages: certifi, chardet, idna, urllib3, requests Successfully installed certifi-2017.7.27.1 chardet-3.0.4 idna-2.6 requests-2.18.4 urllib3-1.22 (env-test) # Teron @ TeronsMac in ~ [23:34:44] $ pip list DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning. certifi (2017.7.27.1) chardet (3.0.4) idna (2.6) pip (9.0.1) requests (2.18.4) setuptools (36.4.0) urllib3 (1.22) wheel (0.30.0) (env-test) # Teron @ TeronsMac in ~ [23:35:04] $ pyenv deactivate # Teron @ TeronsMac in ~ [23:35:35] $ pip list DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning. pip (9.0.1) setuptools (28.8.0) # deactivate 後,pip list看不到env-test裏安裝的requests,環境隔離了
12. 列出全部的virtual環境,以及刪除某個環境
# Teron @ TeronsMac in ~ [23:35:39] $ pyenv virtualenvs 2.7.13/envs/env-test (created from /Users/Teron/.pyenv/versions/2.7.13) env-test (created from /Users/Teron/.pyenv/versions/2.7.13) # Teron @ TeronsMac in ~ [23:37:59] $ pyenv uninstall env-test pyenv-virtualenv: remove /Users/Teron/.pyenv/versions/2.7.13/envs/env-test? y # Teron @ TeronsMac in ~ [23:39:01] $ pyenv virtualenvs # 可見剛纔的env-test環境已經沒了 # Teron @ TeronsMac in ~ [23:39:18] $ pyenv versions system 2.7.13 * 3.6.2 (set by /Users/Teron/.python-version)
virtualenv
is a very popular tool that creates isolated Python environments for Python libraries. If you're not familiar with this tool, I highly recommend learning it, as it is a very useful tool, and I'll be making comparisons to it for the rest of this answer.
It works by installing a bunch of files in a directory (eg: env/
), and then modifying the PATH
environment variable to prefix it with a custom bin
directory (eg: env/bin/
). An exact copy of the python
or python3
binary is placed in this directory, but Python is programmed to look for libraries relative to its path first, in the environment directory. It's not part of Python's standard library, but is officially blessed by the PyPA (Python Packaging Authority). Once activated, you can install packages in the virtual environment using pip
.
pyenv
is used to isolate Python versions. For example, you may want to test your code against Python 2.6, 2.7, 3.3, 3.4 and 3.5, so you'll need a way to switch between them. Once activated, it prefixes the PATH
environment variable with ~/.pyenv/shims
, where there are special files matching the Python commands (python
, pip
). These are not copies of the Python-shipped commands; they are special scripts that decide on the fly which version of Python to run based on the PYENV_VERSION
environment variable, or the .python-version
file, or the ~/.pyenv/version
file. pyenv
also makes the process of downloading and installing multiple Python versions easier, using the command pyenv install
.
pyenv-virtualenv
is a plugin for pyenv
by the same author as pyenv
, to allow you to use pyenv
and virtualenv
at the same time conveniently. However, if you're using Python 3.3 or later, pyenv-virtualenv
will try to run python -m venv
if it is available, instead of virtualenv
. You can use virtualenv
and pyenv
together without pyenv-virtualenv
, if you don't want the convenience features.
virtualenvwrapper
is a set of extensions to virtualenv
(see docs). It gives you commands like mkvirtualenv
, lssitepackages
, and especially workon
for switching between different virtualenv
directories. This tool is especially useful if you want multiple virtualenv
directories.
pyenv-virtualenvwrapper
is a plugin for pyenv
by the same author as pyenv
, to conveniently integrate virtualenvwrapper
into pyenv
.
pipenv
, by Kenneth Reitz (the author of requests
), is a brand new (possibly experimental) project that aims to combine Pipfile, pip and virtualenv into one command on the command-line.
pyvenv
is a script shipped with Python 3 but deprecated in Python 3.6 as it had problems (not to mention the confusing name). In Python 3.6+, the exact equivalent is python3 -m venv
.
venv
is a package shipped with Python 3, which you can run using python3 -m venv
(although for some reason some distros separate it out into a separate distro package, such as python3-venv
on Ubuntu/Debian). It serves a similar purpose to virtualenv
, and works in a very similar way, but it doesn't need to copy Python binaries around (except on Windows). Use this if you don't need to support Python 2. At the time of writing, the Python community seems to be happy with virtualenv
and I haven't heard much talk of venv
.