安裝
快捷安裝
# mac
$ brew install pyenv
源碼安裝
# Check out pyenv
$ git clone https://github.com/pyenv/pyenv.git ~/.pyenv
# Define environment variable PYENV_ROOT
$ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
$ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
# Add pyenv init to your shell
$ echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bash_profile
# Restart your shell
exec "$SHELL"
# Just run!
$ pyenv -v
pyenv 1.2.17-1-g89786b90
注意:
- Zsh:修改
~/.zshrc
文件而不是~/.bash_profile
。 - Ubuntu、Fedora:修改
~/.bashrc
文件而不是~/.bash_profile
。
使用
安裝需要的python版本:
# install python
$ pyenv install 2.7.8
$ pyenv install 3.8.2
$ pyenv versions
system
2.7.8
* 3.8.2 (set by /Users/youshu/.pyenv/version)
一般有一個2.7,一個3.8就夠了。
如果不確定哪些版本支持,可以查看pyenv支持安裝的Python版本有哪些:
$ pyenv install --list
使用指定版本Python:
$ pyenv local 2.7.8
$ python -V
Python 2.7.8
使用local
指令只是在當前shell切換到指定版本,如果需要全局生效,則需要使用global
指令:
$ pyenv global 3.8.2
$ python -V
Python 3.8.2
使用最新版本
如果你需要的版本不在pyenv支持的列表里,可以手動新增。~/.pyenv/plugins/python-build/share/python-build/
目錄存放的就是各個版本的下載地址,文件名就是Python的版本名。
$ ls ~/.pyenv/plugins/python-build/share/python-build/
2.1.3 3.3.6 anaconda-1.6.1 ironpython-2.7.7 miniconda3-3.9.1 pypy-5.1 pypy3.3-5.2-alpha1
...
下面是3.8.2的內容:
$ cat ~/.pyenv/plugins/python-build/share/python-build/3.8.2
#require_gcc
prefer_openssl11
export PYTHON_BUILD_CONFIGURE_WITH_OPENSSL=1
install_package "openssl-1.1.0j" "https://www.openssl.org/source/openssl-1.1.0j.tar.gz#31bec6c203ce1a8e93d5994f4ed304c63ccf07676118b6634edded12ad1b3246" mac_openssl --if has_broken_mac_openssl
install_package "readline-8.0" "https://ftpmirror.gnu.org/readline/readline-8.0.tar.gz#e339f51971478d369f8a053a330a190781acb9864cf4c541060f12078948e461" mac_readline --if has_broken_mac_readline
if has_tar_xz_support; then
install_package "Python-3.8.2" "https://www.python.org/ftp/python/3.8.2/Python-3.8.2.tar.xz#2646e7dc233362f59714c6193017bb2d6f7b38d6ab4a0cb5fbac5c36c4d845df" ldflags_dirs standard verify_py38 copy_python_gdb ensurepip
else
install_package "Python-3.8.2" "https://www.python.org/ftp/python/3.8.2/Python-3.8.2.tgz#e634a7a74776c2b89516b2e013dda1728c89c8149b9863b8cea21946daf9d561" ldflags_dirs standard verify_py38 copy_python_gdb ensurepip
fi
幫助
$ pyenv -h
Usage: pyenv <command> [<args>]
Some useful pyenv commands are:
--version Display the version of pyenv
commands List all available pyenv commands
exec Run an executable with the selected Python version
global Set or show the global Python version
help Display help for a command
hooks List hook scripts for a given pyenv command
init Configure the shell environment for pyenv
install Install a Python version using python-build
local Set or show the local application-specific Python version
prefix Display prefix for a Python version
rehash Rehash pyenv shims (run this after installing executables)
root Display the root directory where versions and shims are kept
shell Set or show the shell-specific Python version
shims List existing pyenv shims
uninstall Uninstall a specific Python version
version Show the current Python version and its origin
version-file Detect the file that sets the current pyenv version
version-name Show the current Python version
version-origin Explain how the current Python version is set
versions List all Python versions available to pyenv
whence List all Python versions that contain the given executable
which Display the full path to an executable
See `pyenv help <command>' for information on a specific command.
For full documentation, see: https://github.com/pyenv/pyenv#readme
參考
1、pyenv/pyenv: Simple Python version management
https://github.com/pyenv/pyenv