时间:2023-01-18 11:01:18 | 栏目:Python代码 | 点击:次
pyenv 可在不同 python 版本之间轻松切换,实现 python 环境隔离,且支持自动激活和退出虚拟环境
git clone git://github.com/pyenv/pyenv.git ~/.pyenv
bash
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc echo 'export PATH="$PYENV_ROOT/shims:$PATH"' >> ~/.bashrc echo 'eval "$(pyenv init -)"' >> ~/.bashrc exec $SHELL -l
zsh
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc echo 'export PATH="$PYENV_ROOT/shims:$PATH"' >> ~/.zshrc echo 'eval "$(pyenv init -)"' >> ~/.zshrc exec $SHELL -l
安装
brew install pyenv
根据自身环境,将下方内容加到对应文件中: .bashrc / .zshrc
export PYENV_ROOT=/usr/local/var/pyenv export PATH="$PYENV_ROOT/bin:$PATH" export PATH="$PYENV_ROOT/shims:$PATH" if which pyenv > /dev/null; then eval "$(pyenv init -)"; fi
验证安装是否成功:
pyenv
基本使用
命令 | 描述 |
---|---|
pyenv --version | 查看 pyenv 的版本 |
pyenv versions | 罗列当前已安装的所有 python 环境,如果是当前正在使用的环境,则前面会有个 * |
pyenv help | 查看帮助 |
pyenv init | 如果输入 pyenv 之后使用 tab 不补全,可以使用该命令进行初始即可使用补全命令 |
安装环境
命令 | 描述 |
---|---|
pyenv install -l | 显示可以安装的版本列表 |
pyenv install 版本号 | 安装指定版本的 python |
pyenv rehash | 更新本地数据库,安装指定版本的 python 后使用 |
环境应用
命令 | 描述 |
---|---|
pyenv global 版本号 | 更改本机版本,重启不会造成再次更改 |
pyenv local 版本号 | 会在当前目录创建 .python-version 文件,并记录设置的 python 环境,每次进入该目录会自动设置成该 python 环境 |
pyenv shell 版本号 | 更改当前 shell 下使用的 python 版本,临时生效,优先级高于 global |
cd .pyenv/plugins git clone https://github.com/pyenv/pyenv-virtualenv.git # 安装virtualenv插件
brew install pyenv-virtualenv
无论使用上述的哪种方式进行的安装,请根据自身环境,将下方内容加到对应文件中: .bashrc / .zshrc
eval "$(pyenv virtualenv-init -)"
命令 | 描述 |
---|---|
pyenv virtualenv 3.8.3 env383 | 创建 3.8.3 版本虚拟环境 |
pyenv virtualenvs | 显示环境 |
pyenv activate env383 | 激活使用指定的虚拟环境 |
pyenv deactivate | 退出当前虚拟环境 |
rm -rf .pyenv/versions/3.8.3 | 删除版本环境 |
rm -rf .pyenv/versions/env383 | 删除虚拟环境 |
在需要使用虚拟环境的目录(通常是项目目录)中:
之后每次进/出该目录时,虚拟环境都将自动激活/退出。
# 命令 pyenv activate env383 # 报错信息 Failed to activate virtualenv. Perhaps pyenv-virtualenv has not been loaded into your shell properly. Please restart current shell and try again.
别慌,重启你的终端,或者执行如下命令即可
exec $SHELL -l
如果你用 pyenv versions 查看,明明已经切换成功,但是用 python -V却还是系统版本。
原因是 pyenv没有加到 $PATH环境变量里去,解决办法如下:
export PYENV_ROOT=~/.pyenv export PATH=$PYENV_ROOT/shims:$PATH
安装 python报错,如
pyenv install 3.8.3
报错内容:
python-build: use openssl@1.1 from homebrew python-build: use readline from homebrew Downloading Python-3.8.3.tar.xz... -> https://www.python.org/ftp/python/3.8.3/Python-3.8.3.tar.xz Installing Python-3.8.3... python-build: use readline from homebrew python-build: use zlib from xcode sdk BUILD FAILED (OS X 12.0.1 using python-build 20180424) Inspect or clean up the working tree at /var/folders/cy/8zkx5hys2tsgb1d_x42_r8qc0000gn/T/python-build.20220122174148.1531 Results logged to /var/folders/cy/8zkx5hys2tsgb1d_x42_r8qc0000gn/T/python-build.20220122174148.1531.log Last 10 log lines: clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include -std=c99 -Wextra -Wno-unused-result -Wno-unused-parameter -Wno-missing-field-initializers -Wstrict-prototypes -Werror=implicit-function-declaration -I./Include/internal -I. -I./Include -I/usr/local/opt/readline/include -I/usr/local/opt/readline/include -I/usr/local/var/pyenv/versions/3.8.3/include -I/usr/local/opt/sqlite/include -I/usr/local/opt/readline/include -I/usr/local/opt/readline/include -I/usr/local/var/pyenv/versions/3.8.3/include -I/usr/local/opt/sqlite/include -DPy_BUILD_CORE_BUILTIN -c ./Modules/_codecsmodule.c -o Modules/_codecsmodule.o clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include -std=c99 -Wextra -Wno-unused-result -Wno-unused-parameter -Wno-missing-field-initializers -Wstrict-prototypes -Werror=implicit-function-declaration -I./Include/internal -I. -I./Include -I/usr/local/opt/readline/include -I/usr/local/opt/readline/include -I/usr/local/var/pyenv/versions/3.8.3/include -I/usr/local/opt/sqlite/include -I/usr/local/opt/readline/include -I/usr/local/opt/readline/include -I/usr/local/var/pyenv/versions/3.8.3/include -I/usr/local/opt/sqlite/include -DPy_BUILD_CORE_BUILTIN -c ./Modules/_weakref.c -o Modules/_weakref.o ./Modules/posixmodule.c:9221:15: error: implicit declaration of function 'sendfile' is invalid in C99 [-Werror,-Wimplicit-function-declaration] ret = sendfile(in, out, offset, &sbytes, &sf, flags); ^ clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include -std=c99 -Wextra -Wno-unused-result -Wno-unused-parameter -Wno-missing-field-initializers -Wstrict-prototypes -Werror=implicit-function-declaration -I./Include/internal -I. -I./Include -I/usr/local/opt/readline/include -I/usr/local/opt/readline/include -I/usr/local/var/pyenv/versions/3.8.3/include -I/usr/local/opt/sqlite/include -I/usr/local/opt/readline/include -I/usr/local/opt/readline/include -I/usr/local/var/pyenv/versions/3.8.3/include -I/usr/local/opt/sqlite/include -DPy_BUILD_CORE_BUILTIN -DPy_BUILD_CORE_BUILTIN -I./Include/internal -c ./Modules/_functoolsmodule.c -o Modules/_functoolsmodule.o 1 error generated. make: *** [Modules/posixmodule.o] Error 1 make: *** Waiting for unfinished jobs.... 1 warning generated.
使用下面的命令安装即可,注意修改成相应的版本号
CFLAGS="-I$(brew --prefix openssl)/include -I$(brew --prefix bzip2)/include -I$(brew --prefix readline)/include -I$(xcrun --show-sdk-path)/usr/include" LDFLAGS="-L$(brew --prefix openssl)/lib -L$(brew --prefix readline)/lib -L$(brew --prefix zlib)/lib -L$(brew --prefix bzip2)/lib" pyenv install --patch 3.8.3 < <(curl -sSL https://github.com/python/cpython/commit/8ea6353.patch\?full_index\=1)
1,基于每个用户更改全局Python版本
2,对每个项目的Python版本提供支持
3,允许通过环境变量覆盖Python版本
4,一次搜索多个版本的Python
在一个高的级别上,pyenv使用shim可执行文件注入到PATH来拦截Python命令;确定您的应用程序指定了哪个Python版本,并将命令传递给正确的已安装Python版本。
当运行诸如python或pip等命令时,操作系统将在目录列表中搜索以查找具有该名称的可执行文件。此目录列表位于一个名为PATH的环境变量中,列表中的每个目录都用冒号分隔:
/usr/local/bin:/usr/bin:/bin
PATH从左到右搜索目录,因此列表开头目录中的匹配可执行文件优先于结尾目录中的另一个可执行文件。在这个例子中, /usr/local/bin目录将被搜索的第一个,然后是/usr/bin,然后是/bin。
pyenv通过在你的PATH前插入shims目录来工作:
$(pyenv root)/shims:/usr/local/bin:/usr/bin:/bin
Shim是轻量级的可执行文件,它们只是将命令传递给pyenv。因此在安装了pyenv的情况下,运行pip时操作系统将执行以下操作:
1,在PATH中搜索名称为pip的可执行文件
2,在PATH开头的shims目录中,找到名称为pip的shim文件
3,运行名称为pip的shime文件,shime将命令传递给pyenv